@Override public StatementBuilder finish() { if (initialization == null) { declareVariable(isFinal ? Variable.createFinal(name, type) : Variable.create(name, type)); } else { declareVariable(isFinal ? Variable.createFinal(name, type, initialization) : Variable.create(name, type, initialization)); } return StatementBuilder.this; } };
public ContextBuilder addVariable(String name, Class<?> type) { context.addVariable(Variable.create(name, type)); return this; }
@Override public StatementBuilder declareVariable(final String name, final Class<?> type, final Object initialization) { return declareVariable(Variable.create(name, type, initialization)); }
@Override public StatementBuilder finish() { if (initialization == null) { declareVariable(isFinal ? Variable.createFinal(name, type) : Variable.create(name, type)); } else { declareVariable(isFinal ? Variable.createFinal(name, type, initialization) : Variable.create(name, type, initialization)); } return StatementBuilder.this; } };
public ContextBuilder addVariable(String name, TypeLiteral<?> type) { context.addVariable(Variable.create(name, type)); return this; }
public ContextBuilder addVariable(String name, Class<?> type) { context.addVariable(Variable.create(name, type)); return this; }
@Override public StatementBuilder declareVariable(final String name, final Class<?> type) { return declareVariable(Variable.create(name, type)); }
public ContextBuilder addVariable(String name, Class<?> type, Object initialization) { context.addVariable(Variable.create(name, type, initialization)); return this; }
public VariableDeclarationInitializer<ContextBuilder> declareVariable(String name, Class<?> type) { return declareVariable(Variable.create(name, type)); }
@Override public StatementBuilder declareVariable(final String name, final Object initialization) { return declareVariable(Variable.create(name, initialization)); }
@Override public StatementBuilder declareVariable(final String name, final MetaClass type, final Object initialization) { return declareVariable(Variable.create(name, type, initialization)); }
public BuildMetaClass(final Context context, final String name) { super(null); this.className = name; if (context != null) { this.context = Context.create(context); this.context.addVariable(Variable.create("this", this)); context.attachClass(this); } }
public BuildMetaClass(final Context context, final String name) { super(null); this.className = name; if (context != null) { this.context = Context.create(context); this.context.addVariable(Variable.create("this", this)); context.attachClass(this); } }
ObjectBuilder(final MetaClass type, final Context context, final CallElementBuilder callElementBuilder) { super(context, callElementBuilder); if (context != null) { context.attachClass(type); Arrays.stream(type.getDeclaredFields()). forEach(f -> context.addVariable(Variable.create(f.getName(), f.getType()))); } this.type = type; }
ObjectBuilder(final MetaClass type, final Context context, final CallElementBuilder callElementBuilder) { super(context, callElementBuilder); if (context != null) { context.attachClass(type); Arrays.stream(type.getDeclaredFields()). forEach(f -> context.addVariable(Variable.create(f.getName(), f.getType()))); } this.type = type; }
/** * Add a variable to the current scope. * * @param name * the name of the variable, must not be null. * @param type * the type of the variable, must not be null. * * @return the current context with the variable added. */ public Context addVariable(final String name, final Class<?> type) { checkThread(); return addVariable(Variable.create(Assert.notNull(name), Assert.notNull(type))); }
@Test public void testCastWithVariableGetAPI() { final Context ctx = Context.create(); ctx.addVariable(Variable.create("str", String.class)); final Statement stmt = Cast.to(Object.class, Variable.get("str")); assertEquals("created a redundant cast", "str", stmt.generate(ctx)); }
@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 testReturnVoid() { final Context ctx = Context.create(); ctx.addVariable(Variable.create("foo", Object.class)); final Statement stmt = Stmt.if_(Bool.isNull(Refs.get("foo"))) .append(Stmt.returnVoid()).finish(); assertEquals("failed to generate return statement", "if (foo == null) {\n" + " return;\n" + "}", stmt.generate(ctx)); }
@Test public void testForeachLoopWithProvidedContext() { Context c = ContextBuilder.create().addVariable(Variable.create("s", String.class)).getContext(); String s = StatementBuilder.create(c) .loadLiteral(new String[] { "s1", "s2" }) .foreach("s") .append(StatementBuilder.create().loadVariable("s").invoke("getBytes")) .finish().toJavaString(); assertEquals("Failed to generate foreach loop using a literal String array", FOREACH_LITERAL_STRING_ARRAY, s); }