Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import org.codehaus.groovy.ast.InnerClassNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.ast.stmt.TryCatchStatement;
import org.codehaus.groovy.classgen.asm.BytecodeHelper;
import org.codehaus.groovy.control.CompilationUnit;
Expand All @@ -54,17 +56,22 @@
import static org.codehaus.groovy.ast.ClassHelper.VOID_TYPE;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callSuperX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.catchS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorSuperX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorThisX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.notX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
import static org.codehaus.groovy.ast.tools.GeneralUtils.throwS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.tryCatchS;
Expand Down Expand Up @@ -341,6 +348,9 @@ public void visit(final MethodVisitor mv) {
final ClassNode returnType, final Parameter[] parameters, final BiConsumer<BlockStatement, Parameter[]> consumer) {
MethodNode method = innerClass.getDeclaredMethod(methodName, parameters);
if (method == null) {
ClassNode mme = ClassHelper.make(groovy.lang.MissingMethodException.class);
ClassNode mpe = ClassHelper.make(groovy.lang.MissingPropertyException.class);

// try {
// <consumer dispatch>
// } catch (MissingMethodException notFound) {
Expand All @@ -352,10 +362,10 @@ public void visit(final MethodVisitor mv) {
Expression selfType = varX("this");
if ((modifiers & ACC_STATIC) == 0) selfType = callX(selfType, "getClass");
if (methodName.endsWith("methodMissing")) {
exceptionT = ClassHelper.make(groovy.lang.MissingMethodException.class);
exceptionT = mme;
newException = ctorX(exceptionT, args(propX(varX(catchParam),"method"), selfType, propX(varX(catchParam),"arguments")));
} else {
exceptionT = ClassHelper.make(groovy.lang.MissingPropertyException.class);
exceptionT = mpe;
newException = ctorX(exceptionT, args(propX(varX(catchParam), "property"), selfType, propX(varX(catchParam), "cause")));
}
catchParam.setType(exceptionT);
Expand All @@ -365,7 +375,30 @@ public void visit(final MethodVisitor mv) {
TryCatchStatement tryCatch = tryCatchS(handleMissing);
tryCatch.addCatch(catchS(catchParam, throwS(newException)));

innerClass.addSyntheticMethod(methodName, modifiers, returnType, parameters, ClassNode.EMPTY_ARRAY, tryCatch);
Statement methodBody = tryCatch;
if ((modifiers & ACC_STATIC) == 0) { // GROOVY-11823
// if (!"methodMissing".equals(name))
Expression noRecursion = notX(callX(constX(methodName), "equals", varX(parameters[0])));
// try {
// return super.methodMissing(name,args)
// } catch (MissingMethodException ignore) {
// } catch (MissingPropertyException ignore) {
// }
BlockStatement trySuperClass = block();
if (!ClassHelper.isPrimitiveVoid(returnType)) {
trySuperClass.addStatement(returnS(callSuperX(methodName, args(parameters))));
} else {
trySuperClass.addStatement(stmt(callSuperX(methodName, args(parameters))));
trySuperClass.addStatement(returnS(ConstantExpression.EMPTY_EXPRESSION));
}
tryCatch = tryCatchS(trySuperClass);
tryCatch.addCatch(catchS(param(mme, "ignore"), block()));
if (methodName.equals("propertyMissing"))
tryCatch.addCatch(catchS(param(mpe, "ignore"), block()));

methodBody = block(ifS(noRecursion, tryCatch), methodBody);
}
innerClass.addSyntheticMethod(methodName, modifiers, returnType, parameters, ClassNode.EMPTY_ARRAY, methodBody);

// if there is a user-defined method, add compiler error and continue
} else if (isStatic(innerClass) && (method.getModifiers() & ACC_SYNTHETIC) == 0) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/groovy/gls/innerClass/InnerClassTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2242,6 +2242,7 @@ final class InnerClassTest {
def err = shouldFail """
class Upper {
$returnType propertyMissing(String name, Object value) {
throw new MissingPropertyException(name, getClass())
}
}
class Outer {
Expand All @@ -2253,6 +2254,25 @@ final class InnerClassTest {
assert err =~ /No such property: missing for class: Outer.Inner/
}

// GROOVY-11823
@Test
void testNestedPropertyHandling5() {
assertScript '''
class Upper {
Object propertyMissing(String name) {
if (name == 'fizz') return 'buzz'
throw new MissingPropertyException(name, getClass())
}
}
class Outer {
static class Inner extends Upper {
}
}
def inner = new Outer.Inner()
assert inner.fizz == 'buzz'
'''
}

// GROOVY-7312
@Test
void testInnerClassOfInterfaceIsStatic() {
Expand Down
Loading