Search in sources :

Example 1 with JClass

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

the class TypeUtil method buildClass.

private static JClass buildClass(JClassContainer _package, ClassOrInterfaceType c, int arrayCount) {
    final String packagePrefix = (c.getScope() != null) ? c.getScope().toString() + "." : "";
    JClass _class;
    try {
        _class = _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(packagePrefix + c.getName()));
    } catch (ClassNotFoundException e) {
        _class = _package.owner().ref(packagePrefix + c.getName());
    }
    for (int i = 0; i < arrayCount; i++) {
        _class = _class.array();
    }
    List<Type> typeArgs = c.getTypeArgs();
    if (typeArgs != null && typeArgs.size() > 0) {
        JClass[] genericArgumentClasses = new JClass[typeArgs.size()];
        for (int i = 0; i < typeArgs.size(); i++) {
            genericArgumentClasses[i] = buildClass(_package, (ClassOrInterfaceType) ((ReferenceType) typeArgs.get(i)).getType(), ((ReferenceType) typeArgs.get(i)).getArrayCount());
        }
        _class = _class.narrow(genericArgumentClasses);
    }
    return _class;
}
Also used : ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) Type(japa.parser.ast.type.Type) ReferenceType(japa.parser.ast.type.ReferenceType) JClass(com.sun.codemodel.JClass) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) ReferenceType(japa.parser.ast.type.ReferenceType)

Example 2 with JClass

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

the class ArrayRuleTest method arrayWithNonUniqueItemsProducesList.

@Test
public void arrayWithNonUniqueItemsProducesList() {
    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.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 3 with JClass

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

the class ArrayRuleTest method arrayWithUniqueItemsProducesSet.

@Test
public void arrayWithUniqueItemsProducesSet() {
    JCodeModel codeModel = new JCodeModel();
    JPackage jpackage = codeModel._package(getClass().getPackage().getName());
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode itemsNode = mapper.createObjectNode();
    itemsNode.put("type", "integer");
    ObjectNode propertyNode = mapper.createObjectNode();
    propertyNode.set("uniqueItems", BooleanNode.TRUE);
    propertyNode.set("items", itemsNode);
    JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, mock(Schema.class));
    assertThat(propertyType, notNullValue());
    assertThat(propertyType.erasure(), is(codeModel.ref(Set.class)));
    assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Integer.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JClass(com.sun.codemodel.JClass) Schema(org.jsonschema2pojo.Schema) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with JClass

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

the class ObjectRule method createClass.

/**
     * Creates a new Java class that will be generated.
     *
     * @param nodeName
     *            the node name which may be used to dictate the new class name
     * @param node
     *            the node representing the schema that caused the need for a
     *            new class. This node may include a 'javaType' property which
     *            if present will override the fully qualified name of the newly
     *            generated class.
     * @param _package
     *            the package which may contain a new class after this method
     *            call
     * @return a reference to a newly created class
     * @throws ClassAlreadyExistsException
     *             if the given arguments cause an attempt to create a class
     *             that already exists, either on the classpath or in the
     *             current map of classes to be generated.
     */
private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _package) throws ClassAlreadyExistsException {
    JDefinedClass newType;
    try {
        boolean usePolymorphicDeserialization = usesPolymorphicDeserialization(node);
        if (node.has("javaType")) {
            String fqn = substringBefore(node.get("javaType").asText(), "<");
            if (isPrimitive(fqn, _package.owner())) {
                throw new ClassAlreadyExistsException(primitiveType(fqn, _package.owner()));
            }
            JClass existingClass;
            try {
                _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn));
                existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : ""));
                throw new ClassAlreadyExistsException(existingClass);
            } catch (ClassNotFoundException e) {
            }
            int index = fqn.lastIndexOf(".") + 1;
            if (index >= 0 && index < fqn.length()) {
                fqn = fqn.substring(0, index) + ruleFactory.getGenerationConfig().getClassNamePrefix() + fqn.substring(index) + ruleFactory.getGenerationConfig().getClassNameSuffix();
            }
            try {
                _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn));
                existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : ""));
                throw new ClassAlreadyExistsException(existingClass);
            } catch (ClassNotFoundException e) {
            }
            if (usePolymorphicDeserialization) {
                newType = _package.owner()._class(JMod.PUBLIC, fqn, ClassType.CLASS);
            } else {
                newType = _package.owner()._class(fqn);
            }
        } else {
            if (usePolymorphicDeserialization) {
                newType = _package._class(JMod.PUBLIC, getClassName(nodeName, node, _package), ClassType.CLASS);
            } else {
                newType = _package._class(getClassName(nodeName, node, _package));
            }
        }
    } catch (JClassAlreadyExistsException e) {
        throw new ClassAlreadyExistsException(e.getExistingClass());
    }
    ruleFactory.getAnnotator().propertyInclusion(newType, node);
    return newType;
}
Also used : JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) ClassAlreadyExistsException(org.jsonschema2pojo.exception.ClassAlreadyExistsException)

Example 5 with JClass

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

the class ObjectRule method searchSuperClassesForField.

/**
     * This is recursive with searchClassAndSuperClassesForField
     */
private JFieldVar searchSuperClassesForField(String property, JDefinedClass jclass) {
    JClass superClass = jclass._extends();
    JDefinedClass definedSuperClass = definedClassOrNullFromType(superClass);
    if (definedSuperClass == null) {
        return null;
    }
    return searchClassAndSuperClassesForField(property, definedSuperClass);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass)

Aggregations

JClass (com.sun.codemodel.JClass)52 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 JFieldVar (com.sun.codemodel.JFieldVar)8 JType (com.sun.codemodel.JType)8 HashMap (java.util.HashMap)8 JCodeModel (com.sun.codemodel.JCodeModel)7 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