Search in sources :

Example 1 with JMethod

use of com.sun.codemodel.JMethod 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 JMethod

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

the class ObjectRule method addToString.

private void addToString(JDefinedClass jclass) {
    JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
    Class<?> toStringBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.ToStringBuilder.class : org.apache.commons.lang.builder.ToStringBuilder.class;
    JBlock body = toString.body();
    JInvocation reflectionToString = jclass.owner().ref(toStringBuilder).staticInvoke("reflectionToString");
    reflectionToString.arg(JExpr._this());
    body._return(reflectionToString);
    toString.annotate(Override.class);
}
Also used : JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod)

Example 3 with JMethod

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

use of com.sun.codemodel.JMethod 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 5 with JMethod

use of com.sun.codemodel.JMethod 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)

Aggregations

JMethod (com.sun.codemodel.JMethod)62 JVar (com.sun.codemodel.JVar)40 JBlock (com.sun.codemodel.JBlock)26 JClass (com.sun.codemodel.JClass)21 JDefinedClass (com.sun.codemodel.JDefinedClass)17 JInvocation (com.sun.codemodel.JInvocation)15 JFieldVar (com.sun.codemodel.JFieldVar)11 JType (com.sun.codemodel.JType)10 JExpression (com.sun.codemodel.JExpression)9 Map (java.util.Map)7 JConditional (com.sun.codemodel.JConditional)6 HashMap (java.util.HashMap)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 ByteString (com.linkedin.data.ByteString)2 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2 DataSchema (com.linkedin.data.schema.DataSchema)2 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2