private Statement generateRequest(ClassStructureBuilder<?> classBuilder, MetaMethod method, Statement methodParams, boolean intercepted) { final Statement sendable = Stmt .invokeStatic(MessageBuilder.class, "createCall") .invoke("call", remote.getFullyQualifiedName()) .invoke("endpoint", ProxyUtil.createCallSignature(method), Stmt.loadClassMember("qualifiers"), methodParams) .invoke("respondTo", method.getReturnType().asBoxed(), Stmt.loadVariable("remoteCallback")) .invoke("errorsHandledBy", Stmt.loadVariable("errorCallback")); final BlockStatement requestBlock = new BlockStatement(); requestBlock.addStatement(Stmt.declareVariable("sendable", RemoteCallSendable.class, sendable)); requestBlock.addStatement(Stmt.loadStatic(classBuilder.getClassDefinition(), "this") .invoke("sendRequest", Variable.get("bus"), Variable.get("sendable"))); return requestBlock; } }
public ContextBuilder addVariable(String name, Class<?> type) { context.addVariable(Variable.create(name, type)); return this; }
@Override public String toJavaString() { final Collection<Variable> vars = context.getDeclaredVariables(); final StringBuilder buf = new StringBuilder(128); vars.forEach(var -> buf.append(var.generate(context)).append(";\n")); return buf.toString(); }
@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; } };
@Override public String generate(Context context) { if (generatedCache != null) return generatedCache; return generatedCache = ((nullSafe) ? "if (" + collectionExpr + " != null) {\n" : "") + "for (" + getClassReference(loopVar.getType(), context) + " " + loopVar.getName() + " : " + collectionExpr + ") {" + "\n\t" + body.generate(context).replaceAll("\n", "\n\t") + "\n}" + ((nullSafe) ? "\n}" : ""); } }
@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()); } }
/** * Creates a variable, but does not assign it to a scope. If you are trying to * declare a variable, see {@link Stmt#declareVariable(String, MetaClass)}. * * @param name * The variable name * @param type * The variable reference type * @return A newly created variable that is not (yet) referencable. */ public static Variable create(final String name, final MetaClass type) { return new Variable(name, type); }
@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()); }
@Override public String generate(Context context) { if (generatedCache != null) return generatedCache; return generatedCache = ((nullSafe) ? "if (" + collectionExpr + " != null) {\n" : "") + "for (" + getClassReference(loopVar.getType(), context) + " " + loopVar.getName() + " : " + collectionExpr + ") {" + "\n\t" + body.generate(context).replaceAll("\n", "\n\t") + "\n}" + ((nullSafe) ? "\n}" : ""); } }
@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; } };
/** * Creates a variable, but does not assign it to a scope. If you are trying to * declare a variable, see {@link Stmt#declareVariable(String, Object)}. * * @param name * The variable name * @param type * The variable reference type * @return A newly created variable that is not (yet) referencable. */ public static Variable create(final String name, final Object initialization) { return new Variable(name, null, initialization); }
private Statement generateInterceptorLogic(ClassStructureBuilder<?> classBuilder, MetaMethod method, Statement requestLogic, List<Statement> parmVars, List<Class<?>> interceptors) { final Statement callContext = ProxyUtil.generateProxyMethodCallContext(context, RemoteCallContext.class, classBuilder.getClassDefinition(), method, requestLogic, interceptors).finish(); return Stmt.try_() .append( Stmt.declareVariable(CallContextStatus.class).asFinal().named("status").initializeWith( Stmt.newObject(CallContextStatus.class).withParameters(interceptors.toArray()))) .append( Stmt.declareVariable(RemoteCallContext.class).asFinal().named("callContext") .initializeWith(callContext)) .append( Stmt.loadVariable("callContext").invoke("setParameters", Stmt.newArray(Object.class).initialize(parmVars.toArray()))) .append( Stmt.loadVariable("callContext").invoke("proceed")) .finish() .catch_(Throwable.class, "throwable") .append( If.cond(Bool.notEquals(Stmt.loadVariable("errorCallback"), Stmt.loadLiteral(null))) .append( If.cond(Stmt.loadVariable("errorCallback").invoke("error", Stmt.load(null), Variable.get("throwable"))) .append(Stmt.loadVariable("this").invoke("invokeDefaultErrorHandlers", Variable.get("throwable"))) .finish() ).finish() .else_() .append(Stmt.loadVariable("this").invoke("invokeDefaultErrorHandlers", Variable.get("throwable"))) .finish()) .finish(); }
@Override public StatementBuilder declareVariable(final String name, final Class<?> type) { return declareVariable(Variable.create(name, type)); }
@Override public String toJavaString() { final Collection<Variable> vars = context.getDeclaredVariables(); final StringBuilder buf = new StringBuilder(128); vars.forEach(var -> buf.append(var.generate(context)).append(";\n")); return buf.toString(); }
/** * Creates a variable, but does not assign it to a scope. If you are trying to * declare a variable, see {@link Stmt#declareVariable(String, Object)}. * * @param name * The variable name * @param type * The variable reference type * @return A newly created variable that is not (yet) referencable. */ public static Variable create(final String name, final Object initialization) { return new Variable(name, null, initialization); }
public static Statement marshal(final Statement statement, final String contentType) { if (statement instanceof Parameter) { final Parameter param = (Parameter) statement; return marshal(param.getType(), Variable.get(param.getName()), contentType); } return marshal(statement.getType(), statement, contentType); }
@Override public StatementBuilder declareVariable(final String name, final TypeLiteral<?> type) { return declareVariable(Variable.create(name, type)); }
@Override public String generate(Context context) { if (generatedCache != null) return generatedCache; final StringBuilder buf = new StringBuilder("try {\n"); buf.append(block.generate(context)).append("\n} "); if (!catchBlocks.isEmpty()) { for (final Variable exception : catchBlocks.keySet()) { final Context ctx = Context.create(context).addVariable(exception); buf.append("catch (").append(exception.generate(ctx)).append(") ") .append("{\n") .append(catchBlocks.get(exception).generate(ctx)) .append("\n} "); } } else if (finallyBlock == null) { finallyBlock = new BlockStatement(); } if (finallyBlock != null) { final Context ctx = Context.create(context); buf.append(" finally {\n").append(finallyBlock.generate(ctx)).append("\n}\n"); } return generatedCache = buf.toString(); } }
/** * Creates a variable, but does not assign it to a scope. If you are trying to * declare a variable, see {@link Stmt#declareVariable(String, MetaClass, Object)}. * * @param name * The variable name * @param type * The variable reference type * @return A newly created variable that is not (yet) referencable. */ public static Variable create(final String name, final MetaClass type, final Object initialization) { return new Variable(name, type, initialization); }