protected JBlock generateWriter(AcScheme scheme, JavaWriter writer) { JBlock bl=new JBlock(); for (AbstractType tp:scheme.getSchemes().keySet()){ String name = getName(tp, writer); String ge="value.get"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"()"; JBlock th=bl._if(JExpr.direct(ge+"!=null"))._then(); th.add(new JStatement() { @Override public void state(JFormatter f) { f.p("gson.toJson("+ge+","+ge+".getClass(), out);"); f.nl(); } }); } return bl; }
protected JBlock generateWriter(AcScheme scheme, JavaWriter writer) { JBlock bl=new JBlock(); for (AbstractType tp:scheme.getSchemes().keySet()){ String name = getName(tp, writer); String ge="value.get"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"()"; JBlock th=bl._if(JExpr.direct(ge+"!=null"))._then(); th.add(new JStatement() { @Override public void state(JFormatter f) { f.p("gen.writeObject("+ge+");"); f.nl(); } }); } return bl; }
@Override public void customize(ClassCustomizerParameters parameters) { parameters.clazz._implements(Cloneable.class); parameters.clazz.method(JMod.PUBLIC, parameters.clazz, "clone").body().add(new JStatement() { @Override public void state(JFormatter f) { f.p("try {"); f.nl(); f.p("return ("+parameters.clazz.name()+")super.clone();"); f.nl(); f.p("} catch (CloneNotSupportedException e){ throw new IllegalStateException(e);}"); f.nl(); } }); }
public static JStatement split(JDefinedClass theClass, JStatement[] statements, String prefix, int start, int length, int threshold) { final JMethod method = theClass.method(JMod.PRIVATE + JMod.STATIC, theClass.owner().VOID, prefix); if (length < threshold) { for (int index = start; (index - start) < length; index++) { final JStatement statement = statements[index]; method.body().add(statement); } } else { method.body().add( split(theClass, statements, prefix + "_0", start, length / 2, threshold)); method.body().add( split(theClass, statements, prefix + "_1", start + length / 2, length - (length / 2), threshold)); } return JExpr.invoke(method); }
@Override public void caseAIfThenStatement(AIfThenStatement node) { ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); node.getCond().apply(ea); JConditional cond = parent._if(ea.expr); JBlock tmpBlock = new JBlock(); StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context); node.getThen().apply(sa); JStatement stmt = (JStatement) tmpBlock.getContents().get(0); if (stmt instanceof JBlock) { JBlock stmtBlock = (JBlock) stmt; stmtBlock.setNewlineRequired(false); } cond.setThen(stmt); }
@Override public void caseAWhileStatement(AWhileStatement node) { ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); node.getCond().apply(ea); JWhileLoop wl = parent._while(ea.expr); JBlock tmpBlock = new JBlock(); StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context); node.getDo().apply(sa); JStatement stmt = (JStatement) tmpBlock.getContents().get(0); wl.setBody(stmt); }
@Override public void caseADoStatement(ADoStatement node) { ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); node.getCond().apply(ea); JDoLoop wl = parent._do(ea.expr); JBlock tmpBlock = new JBlock(); StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context); node.getDo().apply(sa); JStatement stmt = (JStatement) tmpBlock.getContents().get(0); if (stmt instanceof JBlock) { JBlock stmtBlock = (JBlock) stmt; stmtBlock.setNewlineRequired(false); } wl.setBody(stmt); }
private void basicForStatement(PForInit init, Node cond, PForUpdate update, PStatement _do) { context.pushStatementScope(); try { JForLoop loop = parent._for(); if (init != null) { ForInitAdapter fia = new ForInitAdapter(loop, context); init.apply(fia); } if (update != null) { ForUpdateAdapter fua = new ForUpdateAdapter(loop, context); update.apply(fua); } if (cond != null) { ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); cond.apply(ea); loop.test(ea.expr); } JBlock tmpBlock = new JBlock(); StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context); _do.apply(sa); JStatement stmt = (JStatement) tmpBlock.getContents().get(0); if (stmt instanceof JBlock) { JBlock stmtBlock = (JBlock) stmt; stmtBlock.setNewlineRequired(false); } loop.setBody(stmt); } finally { context.popScope(); } }
@Override public void caseAEnhancedForStatement(AEnhancedForStatement node) { context.pushStatementScope(); try { // Formal Parameter AFormalParameter paramNode = (AFormalParameter) node.getVariable(); FormalParameter param = new FormalParameter(); FieldModifierAdapter fma = new FieldModifierAdapter(context); paramNode.apply(fma); param.mods = fma.getMods(); TypeAdapter ta = new TypeAdapter(context); paramNode.getType().apply(ta); param.type = ta.type; AVariableDeclaratorId avdid = (AVariableDeclaratorId) paramNode.getVariableDeclaratorId(); param.id = avdid.getIdentifier().getText(); param.dim = avdid.getDim().size(); // Iterable ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); node.getIterable().apply(ea); JForEach loop = parent.forEach(param.type, param.id, ea.expr); context.addField(param.id, loop.var(), Field.Type.NORMAL); // Do JBlock tmpBlock = new JBlock(); StatementAdapter sa = new StatementAdapter(new JBlockParent(tmpBlock, context), context); node.getDo().apply(sa); JStatement stmt = (JStatement) tmpBlock.getContents().get(0); // if (stmt instanceof JBlock) { // JBlock stmtBlock = (JBlock) stmt; // stmtBlock.setNewlineRequired(false); // } loop.setBody(stmt); } finally { context.popScope(); } }
protected JStatement logMethodEntry(JMethod method) { assert JavaClassGen.this.log_var != null; final String msg = format("%%s.%s()", method.name()); return this.log_var .invoke("debug").arg(msg).arg(JExpr._this()); }
private void closeClass(final JDefinedClass _class) { final GenData data = generationData.get(_class.fullName()); final JMethod construct = _class.constructor(JMod.PUBLIC); for (final JStatement expr : data.constructorList) { construct.body().add(expr); } }
public JStatement ensureArgumentNotNull(final JVar param) { ensureThatArgument(param, notNull()); return this.ensureClass .staticInvoke("requireNonNull") //$NON-NLS-1$ .arg(param); }
protected void contribute(CompositeAcElement ac,JBlock block,AbstractType target,JavaWriter writer){ JExpression expression=null; switch (ac.getFamily()) { case BOOLEAN: expression=JExpr.direct("vl.isJsonPrimitive()&&vl.getAsJsonPrimitive().isBoolean()"); break; case ARRAY: expression=JExpr.direct("vl.isJsonArray()"); break; case NUMBER: expression=JExpr.direct("vl.isJsonPrimitive()&&vl.getAsJsonPrimitive().isNumber()"); break; case STRING: expression=JExpr.direct("vl.isJsonPrimitive()&&vl.getAsJsonPrimitive().isString()"); break; case OBJECT: expression=JExpr.direct("vl.isJsonObject()"); break; default: break; } JBlock _then = block._if(expression)._then(); for (AcElement x:ac.getChildren()){ if (x.kind()==AcElementKind.PROPERTY_EXISTANCE){ _then=_then._if(JExpr.direct("vl.getAsJsonObject().has(\""+x.getProperty()+"\")"))._then(); } if (x.kind()==AcElementKind.PROPERTY_VALUE){ TestPropertyValueAcElement pv=(TestPropertyValueAcElement) x; _then=_then._if(JExpr.direct("vl.getAsJsonObject().has(\""+x.getProperty()+"\")&&vl.getAsJsonObject().get(\""+x.getProperty()+"\").getAsString()!=null&&vl.getAsJsonObject().get(\""+x.getProperty()+"\").getAsString().equals(\""+pv.getValue()+"\")"))._then(); } }; _then.add(new JStatement() { @Override public void state(JFormatter f) { String name = getName(target, writer); if (ac.getFamily()==TypeFamily.ARRAY&&writer.getConfig().containerStrategyCollection){ JType type = writer.getType(target); if (type.erasure()!=type){ String elementName=((JClass) type).getTypeParameters().get(0).fullName(); String vv="new SimpleParameterizedType("+type.erasure().fullName()+".class,"+elementName+".class)"; f.p("union.set"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"(gson.fromJson(vl, "+vv+"));"); } else{ f.p("union.set"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"(gson.fromJson(vl,"+getJavaTypeName(target, writer)+".class));"); } } else{ f.p("union.set"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"(gson.fromJson(vl,"+getJavaTypeName(target, writer)+".class));"); } f.nl(); f.p("return union;"); f.nl(); } }); }
protected void contribute(CompositeAcElement ac,JBlock block,AbstractType target,JavaWriter writer){ JExpression expression=null; switch (ac.getFamily()) { case BOOLEAN: expression=JExpr.direct("vl.isValueNode()&&vl.isBoolean()"); break; case ARRAY: expression=JExpr.direct("vl.isArray()"); break; case NUMBER: expression=JExpr.direct("vl.isValueNode()&&vl.isNumber()"); break; case STRING: expression=JExpr.direct("vl.isValueNode()&&vl.isTextual()"); break; case OBJECT: expression=JExpr.direct("vl.isObject()"); break; default: break; } JBlock _then = block._if(expression)._then(); for (AcElement x:ac.getChildren()){ if (x.kind()==AcElementKind.PROPERTY_EXISTANCE){ _then=_then._if(JExpr.direct("vl.has(\""+x.getProperty()+"\")"))._then(); } if (x.kind()==AcElementKind.PROPERTY_VALUE){ TestPropertyValueAcElement el=(TestPropertyValueAcElement) x; _then=_then._if(JExpr.direct("vl.has(\""+x.getProperty()+"\")&&vl.get(\""+x.getProperty()+"\").asText().equals(\""+el.getValue()+"\")"))._then(); } }; _then.add(new JStatement() { @Override public void state(JFormatter f) { String name = getName(target, writer); if (ac.getFamily()==TypeFamily.ARRAY&&writer.getConfig().containerStrategyCollection){ JType type = writer.getType(target); if (type.erasure()!=type){ String elementName=((JClass) type).getTypeParameters().get(0).fullName(); String vv="com.fasterxml.jackson.databind.type.CollectionType.construct("+type.erasure().fullName()+".class,com.fasterxml.jackson.databind.type.SimpleType.construct("+elementName+".class))"; f.p("union.set"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"(codec.readValue(vl.traverse(), "+vv+"));"); } } else{ f.p("union.set"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"(codec.readValue(vl.traverse(), "+getJavaTypeName(target, writer)+".class));"); } f.nl(); f.p("return union;"); f.nl(); } }); }