@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"); } }
@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"); } }
@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"); } }
@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"); } }