private void maybeImplementDestroyInstance(final ClassStructureBuilder<?> bodyBlockBuilder, final Injectable injectable, final List<Statement> destroyInstanceStatements) { if (!destroyInstanceStatements.isEmpty()) { bodyBlockBuilder .publicMethod(void.class, "generatedDestroyInstance", finalOf(Object.class, "instance"), finalOf(ContextManager.class, "contextManager")) .append(loadVariable("this").invoke("destroyInstanceHelper", Stmt.castTo(injectable.getInjectedType(), loadVariable("instance")), loadVariable("contextManager"))) .finish(); bodyBlockBuilder.publicMethod(void.class, "destroyInstanceHelper", finalOf(injectable.getInjectedType(), "instance"), finalOf(ContextManager.class, "contextManager")) .appendAll(destroyInstanceStatements).finish(); } }
private static Object createAccessorImpl(final MetaClass type, final String varName) { final MetaClass propertyType = type.getMethod("getValue", new Class[0]).getReturnType(); return ObjectBuilder.newInstanceOf(NativeHasValueAccessors.Accessor.class) .extend() .publicMethod(Object.class, "get") .append(loadVariable(varName).invoke("getValue").returnValue()) .finish() .publicMethod(void.class, "set", finalOf(Object.class, "value")) .append(loadVariable(varName).invoke("setValue", castTo(propertyType, loadVariable("value")))) .finish() .finish(); } }
private static Object createAccessorImpl(final MetaClass type, final String varName) { final MetaClass propertyType = type.getMethod("getValue", new Class[0]).getReturnType(); return ObjectBuilder.newInstanceOf(NativeHasValueAccessors.Accessor.class) .extend() .publicMethod(Object.class, "get") .append(loadVariable(varName).invoke("getValue").returnValue()) .finish() .publicMethod(void.class, "set", finalOf(Object.class, "value")) .append(loadVariable(varName).invoke("setValue", castTo(propertyType, loadVariable("value")))) .finish() .finish(); } }
private void createPutMarshallerMethod() { classStructureBuilder .privateMethod(boolean.class, "putMarshaller", Parameter.of(String.class, "fqcn"), Parameter.of(Marshaller.class, "m")) .append(Stmt.loadVariable(MARSHALLERS_VAR).invoke("put", Stmt.loadVariable("fqcn"), Stmt.loadVariable("m"))) .append(Stmt.loadLiteral(true).returnValue()) .finish(); }
private void createPutMarshallerMethod() { classStructureBuilder .privateMethod(boolean.class, "putMarshaller", Parameter.of(String.class, "fqcn"), Parameter.of(Marshaller.class, "m")) .append(Stmt.loadVariable(MARSHALLERS_VAR).invoke("put", Stmt.loadVariable("fqcn"), Stmt.loadVariable("m"))) .append(Stmt.loadLiteral(true).returnValue()) .finish(); }
if (currentBlock != null) { currentBlock .append(lookupMethod); currentBlock.modifiers(Modifier.Static).finish(); currentBlock.append(Stmt.invokeStatic(CDIEventTypeLookup.class, "get") .invoke("addLookup", subClass.getFullyQualifiedName(), subClass.getSuperClass().getFullyQualifiedName())); currentBlock.append(Stmt.invokeStatic(CDIEventTypeLookup.class, "get") .invoke("addLookup", subClass.getFullyQualifiedName(), interfaceClass.getFullyQualifiedName()));
/** * Generates a getter method for the provided property plus the corresponding code for the * implementation of {@link HasProperties#get(String)}. */ private void generateGetter(final ClassStructureBuilder<?> classBuilder, final String property, final CaseBlockBuilder switchBlock) { final MetaMethod getterMethod = bindable.getBeanDescriptor().getReadMethodForProperty(property); if (getterMethod != null && !getterMethod.isFinal() && getterMethod.isPublic()) { BlockBuilder<CaseBlockBuilder> caseBlock = switchBlock.case_(property); caseBlock.append(Stmt.loadVariable("this").invoke(getterMethod.getName()).returnValue()).finish(); classBuilder.publicMethod(getterMethod.getReturnType(), getterMethod.getName()) .append(target().invoke(getterMethod.getName()).returnValue()) .finish(); proxiedAccessorMethods.add(getterMethod); } }
/** * Generates a getter method for the provided property plus the corresponding code for the * implementation of {@link HasProperties#get(String)}. */ private void generateGetter(final ClassStructureBuilder<?> classBuilder, final String property, final CaseBlockBuilder switchBlock) { final MetaMethod getterMethod = bindable.getBeanDescriptor().getReadMethodForProperty(property); if (getterMethod != null && !getterMethod.isFinal() && getterMethod.isPublic()) { BlockBuilder<CaseBlockBuilder> caseBlock = switchBlock.case_(property); caseBlock.append(Stmt.loadVariable("this").invoke(getterMethod.getName()).returnValue()).finish(); classBuilder.publicMethod(getterMethod.getReturnType(), getterMethod.getName()) .append(target().invoke(getterMethod.getName()).returnValue()) .finish(); proxiedAccessorMethods.add(getterMethod); } }
@Test public void testDefineClassWithMethodCallingMethodOnThis() { final String cls = ClassBuilder.define("org.foo.Foo") .publicScope() .body() .publicMethod(void.class, "bar") .append(Stmt.loadVariable("this").invoke("foo")) .finish() .publicMethod(String.class, "foo") .append(Stmt.load(null).returnValue()) .finish() .toJavaString(); assertEquals("failed to generate class with method calling method on this", CLASS_WITH_METHOD_CALLING_METHOD_ON_THIS, cls); }
@Test public void testDefineClassWithMethodCallingMethodOnSuper() { final String cls = 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("toString").returnValue()) .finish() .toJavaString(); assertEquals("failed to generate class with method calling method on this", CLASS_WITH_METHOD_CALLING_METHOD_ON_SUPER, cls); }
private void generateCommonSetter(final ClassStructureBuilder<?> classBuilder) { classBuilder.privateMethod(void.class, "changeAndFire", Parameter.of(String.class, "property"), Parameter.of(Object.class, "value")) .append(Stmt.declareFinalVariable("oldValue", Object.class, Stmt.loadVariable("this").invoke("get", loadVariable("property")))) .append(Stmt.loadVariable("this").invoke("set", loadVariable("property"), loadVariable("value"))) .append(agent().invoke("updateWidgetsAndFireEvent", false, loadVariable("property"), Variable.get("oldValue"), loadVariable("value"))) .finish(); }
private void generateCommonSetter(final ClassStructureBuilder<?> classBuilder) { classBuilder.privateMethod(void.class, "changeAndFire", Parameter.of(String.class, "property"), Parameter.of(Object.class, "value")) .append(Stmt.declareFinalVariable("oldValue", Object.class, Stmt.loadVariable("this").invoke("get", loadVariable("property")))) .append(Stmt.loadVariable("this").invoke("set", loadVariable("property"), loadVariable("value"))) .append(agent().invoke("updateWidgetsAndFireEvent", false, loadVariable("property"), Variable.get("oldValue"), loadVariable("value"))) .finish(); }
@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"); } }
@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 testDefineClassWithAccessorMethods() { final String cls = ClassBuilder.define("org.foo.Foo") .publicScope() .body() .privateField("name", String.class) .initializesWith(Stmt.load("default")) .finish() .publicMethod(String.class, "getName") .append(Stmt.loadVariable("name").returnValue()) .finish() .publicMethod(void.class, "setName", Parameter.of(String.class, "name")) .append(Stmt.loadClassMember("name").assignValue(Variable.get("name"))) .finish() .toJavaString(); assertEquals("failed to generate class definition with accessor methods", CLASS_WITH_ACCESSOR_METHODS, cls); }
@Test public void testDefineClassWithAccessorMethodsUsingThisKeyword() { final String cls = ClassBuilder .define("org.foo.Foo") .publicScope() .body() .privateField("name", String.class) .initializesWith(Stmt.load("default")) .finish() .publicMethod(String.class, "getName") .append(Stmt.loadVariable("name").returnValue()) .finish() .publicMethod(void.class, "setName", Parameter.of(String.class, "name")) .append(Stmt.loadVariable("this.name").assignValue(Variable.get("name"))) .finish() .toJavaString(); assertEquals("failed to generate class definition with accessor methods", CLASS_WITH_ACCESSOR_METHODS, cls); }
@Test public void testThisReferenceWithStmtLoadVariable() { final ClassStructureBuilder<? extends ClassStructureBuilder<?>> body = ClassBuilder.define("org.foo.Foo").publicScope().body(); final String cls = body .publicMethod(body.getClassDefinition(), "getThis") .append(Stmt.loadVariable("this").returnValue()) .finish() .toJavaString(); assertEquals("did not properly render 'this' reference", "package org.foo;\n" + "\n" + "\n" + "public class Foo {\n" + " public Foo getThis() {\n" + " return this;\n" + " }\n" + "}", cls); }
@Test public void testDefineInnerClassInMethod() { final ClassStructureBuilder<?> innerClass = ClassBuilder.define("Inner") .packageScope() .implementsInterface(Serializable.class) .body() .privateField("name", String.class) .finish() .publicMethod(void.class, "setName", Parameter.of(String.class, "name", true)) .append(Stmt.loadClassMember("name").assignValue(Variable.get("name"))) .finish(); final String cls = ClassBuilder.define("foo.bar.Baz") .publicScope() .body() .publicMethod(void.class, "someMethod") .append(new InnerClass(innerClass.getClassDefinition())) .append(Stmt.newObject(innerClass.getClassDefinition())) .finish() .toJavaString(); assertEquals("failed to generate class with method using inner class", CLASS_WITH_METHOD_USING_INNER_CLASS, cls); }
/** Regression test for ERRAI-363. */ @Test public void testReferenceToAnonymousClassMember() throws Exception { ClassStructureBuilder<?> builder = ClassBuilder.define("com.foo.A").publicScope().body(); builder.privateMethod(void.class, "method") .append(ObjectBuilder.newInstanceOf(Runnable.class).extend() .privateField("memberOfRunnable", Object.class).finish() .publicOverridesMethod("run").append(Stmt.loadVariable("memberOfRunnable").invoke("hashCode")).finish() .finish()) .finish(); String javaString = builder.toJavaString(); assertEquals("package com.foo; public class A { private void method() { new Runnable() { private Object memberOfRunnable; public void run() { memberOfRunnable.hashCode(); } }; } }", javaString); }