public ContextBuilder addVariable(Variable variable) { context.addVariable(variable); return this; }
private static String _getClassReference(final MetaType metaClass, final Context context, final boolean typeParms) { final MetaClass erased; if (metaClass instanceof MetaClass) { erased = ((MetaClass) metaClass).getErased(); } else { return "Object"; } String fqcn = erased.getCanonicalName(); final int idx = fqcn.lastIndexOf('.'); if (idx != -1) { if ((context.isAutoImportActive() || "java.lang".equals(erased.getPackageName())) && !context.hasImport(erased)) { context.addImport(erased); } if (context.hasImport(erased)) { fqcn = fqcn.substring(idx + 1); } } final StringBuilder buf = new StringBuilder(fqcn); if (typeParms) { buf.append(getClassReferencesForParameterizedTypes(((MetaClass) metaClass).getParameterizedType(), context)); } return buf.toString(); }
public void addLiteralizableMetaClasses(final Collection<MetaClass> clazzes) { checkThread(); for (final MetaClass cls : clazzes) { addLiteralizableClass(cls); } }
/** * Returns a reference to the class member {@link Variable} with the given name. * * @param name * the name of the class member variable. * * @return the {@link VariableReference} found, can not be null. * * @throws OutOfScopeException * if member variable with the given name can not be found. */ public VariableReference getClassMember(final String name) { checkThread(); return getVariable(name, true); }
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 static ClassDefinitionBuilderCommentOption<?> define(final String fullyQualifiedName) { return new ClassBuilder<DefaultClassStructureBuilder>(fullyQualifiedName, null, Context.create().autoImport()); }
@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 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 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 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 testObjectCreationWithNestedParameterizedTypeAndClassImports() { final Context c = Context.create() .addImport(MetaClassFactory.get(ArrayList.class)) .addImport(MetaClassFactory.get(HashMap.class)); final String s = StatementBuilder.create(c) .newObject(new TypeLiteral<ArrayList<ArrayList<HashMap<String, Integer>>>>() { }).toJavaString(); assertEquals("failed to generate new object with parameterized type", "new ArrayList<ArrayList<HashMap<String, Integer>>>()", s); }
@Override public String generate(Context context) { type = context.getVariable(name).getType(); return name; }
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; }
public static Statement generate(final Context context, final Object o) { if (o instanceof VariableReference) { return context.getVariable(((VariableReference) o).getName()); } else if (o instanceof Variable) { final Variable v = (Variable) o; if (context.isScoped(v)) { return v.getReference(); } else { if (context.isPermissiveMode()) { return v.getReference(); } else { throw new OutOfScopeException("variable cannot be referenced from this scope: " + v.getName()); } } } else if (o instanceof Statement) { ((Statement) o).generate(context); return (Statement) o; } else { return LiteralFactory.getLiteral(context, o); } }
Context ctx = Context.create(); ctx.addLiteralizableClass(Person.class);
@Test public void testAddVariableWithObjectInitializationWithStringTypeInference() { Context ctx = ContextBuilder.create().addVariable("str", ObjectBuilder.newInstanceOf(String.class)).getContext(); VariableReference str = ctx.getVariable("str"); assertEquals("Wrong variable name", "str", str.getName()); // variable type and 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()); Assert.assertEquals("Wrong variable type", MetaClassFactory.get(String.class), v.getType()); } }
protected AbstractStatementBuilder(Context context, final CallElementBuilder callElementBuilder) { if (context == null) { context = Context.create(); } this.context = context; this.callElementBuilder = callElementBuilder; }
public ClassBuilder<T> importsClass(final MetaClass clazz) { classDefinition.getContext().addImport(clazz); return this; }
/** * Creates a new and empty context. * * @return empty context */ public static Context create() { return new Context(); }
@Override public void handleCall(final CallWriter writer, final Context context, final Statement statement) { final Label label = Label.create(name); context.addLabel(label); writer.reset(); writer.append(label.generate(context)); }