private Statement generateRequest(ClassStructureBuilder<?> classBuilder, MetaMethod method, Statement methodParams, boolean intercepted) { final Statement sendable = Stmt .invokeStatic(MessageBuilder.class, "createCall") .invoke("call", remote.getFullyQualifiedName()) .invoke("endpoint", ProxyUtil.createCallSignature(method), Stmt.loadClassMember("qualifiers"), methodParams) .invoke("respondTo", method.getReturnType().asBoxed(), Stmt.loadVariable("remoteCallback")) .invoke("errorsHandledBy", Stmt.loadVariable("errorCallback")); final BlockStatement requestBlock = new BlockStatement(); requestBlock.addStatement(Stmt.declareVariable("sendable", RemoteCallSendable.class, sendable)); requestBlock.addStatement(Stmt.loadStatic(classBuilder.getClassDefinition(), "this") .invoke("sendRequest", Variable.get("bus"), Variable.get("sendable"))); return requestBlock; } }
@Override public StringBuilderBuilder append(final Object statement) { statementBuilder.invoke("append", statement); return this; }
public static Collection<Statement> createAllPropertyBindings(final Statement proxyRef, final Map<String, ProxyProperty> proxyProperties) { final List<Statement> statementList = new ArrayList<Statement>(); for (final Map.Entry<String, ProxyProperty> entry : proxyProperties.entrySet()) { statementList.add(Stmt.nestedCall(proxyRef).invoke("$set_" + entry.getKey(), entry.getValue().getOriginalValueReference())); } return statementList; }
public static Collection<Statement> createAllPropertyBindings(final Statement proxyRef, final Map<String, ProxyProperty> proxyProperties) { final List<Statement> statementList = new ArrayList<Statement>(); for (final Map.Entry<String, ProxyProperty> entry : proxyProperties.entrySet()) { statementList.add(Stmt.nestedCall(proxyRef).invoke("$set_" + entry.getKey(), entry.getValue().getOriginalValueReference())); } return statementList; }
private static void addCleanup(final Decorable decorable, final FactoryController controller, final List<Statement> destructionStmts) { final DataBindingUtil.DataBinderRef dataBinder = DataBindingUtil.lookupDataBinderRef(decorable, controller); if (!controller.hasAttribute(STYLE_BINDING_HOUSEKEEPING_ATTR)) { destructionStmts.add( Stmt.invokeStatic(StyleBindingsRegistry.class, "get").invoke("cleanAllForBean", Refs.get("instance"))); if (dataBinder != null) { destructionStmts.add(controller.getReferenceStmt("styleBindingChangeHandlerUnsub", PropertyChangeUnsubscribeHandle.class).invoke("unsubscribe")); } controller.setAttribute(STYLE_BINDING_HOUSEKEEPING_ATTR, Boolean.TRUE); } }
private static void addCleanup(final Decorable decorable, final FactoryController controller, final List<Statement> destructionStmts) { final DataBindingUtil.DataBinderRef dataBinder = DataBindingUtil.lookupDataBinderRef(decorable, controller); if (!controller.hasAttribute(STYLE_BINDING_HOUSEKEEPING_ATTR)) { destructionStmts.add( Stmt.invokeStatic(StyleBindingsRegistry.class, "get").invoke("cleanAllForBean", Refs.get("instance"))); if (dataBinder != null) { destructionStmts.add(controller.getReferenceStmt("styleBindingChangeHandlerUnsub", PropertyChangeUnsubscribeHandle.class).invoke("unsubscribe")); } controller.setAttribute(STYLE_BINDING_HOUSEKEEPING_ATTR, Boolean.TRUE); } }
private static void _doRunnableTasks(final Collection<MetaClass> classes, final BlockBuilder<?> blockBuilder) { for (final MetaClass clazz : classes) { if (!clazz.isAssignableTo(Runnable.class)) { throw new RuntimeException("annotated @IOCBootstrap task: " + clazz.getName() + " is not of type: " + Runnable.class.getName()); } blockBuilder.append(Stmt.nestedCall(Stmt.newObject(clazz)).invoke("run")); } }
public Statement extractJSONObjectProperty(final String fieldName, final MetaClass fromType) { if (fromType.getFullyQualifiedName().equals(EJObject.class.getName())) { return loadVariable("obj").invoke("get", fieldName); } else { return Stmt.nestedCall(Cast.to(fromType, loadVariable("a0"))).invoke("get", fieldName); } }
@Override protected List<Statement> generateCreateInstanceStatements(final ClassStructureBuilder<?> bodyBlockBuilder, final Injectable injectable, final DependencyGraph graph, final InjectionContext injectionContext) { return Collections.<Statement> singletonList( Stmt.castTo(injectable.getInjectedType(), invokeStatic(WindowInjectionContextStorage.class, "createOrGet") .invoke("getBean", injectable.getInjectedType().getFullyQualifiedName())).returnValue()); }
@Test public void testTypeInferenceWorksPropertyForParameterizedMethodTypes() { final String s = Stmt.loadStatic(BeanWithTypeParmedMeths.class, "INSTANCE") .invoke("setFooBarMap", Stmt.loadStatic(BeanWithTypeParmedMeths.class, "INSTANCE").invoke("getFooBarMap")) .toJavaString(); assertEquals("org.jboss.errai.codegen.test.model.BeanWithTypeParmedMeths.INSTANCE" + ".setFooBarMap(org.jboss.errai.codegen.test.model.BeanWithTypeParmedMeths.INSTANCE.getFooBarMap())", s); }
public void marshallEnum(final ContextualStatementBuilder bb, final Statement valueStatement, final MetaClass toType) { final Implementations.StringBuilderBuilder internalSBB = Implementations.newStringBuilder() .append("{\"").append(SerializationParts.ENCODED_TYPE) .append( "\":\"").append( toType.getFullyQualifiedName()).append("\",\"") .append(SerializationParts.ENUM_STRING_VALUE).append("\":\"") .append(Stmt.nestedCall(valueStatement).invoke("name")).append("\"}"); final TernaryStatement ternaryStatement = new TernaryStatement( Bool.isNotNull(valueStatement), internalSBB, Stmt.load("null")); bb.invoke("append", ternaryStatement); }
@Test public void testInvokeUsingStandardizedLoadLiteral() { String s = StatementBuilder.create() .load("foo") .invoke("toUpperCase").toJavaString(); assertEquals("Failed injecting literal with load()", "\"foo\".toUpperCase()", s); }
@Test public void testInvokeOnLiteral() { String result = StatementBuilder.create().loadLiteral("foo").invoke("toString").toJavaString(); assertEquals("Failed to generate invocation using literal parameters", "\"foo\".toString()", result); }
private Object createHandlerForMethod(final MetaMethod method) { return ObjectBuilder .newInstanceOf(PropertyChangeHandler.class) .extend() .publicOverridesMethod("onPropertyChange", finalOf(PropertyChangeEvent.class, "event")) .append(castTo(method.getDeclaringClass(), loadVariable("agent").loadField("target")).invoke(method, loadVariable("event"))) .finish() .finish(); }
private void implementEquals(final ClassStructureBuilder<?> proxyImpl) { proxyImpl.publicMethod(boolean.class, "equals", Parameter.of(Object.class, "obj")).body() .append(loadVariable("obj").assignValue(invokeStatic(Factory.class, "maybeUnwrapProxy", loadVariable("obj")))) .append(loadVariable("proxyHelper").invoke("getInstance", loadVariable("this")).invoke("equals", loadVariable("obj")).returnValue()) .finish(); }
@Test public void testInvokeUsingStandardizedLoadVariableInstance() { Context context = ContextBuilder.create() .addVariable("s", String.class) .getContext(); Variable v = Variable.create("s", String.class); String s = StatementBuilder.create(context) .load(v) .invoke("toUpperCase").toJavaString(); assertEquals("Failed using load() passing a variable instance", "s.toUpperCase()", s); }
@Test public void testNestedCall() { final String s = StatementBuilder.create() .nestedCall( StatementBuilder.create().declareVariable("n", Integer.class).loadVariable("n").invoke("toString")) .invoke("getBytes") .toJavaString(); assertEquals("failed to generate nested call", "n.toString().getBytes()", s); }
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 testDefineClassWithStaticMethod() { final String cls = ClassBuilder.define("my.test.Clazz") .publicScope().body() .publicMethod(void.class, "test").modifiers(Modifier.Static) .body() .append(Stmt.loadStatic(System.class, "out").invoke("println", "Hello, World!")) .finish() .toJavaString(); assertEquals("failed to generate class with static method", CLASS_WITH_STATIC_METHOD, cls); }
@Test public void testDeclareVariableWithStatementInitialization() { final Context ctx = Context.create(); final String s = Stmt.declareVariable("str", String.class, Stmt.nestedCall(Stmt.newObject(Integer.class).withParameters(2)).invoke("toString")) .generate(ctx); assertEquals("failed to generate variable declaration with statement initialization", "String str = new Integer(2).toString();", s); final VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); }