@Override public MetaClass getType() { return reference.getType(); } }
@Override public MetaClass getType() { return reference.getType(); } }
@Override public String generate(Context context) { type = context.getVariable(name).getType(); return name; }
@Override public String generate(Context context) { type = context.getVariable(name).getType(); return name; }
public static Variable from(final VariableReference ref) { return new Variable(ref.getName(), ref.getType()); }
@Override public String generate(final Context context) { final String type = LoadClassReference.getClassReference(reference.getType(), context, true); if (statement != null) { return (isFinal ? "final " : "") + type + " " + super.generate(context); } else { return (isFinal ? "final " : "") + type + " " + reference.getName(); } } }
public static StatementBuilder declareVariable(final VariableReference ref, final Object initialization) { return StatementBuilder.create().declareVariable(ref.getName(), ref.getType(), initialization); }
@Override public String generate(final Context context) { final String type = LoadClassReference.getClassReference(reference.getType(), context, true); if (statement != null) { return (isFinal ? "final " : "") + type + " " + super.generate(context); } else { return (isFinal ? "final " : "") + type + " " + reference.getName(); } } }
public static StatementBuilder declareFinalVariable(final VariableReference ref, final Object initialization) { return StatementBuilder.create().declareFinalVariable(ref.getName(), ref.getType(), initialization); }
@Test public void testAddVariableWithObjectInitializationWithExactTypeProvided() { Context ctx = ContextBuilder.create().addVariable("str", String.class, ObjectBuilder.newInstanceOf(String.class)).getContext(); VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), str.getType()); // variable value cannot be verified before initialization statement was generated. Variable v = ctx.getVariables().get("str"); v.generate(ctx); assertNotNull("Value could not be generated", v.getValue()); }
@Test public void testAddVariableWithStringTypeInference() { Context ctx = ContextBuilder.create().addVariable("n", "10").getContext(); 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 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 testAddVariableWithExactTypeProvided() { Context ctx = ContextBuilder.create().addVariable("n", Integer.class, 10).getContext(); 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()); }
@Test public void testAddVariableWithIntegerTypeInference() { Context ctx = ContextBuilder.create().addVariable("n", 10).getContext(); 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()); }
@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 testDeclareVariableWithObjectInitializationWithStringTypeInference() { final Context ctx = Context.create(); final String s = StatementBuilder.create(ctx) .declareVariable("str", ObjectBuilder.newInstanceOf(String.class)).toJavaString(); assertEquals("failed to generate variable declaration with object initialization and string type inference", "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 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()); }
@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()); }
@Test public void testDeclareVariableWithStatementInitialization() { final Context ctx = Context.create(); final String s = Stmt.declareVariable("str", String.class, Stmt.nestedCall(Stmt.newObject(Integer.class).withParameters(2)).invoke("toString")) .generate(ctx); assertEquals("failed to generate variable declaration with statement initialization", "String str = new Integer(2).toString();", 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()); }