Search in sources :

Example 31 with JClass

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

the class SerializableHelper method processDefinedClassForSerializableSupport.

private static void processDefinedClassForSerializableSupport(JDefinedClass jclass, DataOutputStream dataOutputStream) throws IOException {
    dataOutputStream.writeUTF(jclass.fullName());
    dataOutputStream.writeInt(jclass.mods().getValue());
    for (JTypeVar typeParam : jclass.typeParams()) {
        dataOutputStream.writeUTF(typeParam.fullName());
    }
    //sorted
    TreeMap<String, JDefinedClass> sortedClasses = new TreeMap<String, JDefinedClass>();
    Iterator<JDefinedClass> classes = jclass.classes();
    while (classes.hasNext()) {
        JDefinedClass nestedClass = classes.next();
        sortedClasses.put(nestedClass.fullName(), nestedClass);
    }
    for (JDefinedClass nestedClass : sortedClasses.values()) {
        processDefinedClassForSerializableSupport(nestedClass, dataOutputStream);
    }
    //sorted
    TreeSet<String> fieldNames = new TreeSet<String>(jclass.fields().keySet());
    for (String fieldName : fieldNames) {
        JFieldVar fieldVar = jclass.fields().get(fieldName);
        //non private members
        if ((fieldVar.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) {
            processFieldVarForSerializableSupport(jclass.fields().get(fieldName), dataOutputStream);
        }
    }
    Iterator<JClass> interfaces = jclass._implements();
    List<JClass> interfacesList = new ArrayList<JClass>();
    while (interfaces.hasNext()) {
        JClass aInterface = interfaces.next();
        interfacesList.add(aInterface);
    }
    Collections.sort(interfacesList, INTERFACE_COMPARATOR);
    for (JClass aInterface : interfacesList) {
        dataOutputStream.writeUTF(aInterface.fullName());
    }
    //we should probably serialize the parent class too! (but what if it has serialversionUID on it? that would be a field and would affect the serialversionUID!)
    if (jclass._extends() != null) {
        dataOutputStream.writeUTF(jclass._extends().fullName());
    }
    processMethodCollectionForSerializableSupport(jclass.methods().iterator(), dataOutputStream);
    processMethodCollectionForSerializableSupport(jclass.constructors(), dataOutputStream);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) JTypeVar(com.sun.codemodel.JTypeVar) JFieldVar(com.sun.codemodel.JFieldVar) TreeSet(java.util.TreeSet)

Example 32 with JClass

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

the class ObjectRule method addHashCode.

private void addHashCode(JDefinedClass jclass) {
    Map<String, JFieldVar> fields = jclass.fields();
    JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode");
    Class<?> hashCodeBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.HashCodeBuilder.class : org.apache.commons.lang.builder.HashCodeBuilder.class;
    JBlock body = hashCode.body();
    JClass hashCodeBuilderClass = jclass.owner().ref(hashCodeBuilder);
    JInvocation hashCodeBuilderInvocation = JExpr._new(hashCodeBuilderClass);
    if (!jclass._extends().fullName().equals(Object.class.getName())) {
        hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("hashCode"));
    }
    for (JFieldVar fieldVar : fields.values()) {
        if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
            continue;
        }
        hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("append").arg(fieldVar);
    }
    body._return(hashCodeBuilderInvocation.invoke("toHashCode"));
    hashCode.annotate(Override.class);
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod)

Example 33 with JClass

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

the class ArrayRuleTest method arrayOfPrimitivesProducesCollectionOfWrapperTypes.

@Test
public void arrayOfPrimitivesProducesCollectionOfWrapperTypes() {
    JCodeModel codeModel = new JCodeModel();
    JPackage jpackage = codeModel._package(getClass().getPackage().getName());
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode itemsNode = mapper.createObjectNode();
    itemsNode.put("type", "number");
    ObjectNode propertyNode = mapper.createObjectNode();
    propertyNode.set("uniqueItems", BooleanNode.FALSE);
    propertyNode.set("items", itemsNode);
    Schema schema = mock(Schema.class);
    when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
    when(config.isUsePrimitives()).thenReturn(true);
    when(config.isUseDoubleNumbers()).thenReturn(true);
    JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
    assertThat(propertyType, notNullValue());
    assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
    assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Double.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(org.jsonschema2pojo.Schema) JClass(com.sun.codemodel.JClass) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 34 with JClass

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

the class ArrayRuleTest method arrayDefaultsToNonUnique.

@Test
public void arrayDefaultsToNonUnique() {
    JCodeModel codeModel = new JCodeModel();
    JPackage jpackage = codeModel._package(getClass().getPackage().getName());
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode itemsNode = mapper.createObjectNode();
    itemsNode.put("type", "boolean");
    ObjectNode propertyNode = mapper.createObjectNode();
    propertyNode.set("uniqueItems", BooleanNode.FALSE);
    propertyNode.set("items", itemsNode);
    Schema schema = mock(Schema.class);
    when(schema.getId()).thenReturn(URI.create("http://example/defaultArray"));
    JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
    assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(org.jsonschema2pojo.Schema) JClass(com.sun.codemodel.JClass) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 35 with JClass

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

the class ObjectRule method addEquals.

private void addEquals(JDefinedClass jclass) {
    Map<String, JFieldVar> fields = jclass.fields();
    JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals");
    JVar otherObject = equals.param(Object.class, "other");
    Class<?> equalsBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.EqualsBuilder.class : org.apache.commons.lang.builder.EqualsBuilder.class;
    JBlock body = equals.body();
    body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE);
    body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE);
    JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject));
    JClass equalsBuilderClass = jclass.owner().ref(equalsBuilder);
    JInvocation equalsBuilderInvocation = JExpr._new(equalsBuilderClass);
    if (!jclass._extends().fullName().equals(Object.class.getName())) {
        equalsBuilderInvocation = equalsBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("equals").arg(otherObject));
    }
    for (JFieldVar fieldVar : fields.values()) {
        if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
            continue;
        }
        equalsBuilderInvocation = equalsBuilderInvocation.invoke("append").arg(fieldVar).arg(rhsVar.ref(fieldVar.name()));
    }
    JInvocation reflectionEquals = jclass.owner().ref(equalsBuilder).staticInvoke("reflectionEquals");
    reflectionEquals.arg(JExpr._this());
    reflectionEquals.arg(otherObject);
    body._return(equalsBuilderInvocation.invoke("isEquals"));
    equals.annotate(Override.class);
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Aggregations

JClass (com.sun.codemodel.JClass)53 JVar (com.sun.codemodel.JVar)25 JDefinedClass (com.sun.codemodel.JDefinedClass)22 JMethod (com.sun.codemodel.JMethod)20 JInvocation (com.sun.codemodel.JInvocation)15 JBlock (com.sun.codemodel.JBlock)13 Map (java.util.Map)9 JCodeModel (com.sun.codemodel.JCodeModel)8 JFieldVar (com.sun.codemodel.JFieldVar)8 JType (com.sun.codemodel.JType)8 HashMap (java.util.HashMap)8 JExpression (com.sun.codemodel.JExpression)7 JPackage (com.sun.codemodel.JPackage)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 Test (org.junit.Test)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 DataSchema (com.linkedin.data.schema.DataSchema)4 Schema (org.jsonschema2pojo.Schema)4