@Override public BlockBuilder<AnonymousClassStructureBuilder> publicOverridesMethod(final String name, final Parameter... args) { final List<MetaClass> types = new ArrayList<MetaClass>(); Arrays.stream(args).forEach(a -> types.add(a.getType())); final MetaMethod method = classDefinition.getSuperClass() .getBestMatchingMethod(name, types.toArray(new MetaClass[args.length])); if (method == null) throw new UndefinedMethodException("Can't override (inherited method not found):" + classDefinition.getFullyQualifiedNameWithTypeParms() + "." + name + "(" + types + ")"); return publicOverridesMethod(method, DefParameters.fromParameters(args)); }
@Test public void testInvokeUndefinedStaticMethod() { try { StatementBuilder.create() .invokeStatic(Integer.class, "intValue") .toJavaString(); fail("expected UndefinedMethodException"); } catch (UndefinedMethodException udme) { // expected assertEquals("Wrong exception details", udme.getMethodName(), "intValue"); } }
@Test public void testInvokeUndefinedMethodOnVariable() { try { StatementBuilder.create() .declareVariable("obj", Object.class) .declareVariable("param", String.class) .loadVariable("obj") .invoke("undefinedMethod", Variable.get("param")) .toJavaString(); fail("expected UndefinedMethodException"); } catch (UndefinedMethodException udme) { // expected assertEquals("Wrong exception thrown", udme.getMethodName(), "undefinedMethod"); } }
@Override public BlockBuilder<AnonymousClassStructureBuilder> publicOverridesMethod(final String name, final Parameter... args) { final List<MetaClass> types = new ArrayList<MetaClass>(); Arrays.stream(args).forEach(a -> types.add(a.getType())); final MetaMethod method = classDefinition.getSuperClass() .getBestMatchingMethod(name, types.toArray(new MetaClass[args.length])); if (method == null) throw new UndefinedMethodException("Can't override (inherited method not found):" + classDefinition.getFullyQualifiedNameWithTypeParms() + "." + name + "(" + types + ")"); return publicOverridesMethod(method, DefParameters.fromParameters(args)); }
@Test public void testInvokeChainedUndefinedMethod() { try { StatementBuilder.create() .declareVariable("s", String.class) .declareVariable("regex", String.class) .declareVariable("replacement", String.class) .loadVariable("s") .invoke("replaceAll", Variable.get("regex"), Variable.get("replacement")) .invoke("undefinedMethod", Variable.get("regex"), Variable.get("replacement")) .toJavaString(); fail("expected UndefinedMethodException"); } catch (UndefinedMethodException udme) { // expected assertEquals("Wrong exception thrown", udme.getMethodName(), "undefinedMethod"); } }
public static Statement[] generateCallParameters(final MetaMethod method, final Context context, final Object... parameters) { if (parameters.length != method.getParameters().length && !method.isVarArgs()) { throw new UndefinedMethodException("Wrong number of parameters"); } final MetaParameter[] methParms = method.getParameters(); final Statement[] statements = new Statement[parameters.length]; int i = 0; for (Object parameter : parameters) { if (parameter instanceof Statement) { if (((Statement) parameter).getType() == null) { parameter = generate(context, parameter); } } try { statements[i] = convert(context, parameter, methParms[i++].getType()); } catch (final GenerationException t) { t.appendFailureInfo("in method call: " + method.getDeclaringClass().getFullyQualifiedName() + "." + method.getName() + "(" + Arrays.toString(methParms) + ")"); throw t; } } return statements; }
@Test public void testDefineClassWithMethodCallingInvalidMethodOnThis() { try { ClassBuilder.define("org.foo.Foo") .publicScope() .body() .publicMethod(void.class, "bar") .append(Stmt.loadVariable("this").invoke("foo", "invalidParam")) .finish() .publicMethod(String.class, "foo") .append(Stmt.load(null).returnValue()) .finish() .toJavaString(); fail("exprected UndefinedMethodException"); } catch (UndefinedMethodException udme) { // expected assertEquals("Wrong exception thrown", udme.getMethodName(), "foo"); } }
public static Statement[] generateCallParameters(final MetaMethod method, final Context context, final Object... parameters) { if (parameters.length != method.getParameters().length && !method.isVarArgs()) { throw new UndefinedMethodException("Wrong number of parameters"); } final MetaParameter[] methParms = method.getParameters(); final Statement[] statements = new Statement[parameters.length]; int i = 0; for (Object parameter : parameters) { if (parameter instanceof Statement) { if (((Statement) parameter).getType() == null) { parameter = generate(context, parameter); } } try { statements[i] = convert(context, parameter, methParms[i++].getType()); } catch (final GenerationException t) { t.appendFailureInfo("in method call: " + method.getDeclaringClass().getFullyQualifiedName() + "." + method.getName() + "(" + Arrays.toString(methParms) + ")"); throw t; } } return statements; }
@Test public void testDefineClassWithMethodCallingInvalidMethodOnSuper() { try { ClassBuilder.define("org.foo.Foo") .publicScope() .body() .publicMethod(void.class, "bar") .append(Stmt.loadVariable("this").invoke("foo")) .finish() .publicMethod(String.class, "foo") .append(Stmt.loadVariable("super").invoke("undefinedMethod")) .finish() .toJavaString(); fail("exprected UndefinedMethodException"); } catch (UndefinedMethodException udme) { // expected assertEquals("Wrong exception thrown", udme.getMethodName(), "undefinedMethod"); } }
if (context.isPermissiveMode()) { try { blameAndRethrow( new UndefinedMethodException(statement.getType(), methodName, parameterTypes) ); throw new UndefinedMethodException(statement.getType(), methodName, parameterTypes);
if (context.isPermissiveMode()) { try { blameAndRethrow( new UndefinedMethodException(statement.getType(), methodName, parameterTypes) ); throw new UndefinedMethodException(statement.getType(), methodName, parameterTypes);