Search in sources :

Example 1 with JFormatter

use of com.sun.codemodel.JFormatter in project scout.rt by eclipse.

the class AnnotationUtil method createExpression.

/**
 * Creates an expression for the given annotation value. Thereby, all annotation member types as specified by JLS are
 * supported: primitive, String, Class, Enum, annotation, 1-dimensional array.
 */
public static JExpression createExpression(final JCodeModel model, final AnnotationValue _paramValue) {
    final Object _rawParamValue = _paramValue.getValue();
    // Class member type.
    if (_rawParamValue instanceof TypeMirror) {
        final TypeMirror _clazz = (TypeMirror) _rawParamValue;
        return JExpr.dotclass(model.ref(_clazz.toString()));
    } else // Enum member type.
    if (_rawParamValue instanceof VariableElement) {
        final VariableElement _enum = (VariableElement) _rawParamValue;
        final JClass enumType = model.ref(_enum.asType().toString());
        final String enumValue = _enum.getSimpleName().toString();
        return new JExpressionImpl() {

            @Override
            public void generate(final JFormatter f) {
                f.t(enumType).p('.').p(enumValue);
            }
        };
    } else // Annotation member type.
    if (_rawParamValue instanceof AnnotationMirror) {
        final AnnotationMirror _refAnnotation = (AnnotationMirror) _rawParamValue;
        final JClass refAnnotationClazz = model.ref(_refAnnotation.getAnnotationType().toString());
        final P_AnnotationExpression refAnnotationExpression = new P_AnnotationExpression(refAnnotationClazz);
        for (final Entry<? extends ExecutableElement, ? extends AnnotationValue> _annotationParamEntry : _refAnnotation.getElementValues().entrySet()) {
            final String paramName = _annotationParamEntry.getKey().getSimpleName().toString();
            refAnnotationExpression.param(paramName, AnnotationUtil.createExpression(model, _annotationParamEntry.getValue()));
        }
        return refAnnotationExpression;
    } else // Array member type.
    if (_rawParamValue instanceof List<?>) {
        final List<JExpression> expressions = new ArrayList<>();
        for (final Object _arrayElementValue : (List<?>) _rawParamValue) {
            expressions.add(AnnotationUtil.createExpression(model, (AnnotationValue) _arrayElementValue));
        }
        return new JExpressionImpl() {

            @Override
            public void generate(final JFormatter f) {
                if (expressions.size() == 1) {
                    f.g(expressions.get(0));
                } else {
                    f.p("{").g(expressions).p("}");
                }
            }
        };
    } else if (_rawParamValue instanceof String) {
        return JExpr.lit((String) _rawParamValue);
    } else if (_rawParamValue instanceof Integer) {
        return JExpr.lit((Integer) _rawParamValue);
    } else if (_rawParamValue instanceof Float) {
        return JExpr.lit((Float) _rawParamValue);
    } else if (_rawParamValue instanceof Double) {
        return JExpr.lit((Double) _rawParamValue);
    } else if (_rawParamValue instanceof Boolean) {
        return JExpr.lit((Boolean) _rawParamValue);
    } else if (_rawParamValue instanceof Character) {
        return JExpr.lit((Character) _rawParamValue);
    } else if (_rawParamValue instanceof Byte) {
        return JExpr.lit((Byte) _rawParamValue);
    } else {
        return JExpr._null();
    }
}
Also used : JClass(com.sun.codemodel.JClass) JFormatter(com.sun.codemodel.JFormatter) VariableElement(javax.lang.model.element.VariableElement) AnnotationMirror(javax.lang.model.element.AnnotationMirror) JExpressionImpl(com.sun.codemodel.JExpressionImpl) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with JFormatter

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

the class DefaultRuleTest method whenIsInitializeCollections_true_applyInitializesField.

@Test
public void whenIsInitializeCollections_true_applyInitializesField() throws JClassAlreadyExistsException {
    when(config.isInitializeCollections()).thenReturn(true);
    JDefinedClass jclass = new JCodeModel()._class("org.jsonschema2pojo.rules.ExampleClass");
    JFieldVar field = jclass.field(JMod.NONE, jclass.owner().ref(fieldTypeClass).narrow(Object.class), "value");
    ArrayNode node = new ObjectMapper().createArrayNode().add(1);
    StringWriter sw = new StringWriter();
    rule.apply("fooBar", node, null, field, null).bind(new JFormatter(sw));
    assertThat(sw.toString(), startsWith(String.format("%s<%s> value = ", fieldTypeClass.getName(), Object.class.getName())));
}
Also used : StringWriter(java.io.StringWriter) JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JFormatter(com.sun.codemodel.JFormatter) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with JFormatter

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

the class DefaultRuleTest method whenIsInitializeCollections_false_applyDoesNotInitializeField.

@Test
public void whenIsInitializeCollections_false_applyDoesNotInitializeField() throws JClassAlreadyExistsException {
    final String fieldName = "fieldName";
    when(config.isInitializeCollections()).thenReturn(false);
    JDefinedClass jclass = new JCodeModel()._class("org.jsonschema2pojo.rules.ExampleClass");
    JFieldVar field = jclass.field(JMod.NONE, jclass.owner().ref(fieldTypeClass).narrow(Object.class), fieldName);
    ArrayNode node = new ObjectMapper().createArrayNode();
    StringWriter sw = new StringWriter();
    rule.apply("fooBar", node, null, field, null).bind(new JFormatter(sw));
    assertThat(sw.toString(), is(String.format("%s<%s> %s", fieldTypeClass.getName(), Object.class.getName(), fieldName)));
}
Also used : StringWriter(java.io.StringWriter) JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JFormatter(com.sun.codemodel.JFormatter) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JFormatter (com.sun.codemodel.JFormatter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 JCodeModel (com.sun.codemodel.JCodeModel)2 JDefinedClass (com.sun.codemodel.JDefinedClass)2 JFieldVar (com.sun.codemodel.JFieldVar)2 StringWriter (java.io.StringWriter)2 Test (org.junit.Test)2 JClass (com.sun.codemodel.JClass)1 JExpressionImpl (com.sun.codemodel.JExpressionImpl)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1