Search in sources :

Example 1 with JBlock

use of com.sun.codemodel.JBlock in project jsonschema2pojo by joelittlejohn.

the class ObjectRule method addConstructors.

private void addConstructors(JDefinedClass jclass, JsonNode node, Schema schema, boolean onlyRequired) {
    LinkedHashSet<String> classProperties = getConstructorProperties(node, onlyRequired);
    LinkedHashSet<String> combinedSuperProperties = getSuperTypeConstructorPropertiesRecursive(node, schema, onlyRequired);
    // no properties to put in the constructor => default constructor is good enough.
    if (classProperties.isEmpty() && combinedSuperProperties.isEmpty()) {
        return;
    }
    // add a no-args constructor for serialization purposes
    JMethod noargsConstructor = jclass.constructor(JMod.PUBLIC);
    noargsConstructor.javadoc().add("No args constructor for use in serialization");
    // add the public constructor with property parameters
    JMethod fieldsConstructor = jclass.constructor(JMod.PUBLIC);
    JBlock constructorBody = fieldsConstructor.body();
    JInvocation superInvocation = constructorBody.invoke("super");
    Map<String, JFieldVar> fields = jclass.fields();
    Map<String, JVar> classFieldParams = new HashMap<String, JVar>();
    for (String property : classProperties) {
        JFieldVar field = fields.get(property);
        if (field == null) {
            throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
        }
        fieldsConstructor.javadoc().addParam(property);
        JVar param = fieldsConstructor.param(field.type(), field.name());
        constructorBody.assign(JExpr._this().ref(field), param);
        classFieldParams.put(property, param);
    }
    List<JVar> superConstructorParams = new ArrayList<JVar>();
    for (String property : combinedSuperProperties) {
        JFieldVar field = searchSuperClassesForField(property, jclass);
        if (field == null) {
            throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
        }
        JVar param = classFieldParams.get(property);
        if (param == null) {
            param = fieldsConstructor.param(field.type(), field.name());
        }
        fieldsConstructor.javadoc().addParam(property);
        superConstructorParams.add(param);
    }
    for (JVar param : superConstructorParams) {
        superInvocation.arg(param);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JInvocation(com.sun.codemodel.JInvocation) JFieldVar(com.sun.codemodel.JFieldVar) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 2 with JBlock

use of com.sun.codemodel.JBlock in project jsonschema2pojo by joelittlejohn.

the class AdditionalPropertiesRule method addBuilder.

private void addBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
    JMethod builder = jclass.method(JMod.PUBLIC, jclass, "withAdditionalProperty");
    JVar nameParam = builder.param(String.class, "name");
    JVar valueParam = builder.param(propertyType, "value");
    JBlock body = builder.body();
    JInvocation mapInvocation = body.invoke(JExpr._this().ref(field), "put");
    mapInvocation.arg(nameParam);
    mapInvocation.arg(valueParam);
    body._return(JExpr._this());
}
Also used : JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 3 with JBlock

use of com.sun.codemodel.JBlock in project jsonschema2pojo by joelittlejohn.

the class PropertyRule method addBuilder.

private JMethod addBuilder(JDefinedClass c, JFieldVar field) {
    JMethod builder = c.method(JMod.PUBLIC, c, getBuilderName(field.name()));
    JVar param = builder.param(field.type(), field.name());
    JBlock body = builder.body();
    body.assign(JExpr._this().ref(field), param);
    body._return(JExpr._this());
    return builder;
}
Also used : JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 4 with JBlock

use of com.sun.codemodel.JBlock in project drill by apache.

the class DrillAggFuncHolder method renderEnd.

@Override
public HoldingContainer renderEnd(ClassGenerator<?> classGenerator, HoldingContainer[] inputVariables, JVar[] workspaceJVars, FieldReference fieldReference) {
    HoldingContainer out = classGenerator.declare(getReturnType(), false);
    JBlock sub = new JBlock();
    classGenerator.getEvalBlock().add(sub);
    JVar internalOutput = sub.decl(JMod.FINAL, classGenerator.getHolderType(getReturnType()), getReturnValue().getName(), JExpr._new(classGenerator.getHolderType(getReturnType())));
    addProtectedBlock(classGenerator, sub, output(), null, workspaceJVars, false);
    sub.assign(out.getHolder(), internalOutput);
    //hash aggregate uses workspace vectors. Initialization is done in "setup" and does not require "reset" block.
    if (!classGenerator.getMappingSet().isHashAggMapping()) {
        generateBody(classGenerator, BlockType.RESET, reset(), null, workspaceJVars, false);
    }
    generateBody(classGenerator, BlockType.CLEANUP, cleanup(), null, workspaceJVars, false);
    return out;
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JBlock(com.sun.codemodel.JBlock) JVar(com.sun.codemodel.JVar)

Example 5 with JBlock

use of com.sun.codemodel.JBlock in project drill by apache.

the class DrillComplexWriterFuncHolder method generateEvalBody.

@Override
protected HoldingContainer generateEvalBody(ClassGenerator<?> classGenerator, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars, FieldReference fieldReference) {
    classGenerator.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", getRegisteredNames()[0]));
    JBlock sub = new JBlock(true, true);
    JBlock topSub = sub;
    JVar complexWriter = classGenerator.declareClassField("complexWriter", classGenerator.getModel()._ref(ComplexWriter.class));
    JInvocation container = classGenerator.getMappingSet().getOutgoing().invoke("getOutgoingContainer");
    //Default name is "col", if not passed in a reference name for the output vector.
    String refName = fieldReference == null ? "col" : fieldReference.getRootSegment().getPath();
    JClass cwClass = classGenerator.getModel().ref(VectorAccessibleComplexWriter.class);
    classGenerator.getSetupBlock().assign(complexWriter, cwClass.staticInvoke("getWriter").arg(refName).arg(container));
    JClass projBatchClass = classGenerator.getModel().ref(ProjectRecordBatch.class);
    JExpression projBatch = JExpr.cast(projBatchClass, classGenerator.getMappingSet().getOutgoing());
    classGenerator.getSetupBlock().add(projBatch.invoke("addComplexWriter").arg(complexWriter));
    classGenerator.getEvalBlock().add(complexWriter.invoke("setPosition").arg(classGenerator.getMappingSet().getValueWriteIndex()));
    sub.decl(classGenerator.getModel()._ref(ComplexWriter.class), getReturnValue().getName(), complexWriter);
    // add the subblock after the out declaration.
    classGenerator.getEvalBlock().add(topSub);
    addProtectedBlock(classGenerator, sub, body, inputVariables, workspaceJVars, false);
    //    JConditional jc = classGenerator.getEvalBlock()._if(complexWriter.invoke("ok").not());
    //    jc._then().add(complexWriter.invoke("reset"));
    //jc._then().directStatement("System.out.println(\"debug : write ok fail!, inIndex = \" + inIndex);");
    //    jc._then()._return(JExpr.FALSE);
    //jc._else().directStatement("System.out.println(\"debug : write successful, inIndex = \" + inIndex);");
    classGenerator.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", getRegisteredNames()[0]));
    return null;
}
Also used : ComplexWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ComplexWriter) VectorAccessibleComplexWriter(org.apache.drill.exec.record.VectorAccessibleComplexWriter) JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JExpression(com.sun.codemodel.JExpression) JVar(com.sun.codemodel.JVar)

Aggregations

JBlock (com.sun.codemodel.JBlock)88 JVar (com.sun.codemodel.JVar)61 JMethod (com.sun.codemodel.JMethod)49 JClass (com.sun.codemodel.JClass)30 JInvocation (com.sun.codemodel.JInvocation)27 JExpression (com.sun.codemodel.JExpression)21 JFieldVar (com.sun.codemodel.JFieldVar)21 JConditional (com.sun.codemodel.JConditional)19 JType (com.sun.codemodel.JType)17 JDefinedClass (com.sun.codemodel.JDefinedClass)11 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)9 Map (java.util.Map)8 JCatchBlock (com.sun.codemodel.JCatchBlock)7 JTryBlock (com.sun.codemodel.JTryBlock)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 JFieldRef (com.sun.codemodel.JFieldRef)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 JForLoop (com.sun.codemodel.JForLoop)4 DataList (com.linkedin.data.DataList)3