Java Code Examples for com.sun.codemodel.JAnnotationValue#generate()

The following examples show how to use com.sun.codemodel.JAnnotationValue#generate() . 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: 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 2
Source File: ClassDiscoverer.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the extraction of the schema type from the XmlElement
 * annotation. This was surprisingly difficult. Apparently the
 * model doesn't provide access to the annotation we're referring to
 * so I need to print it and read the string back. Even the formatter
 * itself is final!
 * @param outline root of the generated code
 * @param directClasses set of classes to append to
 * @param type annotation we're analysing
 */
private static void handleXmlElement(Outline outline, Set<String> directClasses, JAnnotationValue type) {
    StringWriter sw = new StringWriter();
    JFormatter jf = new JFormatter(new PrintWriter(sw));
    type.generate(jf);
    String s = sw.toString();
    s = s.substring(0, s.length()-".class".length());
    if (!s.startsWith("java") && outline.getCodeModel()._getClass(s) == null && !foundWithinOutline(s, outline)) {
        directClasses.add(s);
    }
}
 
Example 3
Source File: CreateJAXBElementNameCallback.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
private String annotationValueToString(JAnnotationValue ns) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JFormatter jf = new JFormatter(pw, "");
    ns.generate(jf);
    pw.flush();
    String s = sw.toString();
    return s.substring(1, s.length()-1);
}
 
Example 4
Source File: PluginImpl.java    From immutable-xjc with MIT License 5 votes vote down vote up
private JMethod getGetterProperty(final JFieldVar field, final JDefinedClass clazz) {
    JMethod getter = clazz.getMethod("get" + StringUtils.capitalize(field.name()), NO_ARGS);
    if (getter == null) {
        getter = clazz.getMethod("is" + StringUtils.capitalize(field.name()), NO_ARGS);
    }

    if (getter == null) {
        List<JDefinedClass> superClasses = getSuperClasses(clazz);
        for (JDefinedClass definedClass : superClasses) {
            getter = getGetterProperty(field, definedClass);

            if (getter != null) {
                break;
            }
        }
    }
    if (getter == null) {
        //XJC does not work conform Introspector.decapitalize when multiple upper-case letter are in field name
        Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName());
        if (xmlElementAnnotation.isPresent()) {
            JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("name");
            if (annotationValue != null) {
                StringWriter sw = new StringWriter();
                JFormatter f = new JFormatter(sw);
                annotationValue.generate(f);
                getter = clazz.getMethod("get" + sw.toString().replaceAll("\"", ""), NO_ARGS);
            }
        }
    }
    return getter;
}