private Statement generateInterceptorLogic(ClassStructureBuilder<?> classBuilder, MetaMethod method, Statement requestLogic, List<Statement> parmVars, List<Class<?>> interceptors) { final Statement callContext = ProxyUtil.generateProxyMethodCallContext(context, RemoteCallContext.class, classBuilder.getClassDefinition(), method, requestLogic, interceptors).finish(); return Stmt.try_() .append( Stmt.declareVariable(CallContextStatus.class).asFinal().named("status").initializeWith( Stmt.newObject(CallContextStatus.class).withParameters(interceptors.toArray()))) .append( Stmt.declareVariable(RemoteCallContext.class).asFinal().named("callContext") .initializeWith(callContext)) .append( Stmt.loadVariable("callContext").invoke("setParameters", Stmt.newArray(Object.class).initialize(parmVars.toArray()))) .append( Stmt.loadVariable("callContext").invoke("proceed")) .finish() .catch_(Throwable.class, "throwable") .append( If.cond(Bool.notEquals(Stmt.loadVariable("errorCallback"), Stmt.loadLiteral(null))) .append( If.cond(Stmt.loadVariable("errorCallback").invoke("error", Stmt.load(null), Variable.get("throwable"))) .append(Stmt.loadVariable("this").invoke("invokeDefaultErrorHandlers", Variable.get("throwable"))) .finish() ).finish() .else_() .append(Stmt.loadVariable("this").invoke("invokeDefaultErrorHandlers", Variable.get("throwable"))) .finish()) .finish(); }
@Test public void testDeclareVariableWithTypeLiteral() { @SuppressWarnings("serial") String declaration = ContextBuilder.create(Context.create().autoImport()) .declareVariable("list", new TypeLiteral<List<String>>() {}) .finish() .toJavaString(); assertEquals("failed to generate variable declaration using a type literal", "List<String> list;", declaration); }
public static BlockBuilder<StatementEnd> autoForLoop(final String varName, final Statement value) { return Stmt.for_(Stmt.declareVariable(int.class).named("i").initializeWith(0), Bool.lessThan(Variable.get("i"), value), new StringStatement(varName + "++", null)); }
public static BlockBuilder<StatementEnd> autoForLoop(final String varName, final Statement value) { return Stmt.for_(Stmt.declareVariable(int.class).named("i").initializeWith(0), Bool.lessThan(Variable.get("i"), value), new StringStatement(varName + "++", null)); }
.initializeWith(invokeStatic(GWT.class, "create", constructed.get(declaringClass)))); .declareVariable(Element.class) .named(parentOfRootTemplateElementVarName) .initializeWith( Stmt.invokeStatic(TemplateUtil.class, "getRootTemplateParentElement", (customProvider) ? Variable.get("template") :
.initializeWith(invokeStatic(GWT.class, "create", constructed.get(declaringClass)))); .declareVariable(Element.class) .named(parentOfRootTemplateElementVarName) .initializeWith( Stmt.invokeStatic(TemplateUtil.class, "getRootTemplateParentElement", (customProvider) ? Variable.get("template") :
@Test public void testDeclareVariableWithIntegerTypeInference() { String declaration = ContextBuilder.create() .declareVariable("n") .initializeWith(10) .toJavaString(); assertEquals("failed to generate variable declaration using a literal initialization and type inference", "Integer n = 10;", declaration); }
@Test public void testDeclareVariableWithStringTypeInference() { String declaration = ContextBuilder.create() .declareVariable("n") .initializeWith("10") .toJavaString(); assertEquals("failed to generate variable declaration using a literal initialization and type inference", "String n = \"10\";", declaration); }
@Test public void testDeclareVariableWithImplicitTypeConversion() { String declaration = ContextBuilder.create() .declareVariable("n", Integer.class) .initializeWith("10") .toJavaString(); assertEquals("failed to generate variable declaration using a literal initialization and type conversion", "Integer n = 10;", declaration); }
@Test public void testDeclareVariableWithExactTypeProvided() { String declaration = ContextBuilder.create() .declareVariable("n", Integer.class) .initializeWith(10) .toJavaString(); assertEquals("failed to generate variable declaration using a literal initialization", "Integer n = 10;", declaration); }
@Test public void testDeclareVariableWithObjectInitialization() { String declaration = ContextBuilder.create() .declareVariable("str", String.class) .initializeWith(ObjectBuilder.newInstanceOf(String.class)) .toJavaString(); assertEquals("failed to generate variable declaration using an objectbuilder initialization", "String str = new String();", declaration); }
.initializeWith(Stmt.invokeStatic(GWT.class, "create", messageBundleResourceInterface)));
@Test public void testDeclareVariableWithObjectInitializationWithParameters() { String declaration = ContextBuilder.create() .declareVariable("str", String.class) .initializeWith(ObjectBuilder.newInstanceOf(String.class).withParameters("abc")) .toJavaString(); assertEquals("failed to generate variable declaration using an objectbuilder initialization with parameters", "String str = new String(\"abc\");", declaration); }
.initializeWith(Stmt.invokeStatic(GWT.class, "create", messageBundleResourceInterface)));
@Test public void testDeclareVariableWithInvalidInitialization() { try { ContextBuilder.create() .declareVariable("n", Integer.class) .initializeWith("abc") .toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException ive) { // expected assertTrue(ive.getCause() instanceof NumberFormatException); } }
@Test public void testIfElseBlockUsingNoRhs() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", "abc") .if_() ._(Stmt.declareVariable(Integer.class).named("n").initializeWith(0)) .finish() .else_() ._(Stmt.declareVariable(Integer.class).named("n").initializeWith(1)) .finish().toJavaString(); assertEquals("Failed to generate empty if block using no rhs", IF_ELSE_BLOCK_NO_RHS, s); }
@Test public void testIfElseBlockUsingRhs() { String s = StatementBuilder.create() .declareVariable("n", Integer.class) .declareVariable("m", Integer.class) .loadVariable("n") .if_(BooleanOperator.GreaterThan, Variable.get("m")) ._(Stmt.declareVariable(Integer.class).named("n").initializeWith(0)) .finish() .else_() ._(Stmt.declareVariable(Integer.class).named("n").initializeWith(1)) .finish().toJavaString(); assertEquals("Failed to generate empty if block using a rhs", IF_ELSE_BLOCK_RHS, s); }
new ParameterizedEntityType(et.getJavaType())), entityTypeMethodName); cmm.append(Stmt.declareVariable(ErraiEntityType.class).asFinal().named("entityType") .initializeWith(entityTypeSubclass.finish().withParameters(et.getName(), et.getJavaType())));
@Test public void testDeclareFinalVariable() { final Context ctx = Context.create(); final String s = StatementBuilder.create(ctx) .declareVariable(String.class).asFinal().named("str").initializeWith("10").toJavaString(); assertEquals("failed to generate final variable declaration", "final String str = \"10\";", s); final VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertTrue("Variable should be final", ctx.getVariables().get("str").isFinal()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); }
private Statement generateInterceptorLogic(ClassStructureBuilder<?> classBuilder, MetaMethod method, Statement requestLogic, List<Statement> parmVars, List<Class<?>> interceptors) { final Statement callContext = ProxyUtil.generateProxyMethodCallContext(context, RemoteCallContext.class, classBuilder.getClassDefinition(), method, requestLogic, interceptors).finish(); return Stmt.try_() .append( Stmt.declareVariable(CallContextStatus.class).asFinal().named("status").initializeWith( Stmt.newObject(CallContextStatus.class).withParameters(interceptors.toArray()))) .append( Stmt.declareVariable(RemoteCallContext.class).asFinal().named("callContext") .initializeWith(callContext)) .append( Stmt.loadVariable("callContext").invoke("setParameters", Stmt.newArray(Object.class).initialize(parmVars.toArray()))) .append( Stmt.loadVariable("callContext").invoke("proceed")) .finish() .catch_(Throwable.class, "throwable") .append( If.cond(Bool.notEquals(Stmt.loadVariable("errorCallback"), Stmt.loadLiteral(null))) .append( If.cond(Stmt.loadVariable("errorCallback").invoke("error", Stmt.load(null), Variable.get("throwable"))) .append(Stmt.loadVariable("this").invoke("invokeDefaultErrorHandlers", Variable.get("throwable"))) .finish() ).finish() .else_() .append(Stmt.loadVariable("this").invoke("invokeDefaultErrorHandlers", Variable.get("throwable"))) .finish()) .finish(); }