Java Code Examples for com.sun.codemodel.JExpr#_null

The following examples show how to use com.sun.codemodel.JExpr#_null . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImplementationBuilder.java    From aem-component-generator with Apache License 2.0 6 votes vote down vote up
/**
 * add getter method for jFieldVar passed in.
 *
 * @param jc
 * @param jFieldVar
 */
private void addGetter(JDefinedClass jc, JFieldVar jFieldVar) {
    JMethod getMethod = jc.method(JMod.PUBLIC, jFieldVar.type(), getMethodFormattedString(jFieldVar.name()));
    getMethod.annotate(codeModel.ref(Override.class));

    if (this.isAllowExporting) {
        if (!this.fieldJsonExposeMap.get(jFieldVar.name())) {
            getMethod.annotate(codeModel.ref(JsonIgnore.class));
        }

        if (StringUtils.isNotBlank(this.fieldJsonPropertyMap.get(jFieldVar.name()))) {
            getMethod.annotate(codeModel.ref(JsonProperty.class))
                    .param("value", this.fieldJsonPropertyMap.get(jFieldVar.name()));
        }
    }


    if (jFieldVar.type().erasure().fullName().equals(List.class.getName())) {
        JExpression condition = new IsNullExpression(jFieldVar, false);
        JExpression ifTrue = codeModel.ref(Collections.class).staticInvoke("unmodifiableList").arg(jFieldVar);
        JExpression ifFalse = JExpr._null();
        getMethod.body()._return(new TernaryOperator(condition, ifTrue, ifFalse));
    } else {
        getMethod.body()._return(jFieldVar);
    }
}
 
Example 2
Source File: PluginImpl.java    From immutable-xjc with MIT License 6 votes vote down vote up
private JExpression getDefensiveCopyExpression(JCodeModel codeModel, JType jType, JVar param) {
    List<JClass> typeParams = ((JClass) jType).getTypeParameters();
    JClass typeParameter = null;
    if (typeParams.iterator().hasNext()) {
        typeParameter = typeParams.iterator().next();
    }

    JClass newClass = null;
    if (param.type().erasure().equals(codeModel.ref(Collection.class))) {
        newClass = codeModel.ref(ArrayList.class);
    } else if (param.type().erasure().equals(codeModel.ref(List.class))) {
        newClass = codeModel.ref(ArrayList.class);
    } else if (param.type().erasure().equals(codeModel.ref(Map.class))) {
        newClass = codeModel.ref(HashMap.class);
    } else if (param.type().erasure().equals(codeModel.ref(Set.class))) {
        newClass = codeModel.ref(HashSet.class);
    } else if (param.type().erasure().equals(codeModel.ref(SortedMap.class))) {
        newClass = codeModel.ref(TreeMap.class);
    } else if (param.type().erasure().equals(codeModel.ref(SortedSet.class))) {
        newClass = codeModel.ref(TreeSet.class);
    }
    if (newClass != null && typeParameter != null) {
        newClass = newClass.narrow(typeParameter);
    }
    return newClass == null ? JExpr._null() : JExpr._new(newClass).arg(param);
}
 
Example 3
Source File: PluginImpl.java    From immutable-xjc with MIT License 6 votes vote down vote up
private JExpression defaultValue(JFieldVar field) {
    JType javaType = field.type();
    if (setDefaultValuesInConstructor) {
        Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName());
        if (xmlElementAnnotation.isPresent()) {
            JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("defaultValue");
            if (annotationValue != null) {
                StringWriter sw = new StringWriter();
                JFormatter f = new JFormatter(sw);
                annotationValue.generate(f);
                return JExpr.lit(sw.toString().replaceAll("\"", ""));
            }
        }
    }
    if (javaType.isPrimitive()) {
        if (field.type().owner().BOOLEAN.equals(javaType)) {
            return JExpr.lit(false);
        } else if (javaType.owner().SHORT.equals(javaType)) {
            return JExpr.cast(javaType.owner().SHORT, JExpr.lit(0));
        } else {
            return JExpr.lit(0);
        }
    }
    return JExpr._null();
}
 
Example 4
Source File: PluginImpl.java    From immutable-xjc with MIT License 5 votes vote down vote up
private JExpression getNewCollectionExpression(JCodeModel codeModel, JType jType) {
    List<JClass> typeParams = ((JClass) jType).getTypeParameters();
    JClass typeParameter = null;
    if (typeParams.iterator().hasNext()) {
        typeParameter = typeParams.iterator().next();
    }

    JClass newClass = null;
    if (jType.erasure().equals(codeModel.ref(Collection.class))) {
        newClass = codeModel.ref(ArrayList.class);
    } else if (jType.erasure().equals(codeModel.ref(List.class))) {
        newClass = codeModel.ref(ArrayList.class);
    } else if (jType.erasure().equals(codeModel.ref(Map.class))) {
        newClass = codeModel.ref(HashMap.class);
    } else if (jType.erasure().equals(codeModel.ref(Set.class))) {
        newClass = codeModel.ref(HashSet.class);
    } else if (jType.erasure().equals(codeModel.ref(SortedMap.class))) {
        newClass = codeModel.ref(TreeMap.class);
    } else if (jType.erasure().equals(codeModel.ref(SortedSet.class))) {
        newClass = codeModel.ref(TreeSet.class);
    }
    if (newClass != null && typeParameter != null) {
        newClass = newClass.narrow(typeParameter);
    }

    return newClass == null ? JExpr._null() : JExpr._new(newClass);
}
 
Example 5
Source File: FastDeserializerGenerator.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private JExpression getSchemaExpr(Schema schema) {
  Long index = Utils.getSchemaFingerprint(schema);
  return (useGenericTypes && schemaVarMap.containsKey(index)) ? schemaVarMap.get(index) : JExpr._null();
}