public static StatementBuilder declareVariable(final String name, final MetaClass type, final Object initialization) { return StatementBuilder.create().declareVariable(name, type, initialization); }
public static StatementBuilder declareFinalVariable(final String name, final MetaClass type, final Object initialization) { return StatementBuilder.create().declareFinalVariable(name, type, initialization); }
@Test public void testCreateAndInitializeArrayWithMissingInitializationAndDimensions() { try { StatementBuilder.create().newArray(String.class).toJavaString(); fail("Expected RuntimeException"); } catch (Exception e) { // expected assertEquals("Wrong exception details", "Must provide either dimension expressions or an array initializer", e.getMessage()); } }
@Test public void testObjectCreationWithParameterizedType() { final String s = StatementBuilder.create().newObject(new TypeLiteral<ArrayList<String>>() { }).toJavaString(); assertEquals("failed to generate new object with parameterized type", "new java.util.ArrayList<String>()", s); }
@Test public void testInvokeWithParameterizedListAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("list", new TypeLiteral<List<String>>() { }) .declareVariable("str", String.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("list"))) .toJavaString(); assertEquals("Failed to generate method invocation with variable return type inferred from List<T>", "String str = Foo.bar(list);", s); }
@Test public void testInvokeWithParameterizedMapAndVariableReturnType() { String s = StatementBuilder.create(Context.create().autoImport()) .declareVariable("map", new TypeLiteral<Map<String, Integer>>() { }) .declareVariable("val", Integer.class, StatementBuilder.create().invokeStatic(Foo.class, "bar", Variable.get("map"))) .toJavaString(); assertEquals("Failed to generate method invocation with variable return type inferred from Map<K, V>", "Integer val = Foo.bar(map);", s); }
@Test public void testWhileLoopChainedWithEmptyExpressionWithBody() { String s = StatementBuilder.create() .declareVariable("b", Boolean.class) .loadVariable("b") .while_() .append(StatementBuilder.create().loadVariable("b").assignValue(false)) .finish().toJavaString(); assertEquals("Failed to generate while loop with chained lhs and body", WHILE_WITH_BODY, s); }
@Test public void testForLoopUnchainedWithInitializer() { String s = StatementBuilder.create() .declareVariable("i", Integer.class) .for_(StatementBuilder.create().loadVariable("i").assignValue(0), Bool.expr(Variable.get("i"), BooleanOperator.LessThan, 100)) .finish().toJavaString(); assertEquals("Failed to generate for loop with initializer", FOR_INITIALIZER_NO_COUNTING_EXP_EMPTY, s); }
@Test public void testEmptyTryBlock() { String s = StatementBuilder.create() .try_() .finish() .toJavaString(); assertEquals("Failed to generate empty try catch block", EMPTY_TRY_FINALLY_BLOCK, s); }
@Test public void testSwitchBlockWithInvalidStatement() { try { StatementBuilder.create() .switch_(Stmt.loadStatic(System.class, "out")) .toJavaString(); fail("expected InvalidTypeException"); } catch (InvalidTypeException e) { // expected } }
@Test public void testInvokeWithLiteralParameters() { String result = StatementBuilder.create().declareVariable("s", String.class) .loadVariable("s").invoke("replaceAll", "foo", "foo\t\n").toJavaString(); assertEquals("Failed to generate invocation using literal parameters", "s.replaceAll(\"foo\", \"foo\\t\\n\")", result); }
@Test public void testInvokeBestMatchingMethod() { String s = StatementBuilder.create() .declareVariable("n", Integer.class) .loadVariable("n") // 1 will be inferred to LiteralValue<Integer>, equals(Integer.class) // should be matched equals(Object.class) .invoke("equals", 1) .toJavaString(); assertEquals("Failed to generate invocation on matched method", "n.equals(1)", s); }
@Test public void testObjectCreationWithAutoImportedParameterizedType() { final Context c = Context.create().autoImport(); final String s = StatementBuilder.create(c).newObject(new TypeLiteral<ArrayList<Date>>() { }).toJavaString(); assertEquals("failed to generate new object with parameterized type", "new ArrayList<Date>()", s); }
@Test public void testThrowExceptionUsingNewInstanceWithParameters() { final Context c = Context.create().autoImport(); final String s = StatementBuilder.create(c).throw_(InvalidTypeException.class, "message").toJavaString(); assertEquals("failed to generate throw statement using a new instance", "throw new InvalidTypeException(\"message\")", s); }
@Test public void testForeachLoopWithNullCheckAndProviderVarType() { String foreachWithListOfStrings = StatementBuilder.create() .declareVariable("list", new TypeLiteral<List<String>>() {}) .loadVariable("list") .foreachIfNotNull("element", Object.class) .finish().toJavaString(); assertEquals("Failed to generate foreach loop using a List<String> and null check", FOREACH_OBJECT_IN_LIST_NOT_NULL, foreachWithListOfStrings); }
@Test public void testTryFinallyBlock() { String s = StatementBuilder.create() .try_() .finish() .finally_() .finish() .toJavaString(); assertEquals("Failed to generate empty try finally block", EMPTY_TRY_FINALLY_BLOCK, s); }
@Test public void testWhileLoopChainedWithExpression() { String s = StatementBuilder.create() .declareVariable("str", String.class) .loadVariable("str") .invoke("length") .while_(BooleanOperator.GreaterThanOrEqual, 2) .finish().toJavaString(); assertEquals("Failed to generate while loop with chained lhs, rhs and no body", WHILE_RHS_EMPTY, s); }
@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()); }
@Test public void testDeclareVariableWithObjectInitializationWithExactTypeProvided() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("str", String.class, ObjectBuilder.newInstanceOf(String.class)).generate(ctx); assertEquals("failed to generate variable declaration with object initialization and type provided", "String str = new String();", 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()); }
@Test public void testDeclareVariableWithExactTypeProvided() { final Context ctx = Context.create(); final String s = StatementBuilder.create().declareVariable("n", Integer.class, 10).generate(ctx); assertEquals("failed to generate variable declaration with type provided", "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()); }