Search in sources :

Example 1 with JFieldVar

use of com.sun.codemodel.JFieldVar 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 2 with JFieldVar

use of com.sun.codemodel.JFieldVar in project Activiti by Activiti.

the class CxfWSDLImporter method _importFields.

protected static void _importFields(final JDefinedClass theClass, final AtomicInteger index, final SimpleStructureDefinition structure) {
    final JClass parentClass = theClass._extends();
    if (parentClass != null && parentClass instanceof JDefinedClass) {
        _importFields((JDefinedClass) parentClass, index, structure);
    }
    for (Entry<String, JFieldVar> entry : theClass.fields().entrySet()) {
        Class<?> fieldClass = ReflectUtil.loadClass(entry.getValue().type().boxify().erasure().fullName());
        String fieldName = entry.getKey();
        if (fieldName.startsWith("_")) {
            if (!JJavaName.isJavaIdentifier(fieldName.substring(1))) {
                //it was prefixed with '_' so we should use the original name.
                fieldName = fieldName.substring(1);
            }
        }
        structure.setFieldName(index.getAndIncrement(), fieldName, fieldClass);
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass)

Example 3 with JFieldVar

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

the class AdditionalPropertiesRule method addInnerBuilder.

private JMethod addInnerBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
    Optional<JDefinedClass> builderClass = StreamSupport.stream(Spliterators.spliteratorUnknownSize(jclass.classes(), Spliterator.ORDERED), false).filter(definedClass -> definedClass.name().equals(getBuilderClassName(jclass))).findFirst();
    JMethod builder = builderClass.get().method(JMod.PUBLIC, builderClass.get(), "withAdditionalProperty");
    JVar nameParam = builder.param(String.class, "name");
    JVar valueParam = builder.param(propertyType, "value");
    JBlock body = builder.body();
    JInvocation mapInvocation = body.invoke(JExpr.ref(JExpr.cast(jclass, JExpr._this().ref("instance")), field), "put");
    mapInvocation.arg(nameParam);
    mapInvocation.arg(valueParam);
    body._return(JExpr._this());
    return builder;
}
Also used : JBlock(com.sun.codemodel.JBlock) JMod(com.sun.codemodel.JMod) JDefinedClass(com.sun.codemodel.JDefinedClass) JVar(com.sun.codemodel.JVar) Spliterators(java.util.Spliterators) JMethod(com.sun.codemodel.JMethod) HashMap(java.util.HashMap) JInvocation(com.sun.codemodel.JInvocation) JExpr(com.sun.codemodel.JExpr) JFieldVar(com.sun.codemodel.JFieldVar) JType(com.sun.codemodel.JType) Map(java.util.Map) Optional(java.util.Optional) Schema(org.jsonschema2pojo.Schema) StreamSupport(java.util.stream.StreamSupport) JsonNode(com.fasterxml.jackson.databind.JsonNode) JClass(com.sun.codemodel.JClass) Spliterator(java.util.Spliterator) JDefinedClass(com.sun.codemodel.JDefinedClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 4 with JFieldVar

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

the class BuilderRule method generateNoArgsBaseBuilderConstructor.

private void generateNoArgsBaseBuilderConstructor(JDefinedClass instanceClass, JDefinedClass builderClass, JDefinedClass concreteBuilderClass) {
    JMethod noArgsConstructor = builderClass.constructor(JMod.PUBLIC);
    JAnnotationUse warningSuppression = noArgsConstructor.annotate(SuppressWarnings.class);
    warningSuppression.param("value", "unchecked");
    JBlock constructorBlock = noArgsConstructor.body();
    JFieldVar instanceField = reflectionHelper.searchClassAndSuperClassesForField("instance", builderClass);
    // Determine if we need to invoke the super() method for our parent builder
    JClass parentClass = builderClass._extends();
    if (!(parentClass.isPrimitive() || reflectionHelper.isFinal(parentClass) || Objects.equals(parentClass.fullName(), "java.lang.Object"))) {
        constructorBlock.invoke("super");
    }
    // Only initialize the instance if the object being constructed is actually this class
    // if it's a subtype then ignore the instance initialization since the subclass will initialize it
    constructorBlock.directStatement("// Skip initialization when called from subclass");
    JInvocation comparison = JExpr._this().invoke("getClass").invoke("equals").arg(JExpr.dotclass(concreteBuilderClass));
    JConditional ifNotSubclass = constructorBlock._if(comparison);
    ifNotSubclass._then().assign(JExpr._this().ref(instanceField), JExpr.cast(instanceField.type(), JExpr._new(instanceClass)));
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod)

Example 5 with JFieldVar

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

the class ConstructorRule method generateCopyConstructor.

private JMethod generateCopyConstructor(JDefinedClass jclass, Set<String> classProperties, Set<String> combinedSuperProperties) {
    // Create the JMethod for the copy constructor
    JMethod copyConstructorResult = jclass.constructor(JMod.PUBLIC);
    // Add a parameter to the copyConstructor for the actual object being copied
    copyConstructorResult.javadoc().addParam("source");
    JVar copyConstructorParam = copyConstructorResult.param(jclass, "source");
    // Create the method block for the copyConstructor
    JBlock constructorBody = copyConstructorResult.body();
    // Invoke the super class constructor for this class. We'll include the original object
    // being copied if there are any combinedSuperProperties to be set, and if not then we'll
    // simply call the empty constructor from the super class
    JInvocation superInvocation = constructorBody.invoke("super");
    if (!combinedSuperProperties.isEmpty()) {
        superInvocation.arg(copyConstructorParam);
    }
    // For each of the class properties being set we then need to do something like the following
    // this.property = source.property
    Map<String, JFieldVar> fields = jclass.fields();
    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");
        }
        constructorBody.assign(JExpr._this().ref(field), copyConstructorParam.ref(field));
    }
    return copyConstructorResult;
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Aggregations

JFieldVar (com.sun.codemodel.JFieldVar)74 JMethod (com.sun.codemodel.JMethod)33 JVar (com.sun.codemodel.JVar)28 JClass (com.sun.codemodel.JClass)24 Test (org.junit.Test)24 JBlock (com.sun.codemodel.JBlock)21 JInvocation (com.sun.codemodel.JInvocation)19 JDefinedClass (com.sun.codemodel.JDefinedClass)16 Random (java.util.Random)12 ByteString (com.linkedin.data.ByteString)10 HashMap (java.util.HashMap)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 JExpression (com.sun.codemodel.JExpression)9 JConditional (com.sun.codemodel.JConditional)8 JType (com.sun.codemodel.JType)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)6 JFieldRef (com.sun.codemodel.JFieldRef)5 JCodeModel (com.sun.codemodel.JCodeModel)3 JPackage (com.sun.codemodel.JPackage)3