protected AbstractStatementBuilder(Context context, final CallElementBuilder callElementBuilder) { if (context == null) { context = Context.create(); } this.context = context; this.callElementBuilder = callElementBuilder; }
public static void assertIsIterable(final Statement statement) { final Class<?> cls = statement.getType().asClass(); if (!cls.isArray() && !Iterable.class.isAssignableFrom(cls)) throw new TypeNotIterableException(statement.generate(Context.create())); }
private Variable(final String name, final MetaClass type, final Object initialization) { this(name, type); final LiteralValue<?> val = LiteralFactory.isLiteral(initialization); if (val != null) { this.type = (type == null) ? val.getType() : type; this.value = GenUtil.convert(Context.create(), initialization, this.type); } else { // deferred initialization this.initialization = initialization; } }
public static ClassStructureBuilder<?> implement(final MetaClass cls) { return new ClassBuilder<DefaultClassStructureBuilder>(cls.getFullyQualifiedName() + "Impl", null, Context.create() .autoImport()) .publicScope() .implementsInterface(cls).body(); }
@Test public void testEnumReference() { final ContextualStatementBuilder statementBuilder = Stmt.loadStatic(TEnum.class, "FIRST"); assertEquals(TEnum.class.getName() + ".FIRST", statementBuilder.generate(Context.create())); } }
/** * This tests that any caching in LiteralFactory does not affect literal * values generated from mutated arrays. */ @Test public void testGenerateIntArrayThenModifyThenGenerateAgain() { final int[] a = new int[] { 1, 2, 3, 4 }; assertEquals("new int[] { 1, 2, 3, 4 }", LiteralFactory.getLiteral(a).generate(Context.create())); a[0] = 10; assertEquals("new int[] { 10, 2, 3, 4 }", LiteralFactory.getLiteral(a).generate(Context.create())); }
private CaseBlockBuilder switch_(final SwitchBlock switchBlock) { this.switchBlock = switchBlock; appendCallElement(new DeferredCallElement((writer, context, statement) -> { if (statement != null) { switchBlock.setSwitchExpr(statement); switchBlock.setSwitchExpr(writer.getCallString()); } writer.reset(); writer.append(switchBlock.generate(Context.create(context))); })); return this; }
@Test public void testStringArrayCreation() { final String[][] input = new String[][]{{"Hello1", "Hello2"}, {"Hello3", "Hello4"}}; final String expected = "new String[][] { { \"Hello1\", \"Hello2\" }, { \"Hello3\", \"Hello4\" } }"; assertEquals(expected, LiteralFactory.getLiteral(input).generate(Context.create())); }
@Test public void testThrowExceptionUsingNewInstance() { final Context c = Context.create().autoImport(); final String s = StatementBuilder.create(c).throw_(InvalidTypeException.class).toJavaString(); assertEquals("failed to generate throw statement using a new instance", "throw new InvalidTypeException()", s); }
@Test public void testCastDown() { final Statement stmt = Cast.to(String.class, Stmt.declareVariable("obj", Object.class).loadVariable("obj")); assertEquals("failed to generate cast", "(String) obj", stmt.generate(Context.create())); }
@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); }
@Test public void testBitwiseOrExpression() { final String generate = Bitwise.or(Stmt.load(1), Stmt.load(2), Stmt.load(3)).generate(Context.create()); assertEquals("1 | 2 | 3", generate); }
@Test public void testObjectCreationWithFullyQualifiedParameterizedTypeAndClassImport() { final Context c = Context.create().addImport(MetaClassFactory.get(ArrayList.class)); final String s = StatementBuilder.create(c).newObject(new TypeLiteral<ArrayList<Date>>() { }).toJavaString(); assertEquals("failed to generate new object with parameterized type", "new ArrayList<java.util.Date>()", s); }
@Test public void testObjectCreationWithParameterizedTypeAndClassImport() { final Context c = Context.create().addImport(MetaClassFactory.get(ArrayList.class)); final String s = StatementBuilder.create(c).newObject(new TypeLiteral<ArrayList<String>>() { }).toJavaString(); assertEquals("failed to generate new object with parameterized type", "new ArrayList<String>()", s); }
@Test public void testWhileLoopUnchainedWithExpression() { Context ctx = Context.create().addVariable("str", String.class); String s = StatementBuilder.create(ctx) .while_(Bool.expr(Stmt.loadVariable("str").invoke("length"), BooleanOperator.GreaterThanOrEqual, 2)) .finish().toJavaString(); assertEquals("Failed to generate while loop with rhs and no body", WHILE_RHS_EMPTY, s); }
@Test public void testInvokeWithParameterTypeConversionOfVariable() { Context c = Context.create().addVariable("n", Integer.class, 123); String s = StatementBuilder.create(c) .declareVariable("str", String.class) .loadVariable("str") .invoke("endsWith", c.getVariable("n").getValue()) .toJavaString(); assertEquals("Failed to generate invocation with parameter type conversion of variable", "str.endsWith(\"123\")", s); }
@Test public void testForeachLoopWithStringInArray() { Statement createObject = StatementBuilder.create().newObject(String.class); String foreachWithStringArray = StatementBuilder.create(Context.create().addVariable("list", String[].class)) .loadVariable("list") .foreach("element") .append(createObject) .finish() .toJavaString(); assertEquals("Failed to generate foreach loop using a String[]", FOREACH_STRING_IN_ARRAY_ONE_STATEMENT, foreachWithStringArray); }
@Test public void testPassBitwiseToMethodParameter() { final Statement bitwiseStatement = Bitwise.or(Stmt.load(1), Stmt.load(2), Expr.qualify(Bitwise.and(Stmt.load(10), Stmt.load(20)))); final String generate = Stmt.newObject(Integer.class).withParameters(bitwiseStatement).generate(Context.create()); assertEquals("new Integer(1 | 2 | (10 & 20))", generate); }
@Test public void testDeclareVariableWithStringTypeInference() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", "10").generate(ctx); assertEquals("failed to generate variable declaration with =String type inference", "String n = \"10\";", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral("10"), n.getValue()); }
@Test public void testDeclareVariableWithIntegerTypeInference() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", 10).generate(ctx); assertEquals("failed to generate variable declaration with Integers type inference", "Integer n = 10;", s); final VariableReference n = ctx.getVariable("n"); assertEquals("Wrong variable name", "n", n.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(Integer.class), n.getType()); Assert.assertEquals("Wrong variable value", LiteralFactory.getLiteral(10), n.getValue()); }