Search in sources :

Example 1 with AltEnum

use of org.glassfish.hk2.xml.internal.alt.AltEnum in project glassfish-hk2 by eclipse-ee4j.

the class Generator method createAnnotationCopyOnly.

private static Annotation createAnnotationCopyOnly(ConstPool parent, AltAnnotation javaAnnotation) throws Throwable {
    Annotation annotation = new Annotation(javaAnnotation.annotationType(), parent);
    Map<String, Object> annotationValues = javaAnnotation.getAnnotationValues();
    for (Map.Entry<String, Object> entry : annotationValues.entrySet()) {
        String valueName = entry.getKey();
        Object value = entry.getValue();
        Class<?> javaAnnotationType = value.getClass();
        if (String.class.equals(javaAnnotationType)) {
            annotation.addMemberValue(valueName, new StringMemberValue((String) value, parent));
        } else if (Boolean.class.equals(javaAnnotationType)) {
            boolean bvalue = (Boolean) value;
            annotation.addMemberValue(valueName, new BooleanMemberValue(bvalue, parent));
        } else if (AltClass.class.isAssignableFrom(javaAnnotationType)) {
            AltClass altJavaAnnotationType = (AltClass) value;
            String sValue;
            if (javaAnnotation.annotationType().equals(XmlElement.class.getName()) && (javaAnnotation.getStringValue("getTypeByName") != null)) {
                sValue = javaAnnotation.getStringValue("getTypeByName");
            } else {
                sValue = altJavaAnnotationType.getName();
            }
            annotation.addMemberValue(valueName, new ClassMemberValue(sValue, parent));
        } else if (Integer.class.equals(javaAnnotationType)) {
            int ivalue = (Integer) value;
            annotation.addMemberValue(valueName, new IntegerMemberValue(parent, ivalue));
        } else if (Long.class.equals(javaAnnotationType)) {
            long lvalue = (Long) value;
            annotation.addMemberValue(valueName, new LongMemberValue(lvalue, parent));
        } else if (Double.class.equals(javaAnnotationType)) {
            double dvalue = (Double) value;
            annotation.addMemberValue(valueName, new DoubleMemberValue(dvalue, parent));
        } else if (Byte.class.equals(javaAnnotationType)) {
            byte bvalue = (Byte) value;
            annotation.addMemberValue(valueName, new ByteMemberValue(bvalue, parent));
        } else if (Character.class.equals(javaAnnotationType)) {
            char cvalue = (Character) value;
            annotation.addMemberValue(valueName, new CharMemberValue(cvalue, parent));
        } else if (Short.class.equals(javaAnnotationType)) {
            short svalue = (Short) value;
            annotation.addMemberValue(valueName, new ShortMemberValue(svalue, parent));
        } else if (Float.class.equals(javaAnnotationType)) {
            float fvalue = (Float) value;
            annotation.addMemberValue(valueName, new FloatMemberValue(fvalue, parent));
        } else if (AltEnum.class.isAssignableFrom(javaAnnotationType)) {
            AltEnum evalue = (AltEnum) value;
            EnumMemberValue jaEnum = new EnumMemberValue(parent);
            jaEnum.setType(evalue.getDeclaringClass());
            jaEnum.setValue(evalue.getName());
            annotation.addMemberValue(valueName, jaEnum);
        } else if (javaAnnotationType.isArray()) {
            Class<?> typeOfArray = javaAnnotationType.getComponentType();
            MemberValue[] arrayValue;
            if (int.class.equals(typeOfArray)) {
                int[] iVals = (int[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new IntegerMemberValue(parent, iVals[lcv]);
                }
            } else if (String.class.equals(typeOfArray)) {
                String[] iVals = (String[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new StringMemberValue(iVals[lcv], parent);
                }
            } else if (long.class.equals(typeOfArray)) {
                long[] iVals = (long[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new LongMemberValue(iVals[lcv], parent);
                }
            } else if (boolean.class.equals(typeOfArray)) {
                boolean[] iVals = (boolean[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new BooleanMemberValue(iVals[lcv], parent);
                }
            } else if (float.class.equals(typeOfArray)) {
                float[] iVals = (float[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new FloatMemberValue(iVals[lcv], parent);
                }
            } else if (double.class.equals(typeOfArray)) {
                double[] iVals = (double[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new DoubleMemberValue(iVals[lcv], parent);
                }
            } else if (byte.class.equals(typeOfArray)) {
                byte[] iVals = (byte[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new ByteMemberValue(iVals[lcv], parent);
                }
            } else if (char.class.equals(typeOfArray)) {
                char[] iVals = (char[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new CharMemberValue(iVals[lcv], parent);
                }
            } else if (short.class.equals(typeOfArray)) {
                short[] iVals = (short[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    arrayValue[lcv] = new ShortMemberValue(iVals[lcv], parent);
                }
            } else if (AltEnum.class.isAssignableFrom(typeOfArray)) {
                AltEnum[] iVals = (AltEnum[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    EnumMemberValue jaEnum = new EnumMemberValue(parent);
                    jaEnum.setType(iVals[lcv].getDeclaringClass());
                    jaEnum.setValue(iVals[lcv].getName());
                    arrayValue[lcv] = jaEnum;
                }
            } else if (AltClass.class.isAssignableFrom(typeOfArray)) {
                AltClass[] iVals = (AltClass[]) value;
                arrayValue = new MemberValue[iVals.length];
                for (int lcv = 0; lcv < iVals.length; lcv++) {
                    AltClass arrayElementClass = iVals[lcv];
                    arrayValue[lcv] = new ClassMemberValue(arrayElementClass.getName(), parent);
                }
            } else if (AltAnnotation.class.isAssignableFrom(typeOfArray)) {
                AltAnnotation[] aVals = (AltAnnotation[]) value;
                arrayValue = new MemberValue[aVals.length];
                for (int lcv = 0; lcv < aVals.length; lcv++) {
                    AltAnnotation arrayAnnotation = aVals[lcv];
                    arrayValue[lcv] = new AnnotationMemberValue(createAnnotationCopyOnly(parent, arrayAnnotation), parent);
                }
            } else {
                throw new AssertionError("Array type " + typeOfArray.getName() + " is not yet implemented for " + valueName);
            }
            ArrayMemberValue arrayMemberValue = new ArrayMemberValue(parent);
            arrayMemberValue.setValue(arrayValue);
            annotation.addMemberValue(valueName, arrayMemberValue);
        } else {
            throw new AssertionError("Annotation type " + javaAnnotationType.getName() + " is not yet implemented for " + valueName);
        }
    }
    return annotation;
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) AltClass(org.glassfish.hk2.xml.internal.alt.AltClass) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) AltAnnotation(org.glassfish.hk2.xml.internal.alt.AltAnnotation) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) Annotation(javassist.bytecode.annotation.Annotation) AltAnnotation(org.glassfish.hk2.xml.internal.alt.AltAnnotation) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) AltEnum(org.glassfish.hk2.xml.internal.alt.AltEnum) AnnotationMemberValue(javassist.bytecode.annotation.AnnotationMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) MemberValue(javassist.bytecode.annotation.MemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) CtClass(javassist.CtClass) AltClass(org.glassfish.hk2.xml.internal.alt.AltClass) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue)

Example 2 with AltEnum

use of org.glassfish.hk2.xml.internal.alt.AltEnum in project glassfish-hk2 by eclipse-ee4j.

the class AnnotationMirrorAltAnnotationImpl method getAnnotationValues.

/* (non-Javadoc)
     * @see org.glassfish.hk2.xml.internal.alt.AltAnnotation#getAnnotationValues()
     */
@Override
public synchronized Map<String, Object> getAnnotationValues() {
    if (values != null)
        return values;
    Map<? extends ExecutableElement, ? extends AnnotationValue> rawValues = processingEnv.getElementUtils().getElementValuesWithDefaults(annotation);
    Map<String, Object> retVal = new TreeMap<String, Object>();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : rawValues.entrySet()) {
        ExecutableElement annoMethod = entry.getKey();
        AnnotationValue annoValue = entry.getValue();
        String key = Utilities.convertNameToString(annoMethod.getSimpleName());
        Object value = annoValue.getValue();
        if (value instanceof TypeMirror) {
            // The annotation method is a java.lang.Class
            value = Utilities.convertTypeMirror((TypeMirror) value, processingEnv);
        } else if (value instanceof VariableElement) {
            // The annotation method is an Enum
            VariableElement variable = (VariableElement) value;
            TypeElement enclosing = (TypeElement) variable.getEnclosingElement();
            String annoClassName = Utilities.convertNameToString(enclosing.getQualifiedName());
            String annoVal = Utilities.convertNameToString(variable.getSimpleName());
            value = new StringAltEnumImpl(annoClassName, annoVal);
        } else if (value instanceof AnnotationMirror) {
            throw new AssertionError("The annotation " + annotation + " key " + key + " has unimplemented type AnnotationMirror");
        } else if (value instanceof List) {
            // The annotation method returns an array of something
            ArrayType returnType = (ArrayType) annoMethod.getReturnType();
            TypeMirror arrayTypeMirror = returnType.getComponentType();
            TypeKind arrayTypeKind = arrayTypeMirror.getKind();
            @SuppressWarnings("unchecked") List<? extends AnnotationValue> array = ((List<? extends AnnotationValue>) value);
            if (TypeKind.INT.equals(arrayTypeMirror.getKind())) {
                int[] iValue = new int[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Integer) item.getValue();
                }
                value = iValue;
            } else if (TypeKind.DECLARED.equals(arrayTypeMirror.getKind())) {
                AltClass[] cValue = new AltClass[array.size()];
                AltEnum[] eValue = new AltEnum[array.size()];
                String[] sValue = new String[array.size()];
                AltAnnotation[] aValue = new AltAnnotation[array.size()];
                boolean isClass = true;
                boolean isEnum = true;
                boolean isAnnos = false;
                int lcv = 0;
                for (AnnotationValue item : array) {
                    Object itemValue = item.getValue();
                    if (itemValue instanceof TypeMirror) {
                        isClass = true;
                        isEnum = false;
                        isAnnos = false;
                        cValue[lcv++] = Utilities.convertTypeMirror((TypeMirror) itemValue, processingEnv);
                    } else if (itemValue instanceof VariableElement) {
                        isClass = false;
                        isEnum = true;
                        isAnnos = false;
                        VariableElement variable = (VariableElement) itemValue;
                        TypeElement enclosing = (TypeElement) variable.getEnclosingElement();
                        String annoClassName = Utilities.convertNameToString(enclosing.getQualifiedName());
                        String annoVal = Utilities.convertNameToString(variable.getSimpleName());
                        eValue[lcv++] = new StringAltEnumImpl(annoClassName, annoVal);
                    } else if (itemValue instanceof String) {
                        isClass = false;
                        isEnum = false;
                        isAnnos = false;
                        sValue[lcv++] = (String) itemValue;
                    } else if (itemValue instanceof List) {
                        throw new AssertionError("Unimplemented declared List type in " + this);
                    } else if (itemValue instanceof AnnotationMirror) {
                        isClass = false;
                        isEnum = false;
                        isAnnos = true;
                        aValue[lcv++] = new AnnotationMirrorAltAnnotationImpl((AnnotationMirror) itemValue, processingEnv);
                    } else {
                        throw new AssertionError("Unknown declared type: " + itemValue.getClass().getName());
                    }
                }
                if (isClass) {
                    value = cValue;
                } else if (isEnum) {
                    value = eValue;
                } else if (isAnnos) {
                    value = aValue;
                } else {
                    value = sValue;
                }
            } else if (TypeKind.LONG.equals(arrayTypeMirror.getKind())) {
                long[] iValue = new long[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Long) item.getValue();
                }
                value = iValue;
            } else if (TypeKind.SHORT.equals(arrayTypeMirror.getKind())) {
                short[] iValue = new short[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Short) item.getValue();
                }
                value = iValue;
            } else if (TypeKind.CHAR.equals(arrayTypeMirror.getKind())) {
                char[] iValue = new char[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Character) item.getValue();
                }
                value = iValue;
            } else if (TypeKind.FLOAT.equals(arrayTypeMirror.getKind())) {
                float[] iValue = new float[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Float) item.getValue();
                }
                value = iValue;
            } else if (TypeKind.DOUBLE.equals(arrayTypeMirror.getKind())) {
                double[] iValue = new double[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Double) item.getValue();
                }
                value = iValue;
            } else if (TypeKind.BOOLEAN.equals(arrayTypeMirror.getKind())) {
                boolean[] iValue = new boolean[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Boolean) item.getValue();
                }
                value = iValue;
            } else if (TypeKind.BYTE.equals(arrayTypeMirror.getKind())) {
                byte[] iValue = new byte[array.size()];
                int lcv = 0;
                for (AnnotationValue item : array) {
                    iValue[lcv++] = (Byte) item.getValue();
                }
                value = iValue;
            } else {
                throw new AssertionError("Array type " + arrayTypeKind + " is not implemented");
            }
        }
        retVal.put(key, value);
    }
    values = Collections.unmodifiableMap(retVal);
    return values;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) TypeKind(javax.lang.model.type.TypeKind) VariableElement(javax.lang.model.element.VariableElement) AltClass(org.glassfish.hk2.xml.internal.alt.AltClass) ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) AltAnnotation(org.glassfish.hk2.xml.internal.alt.AltAnnotation) TypeElement(javax.lang.model.element.TypeElement) TreeMap(java.util.TreeMap) AltEnum(org.glassfish.hk2.xml.internal.alt.AltEnum) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 3 with AltEnum

use of org.glassfish.hk2.xml.internal.alt.AltEnum in project glassfish-hk2 by eclipse-ee4j.

the class AnnotationAltAnnotationImpl method getAnnotationValues.

/* (non-Javadoc)
     * @see org.glassfish.hk2.xml.internal.alt.AltAnnotation#getAnnotationValues()
     */
@Override
public synchronized Map<String, Object> getAnnotationValues() {
    if (values != null)
        return values;
    Map<String, Object> retVal = new TreeMap<String, Object>();
    for (Method javaAnnotationMethod : annotation.annotationType().getMethods()) {
        if (javaAnnotationMethod.getParameterTypes().length != 0)
            continue;
        if (DO_NOT_HANDLE_METHODS.contains(javaAnnotationMethod.getName()))
            continue;
        String key = javaAnnotationMethod.getName();
        Object value;
        try {
            value = ReflectionHelper.invoke(annotation, javaAnnotationMethod, new Object[0], false);
            if (value == null) {
                throw new AssertionError("Recieved null from annotation method " + javaAnnotationMethod.getName());
            }
        } catch (RuntimeException re) {
            throw re;
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
        if (value instanceof Class) {
            value = new ClassAltClassImpl((Class<?>) value, helper);
        } else if (Enum.class.isAssignableFrom(value.getClass())) {
            value = new EnumAltEnumImpl((Enum<?>) value);
        } else if (value.getClass().isArray() && Class.class.equals(value.getClass().getComponentType())) {
            Class<?>[] cValue = (Class<?>[]) value;
            AltClass[] translatedValue = new AltClass[cValue.length];
            for (int lcv = 0; lcv < cValue.length; lcv++) {
                translatedValue[lcv] = new ClassAltClassImpl(cValue[lcv], helper);
            }
            value = translatedValue;
        } else if (value.getClass().isArray() && Enum.class.isAssignableFrom(value.getClass().getComponentType())) {
            Enum<?>[] eValue = (Enum<?>[]) value;
            AltEnum[] translatedValue = new AltEnum[eValue.length];
            for (int lcv = 0; lcv < eValue.length; lcv++) {
                translatedValue[lcv] = new EnumAltEnumImpl(eValue[lcv]);
            }
            value = translatedValue;
        } else if (value.getClass().isArray() && Annotation.class.isAssignableFrom(value.getClass().getComponentType())) {
            Annotation[] aValue = (Annotation[]) value;
            AltAnnotation[] translatedValue = new AltAnnotation[aValue.length];
            for (int lcv = 0; lcv < aValue.length; lcv++) {
                translatedValue[lcv] = new AnnotationAltAnnotationImpl(aValue[lcv], helper);
            }
            value = translatedValue;
        }
        retVal.put(key, value);
    }
    values = Collections.unmodifiableMap(retVal);
    return values;
}
Also used : AltEnum(org.glassfish.hk2.xml.internal.alt.AltEnum) AltAnnotation(org.glassfish.hk2.xml.internal.alt.AltAnnotation) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) AltClass(org.glassfish.hk2.xml.internal.alt.AltClass) AltAnnotation(org.glassfish.hk2.xml.internal.alt.AltAnnotation) Annotation(java.lang.annotation.Annotation) AltClass(org.glassfish.hk2.xml.internal.alt.AltClass)

Aggregations

AltAnnotation (org.glassfish.hk2.xml.internal.alt.AltAnnotation)3 AltClass (org.glassfish.hk2.xml.internal.alt.AltClass)3 AltEnum (org.glassfish.hk2.xml.internal.alt.AltEnum)3 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 CtClass (javassist.CtClass)1 Annotation (javassist.bytecode.annotation.Annotation)1 AnnotationMemberValue (javassist.bytecode.annotation.AnnotationMemberValue)1 ArrayMemberValue (javassist.bytecode.annotation.ArrayMemberValue)1 BooleanMemberValue (javassist.bytecode.annotation.BooleanMemberValue)1 ByteMemberValue (javassist.bytecode.annotation.ByteMemberValue)1 CharMemberValue (javassist.bytecode.annotation.CharMemberValue)1 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)1 DoubleMemberValue (javassist.bytecode.annotation.DoubleMemberValue)1 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)1 FloatMemberValue (javassist.bytecode.annotation.FloatMemberValue)1