Search in sources :

Example 1 with JVar

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

the class SerializableHelper method processMethodCollectionForSerializableSupport.

private static void processMethodCollectionForSerializableSupport(Iterator<JMethod> methods, DataOutputStream dataOutputStream) throws IOException {
    TreeMap<String, JMethod> sortedMethods = new TreeMap<String, JMethod>();
    while (methods.hasNext()) {
        JMethod method = methods.next();
        //Collect non-private methods
        if ((method.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) {
            sortedMethods.put(method.name(), method);
        }
    }
    for (JMethod method : sortedMethods.values()) {
        dataOutputStream.writeUTF(method.name());
        dataOutputStream.writeInt(method.mods().getValue());
        if (method.type() != null) {
            dataOutputStream.writeUTF(method.type().fullName());
        }
        for (JVar param : method.params()) {
            dataOutputStream.writeUTF(param.type().fullName());
        }
    }
}
Also used : TreeMap(java.util.TreeMap) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 2 with JVar

use of com.sun.codemodel.JVar 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 3 with JVar

use of com.sun.codemodel.JVar 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 4 with JVar

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

the class DynamicPropertiesRule method addPublicSetMethod.

private JMethod addPublicSetMethod(JDefinedClass jclass, JMethod internalSetMethod) {
    JMethod method = jclass.method(PUBLIC, jclass.owner().VOID, SETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    JBlock body = method.body();
    JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
    // if we have additional properties, then put value.
    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
        notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam).arg(cast(additionalPropertiesType, valueParam)));
    } else // else throw exception.
    {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }
    return method;
}
Also used : JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 5 with JVar

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

the class DynamicPropertiesRule method addPublicGetMethod.

private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
    JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
    JTypeVar returnType = method.generify("T");
    method.type(returnType);
    Models.suppressWarnings(method, "unchecked");
    JVar nameParam = method.param(String.class, "name");
    JBlock body = method.body();
    JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value", invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
    JConditional found = method.body()._if(notFoundValue.ne(valueVar));
    found._then()._return(cast(returnType, valueVar));
    JBlock notFound = found._else();
    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
    } else {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }
    return method;
}
Also used : JTypeVar(com.sun.codemodel.JTypeVar) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Aggregations

JVar (com.sun.codemodel.JVar)62 JMethod (com.sun.codemodel.JMethod)40 JBlock (com.sun.codemodel.JBlock)28 JClass (com.sun.codemodel.JClass)25 JInvocation (com.sun.codemodel.JInvocation)17 JDefinedClass (com.sun.codemodel.JDefinedClass)13 JExpression (com.sun.codemodel.JExpression)13 JType (com.sun.codemodel.JType)13 JFieldVar (com.sun.codemodel.JFieldVar)9 JConditional (com.sun.codemodel.JConditional)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ArrayList (java.util.ArrayList)5 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)5 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 DataSchema (com.linkedin.data.schema.DataSchema)4 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)4 MapDataSchema (com.linkedin.data.schema.MapDataSchema)4