Search in sources :

Example 1 with JExpressionImpl

use of com.sun.codemodel.JExpressionImpl 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)

Aggregations

JClass (com.sun.codemodel.JClass)1 JExpressionImpl (com.sun.codemodel.JExpressionImpl)1 JFormatter (com.sun.codemodel.JFormatter)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