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(); }
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)); }
@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); }
@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); }
@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(); }
@Test public void testAssignmentOfAnonymousClass() { Statement stmt = ObjectBuilder.newInstanceOf(Retention.class) .extend() .publicOverridesMethod("annotationType") .append(Stmt.load(Retention.class).returnValue()) .finish() .finish(); Statement declaration = Stmt.declareVariable(java.lang.annotation.Annotation.class) .named("foo").initializeWith(stmt); String cls = declaration.generate(Context.create()); assertEquals("java.lang.annotation.Annotation foo = new java.lang.annotation.Retention() {\n" + " public Class annotationType() {\n" + " return java.lang.annotation.Retention.class;\n" + " }\n" + "\n" + "\n" + "};", cls); }
@Test public void testForLoopUnchainedWithDeclaringInitializerAndCountingExpression() { String s = StatementBuilder.create() .for_(Stmt.declareVariable(int.class).named("i").initializeWith(0), Bool.expr(Variable.get("i"), BooleanOperator.LessThan, 100), StatementBuilder.create().loadVariable("i").assignValue(AssignmentOperator.PreIncrementAssign, 1)) .append(StatementBuilder.create().loadStatic(System.class, "out").invoke("println", Variable.get("i"))) .finish().toJavaString(); assertEquals("Failed to generate for loop with declaring initializer and counting expression", FOR_DECLARE_INITIALIZER_COUNTING_EXP, s); }
@Test public void testDeclareVariableWithObjectInitializationUsingSuperClassType() { String declaration = ContextBuilder.create() .declareVariable("str", Object.class) .initializeWith(ObjectBuilder.newInstanceOf(String.class).withParameters("abc")) .toJavaString(); assertEquals("failed to generate variable declaration using an objectbuilder initialization with parameters", "Object str = new String(\"abc\");", declaration); try { Stmt.declareVariable("str", Integer.class, ObjectBuilder.newInstanceOf(String.class).withParameters("abc")) .toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } try { Stmt.declareVariable("str", String.class, ObjectBuilder.newInstanceOf(Object.class)) .toJavaString(); fail("Expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } }
.append( Stmt.declareVariable(CreationalCallback.class).asFinal().named("icc") .initializeWith( Stmt.newObject(CreationalCallback.class).extend() .publicOverridesMethod("callback", Parameter.of(Object.class, "beanInstance", true))
.append( Stmt.declareVariable(CreationalCallback.class).asFinal().named("icc") .initializeWith( Stmt.newObject(CreationalCallback.class).extend() .publicOverridesMethod("callback", Parameter.of(Object.class, "beanInstance", true))