Search in sources :

Example 6 with JDefinedClass

use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.

the class JavaDataTemplateGenerator method generatePathSpecMethodsForUnion.

private void generatePathSpecMethodsForUnion(UnionTemplateSpec unionSpec, JDefinedClass unionClass) throws JClassAlreadyExistsException {
    final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(unionClass);
    for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
        JClass fieldsRefType = _pathSpecClass;
        if (hasNestedFields(member.getSchema())) {
            final JClass unionMemberClass = generate(member.getClassTemplateSpec());
            fieldsRefType = getCodeModel().ref(unionMemberClass.fullName() + ".Fields");
        }
        final JMethod accessorMethod = fieldsNestedClass.method(JMod.PUBLIC, fieldsRefType, CodeUtil.getUnionMemberName(member.getSchema()));
        accessorMethod.body()._return(JExpr._new(fieldsRefType).arg(JExpr.invoke("getPathComponents")).arg(member.getSchema().getUnionMemberKey()));
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) JMethod(com.sun.codemodel.JMethod)

Example 7 with JDefinedClass

use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.

the class JavaDataTemplateGenerator method defineClass.

private JDefinedClass defineClass(ClassTemplateSpec classTemplateSpec) throws JClassAlreadyExistsException {
    JDefinedClass result = _definedClasses.get(classTemplateSpec);
    if (result == null) {
        final int jmodValue = getJModValue(classTemplateSpec.getModifiers());
        final JClassContainer container;
        if (classTemplateSpec.getEnclosingClass() == null) {
            container = getPackage(classTemplateSpec.getPackage());
        } else {
            container = defineClass(classTemplateSpec.getEnclosingClass());
        }
        if (classTemplateSpec instanceof ArrayTemplateSpec || classTemplateSpec instanceof FixedTemplateSpec || classTemplateSpec instanceof MapTemplateSpec || classTemplateSpec instanceof RecordTemplateSpec || classTemplateSpec instanceof TyperefTemplateSpec || classTemplateSpec instanceof UnionTemplateSpec) {
            result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()));
        } else if (classTemplateSpec instanceof EnumTemplateSpec) {
            result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()), ClassType.ENUM);
        } else {
            throw new RuntimeException();
        }
        _definedClasses.put(classTemplateSpec, result);
    }
    return result;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) FixedTemplateSpec(com.linkedin.pegasus.generator.spec.FixedTemplateSpec) MapTemplateSpec(com.linkedin.pegasus.generator.spec.MapTemplateSpec) EnumTemplateSpec(com.linkedin.pegasus.generator.spec.EnumTemplateSpec) TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) JClassContainer(com.sun.codemodel.JClassContainer) ArrayTemplateSpec(com.linkedin.pegasus.generator.spec.ArrayTemplateSpec)

Example 8 with JDefinedClass

use of com.sun.codemodel.JDefinedClass in project rest.li by linkedin.

the class JavaDataTemplateGenerator method generatePathSpecNestedClass.

private JDefinedClass generatePathSpecNestedClass(JDefinedClass templateClass) throws JClassAlreadyExistsException {
    final JDefinedClass fieldsNestedClass = templateClass._class(JMod.PUBLIC | JMod.STATIC, "Fields");
    fieldsNestedClass._extends(_pathSpecClass);
    final JMethod constructor = fieldsNestedClass.constructor(JMod.PUBLIC);
    final JClass listString = getCodeModel().ref(List.class).narrow(String.class);
    final JVar namespace = constructor.param(listString, "path");
    final JVar name = constructor.param(String.class, "name");
    constructor.body().invoke(SUPER).arg(namespace).arg(name);
    fieldsNestedClass.constructor(JMod.PUBLIC).body().invoke(SUPER);
    return fieldsNestedClass;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) List(java.util.List) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 9 with JDefinedClass

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

the class ObjectRule method apply.

/**
     * Applies this schema rule to take the required code generation steps.
     * <p>
     * When this rule is applied for schemas of type object, the properties of
     * the schema are used to generate a new Java class and determine its
     * characteristics. See other implementers of {@link Rule} for details.
     */
@Override
public JType apply(String nodeName, JsonNode node, JPackage _package, Schema schema) {
    JType superType = getSuperType(nodeName, node, _package, schema);
    if (superType.isPrimitive() || isFinal(superType)) {
        return superType;
    }
    JDefinedClass jclass;
    try {
        jclass = createClass(nodeName, node, _package);
    } catch (ClassAlreadyExistsException e) {
        return e.getExistingClass();
    }
    jclass._extends((JClass) superType);
    schema.setJavaTypeIfEmpty(jclass);
    if (node.has("deserializationClassProperty")) {
        addJsonTypeInfoAnnotation(jclass, node);
    }
    if (node.has("title")) {
        ruleFactory.getTitleRule().apply(nodeName, node.get("title"), jclass, schema);
    }
    if (node.has("description")) {
        ruleFactory.getDescriptionRule().apply(nodeName, node.get("description"), jclass, schema);
    }
    ruleFactory.getPropertiesRule().apply(nodeName, node.get("properties"), jclass, schema);
    if (ruleFactory.getGenerationConfig().isIncludeToString()) {
        addToString(jclass);
    }
    if (node.has("javaInterfaces")) {
        addInterfaces(jclass, node.get("javaInterfaces"));
    }
    ruleFactory.getAdditionalPropertiesRule().apply(nodeName, node.get("additionalProperties"), jclass, schema);
    ruleFactory.getDynamicPropertiesRule().apply(nodeName, node.get("properties"), jclass, schema);
    if (node.has("required")) {
        ruleFactory.getRequiredArrayRule().apply(nodeName, node.get("required"), jclass, schema);
    }
    if (ruleFactory.getGenerationConfig().isIncludeHashcodeAndEquals()) {
        addHashCode(jclass);
        addEquals(jclass);
    }
    if (ruleFactory.getGenerationConfig().isParcelable()) {
        addParcelSupport(jclass);
    }
    if (ruleFactory.getGenerationConfig().isIncludeConstructors()) {
        addConstructors(jclass, node, schema, ruleFactory.getGenerationConfig().isConstructorsRequiredPropertiesOnly());
    }
    if (ruleFactory.getGenerationConfig().isSerializable()) {
        SerializableHelper.addSerializableSupport(jclass);
    }
    return jclass;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JType(com.sun.codemodel.JType) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) ClassAlreadyExistsException(org.jsonschema2pojo.exception.ClassAlreadyExistsException)

Example 10 with JDefinedClass

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

Aggregations

JDefinedClass (com.sun.codemodel.JDefinedClass)43 JClass (com.sun.codemodel.JClass)23 JMethod (com.sun.codemodel.JMethod)17 JVar (com.sun.codemodel.JVar)13 JCodeModel (com.sun.codemodel.JCodeModel)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 JType (com.sun.codemodel.JType)9 Test (org.junit.Test)9 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)8 Map (java.util.Map)8 JBlock (com.sun.codemodel.JBlock)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 JExpression (com.sun.codemodel.JExpression)5 JFieldVar (com.sun.codemodel.JFieldVar)5 HashMap (java.util.HashMap)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ArrayList (java.util.ArrayList)4 DataMap (com.linkedin.data.DataMap)3 UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3