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();
}
}
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())));
}
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)));
}
Aggregations