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);
}
}
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());
}
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;
}
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;
}
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;
}
Aggregations