Search in sources :

Example 1 with JAnnotationArrayMember

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

the class ConstructorRule method generateFieldsConstructor.

private JMethod generateFieldsConstructor(JDefinedClass jclass, Set<String> classProperties, Set<String> combinedSuperProperties) {
    // add the public constructor with property parameters
    JMethod fieldsConstructor = jclass.constructor(JMod.PUBLIC);
    GenerationConfig generationConfig = ruleFactory.getGenerationConfig();
    JAnnotationArrayMember constructorPropertiesAnnotation;
    if (generationConfig.isIncludeConstructorPropertiesAnnotation()) {
        constructorPropertiesAnnotation = fieldsConstructor.annotate(ConstructorProperties.class).paramArray("value");
    } else {
        constructorPropertiesAnnotation = null;
    }
    JBlock constructorBody = fieldsConstructor.body();
    JInvocation superInvocation = constructorBody.invoke("super");
    Map<String, JFieldVar> fields = jclass.fields();
    Map<String, JVar> classFieldParams = new HashMap<>();
    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);
        if (generationConfig.isIncludeConstructorPropertiesAnnotation() && constructorPropertiesAnnotation != null) {
            constructorPropertiesAnnotation.param(field.name());
        }
        JVar param = fieldsConstructor.param(field.type(), field.name());
        constructorBody.assign(JExpr._this().ref(field), param);
        classFieldParams.put(property, param);
    }
    List<JVar> superConstructorParams = new ArrayList<>();
    for (String property : combinedSuperProperties) {
        JFieldVar field = reflectionHelper.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);
        if (generationConfig.isIncludeConstructorPropertiesAnnotation() && constructorPropertiesAnnotation != null) {
            constructorPropertiesAnnotation.param(param.name());
        }
        superConstructorParams.add(param);
    }
    for (JVar param : superConstructorParams) {
        superInvocation.arg(param);
    }
    return fieldsConstructor;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAnnotationArrayMember(com.sun.codemodel.JAnnotationArrayMember) JInvocation(com.sun.codemodel.JInvocation) GenerationConfig(org.jsonschema2pojo.GenerationConfig) JFieldVar(com.sun.codemodel.JFieldVar) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 2 with JAnnotationArrayMember

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

the class Models method suppressWarnings.

public static void suppressWarnings(JMethod method, String... values) {
    JAnnotationUse annotation = method.annotate(SuppressWarnings.class);
    JAnnotationArrayMember member = annotation.paramArray("value");
    for (String value : values) {
        member.param(value);
    }
}
Also used : JAnnotationUse(com.sun.codemodel.JAnnotationUse) JAnnotationArrayMember(com.sun.codemodel.JAnnotationArrayMember)

Aggregations

JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)2 JAnnotationUse (com.sun.codemodel.JAnnotationUse)1 JBlock (com.sun.codemodel.JBlock)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JInvocation (com.sun.codemodel.JInvocation)1 JMethod (com.sun.codemodel.JMethod)1 JVar (com.sun.codemodel.JVar)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GenerationConfig (org.jsonschema2pojo.GenerationConfig)1