use of com.sun.codemodel.JAnnotationClassValue in project eclipselink by eclipse-ee4j.
the class XJCJavaAnnotationImpl method getJavaAnnotation.
/**
* Return a Java <code>Annotation</code> representation of this <code>JavaAnnotation</code>.
*
* @return a Java <code>Annotation</code> representation of this <code>JavaAnnotation</code>.
*/
@SuppressWarnings("unchecked")
public Annotation getJavaAnnotation() {
try {
Map<String, Object> components = new HashMap<>();
// First, get all the default values for this annotation class.
Class<Annotation> annotationClass = (Class<Annotation>) getJavaAnnotationClass();
Method[] methods = annotationClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
components.put(methods[i].getName(), methods[i].getDefaultValue());
}
// Get the property values for this annotation instance.
Map<String, JAnnotationValue> memberValues = xjcAnnotation.getAnnotationMembers();
if (memberValues == null) {
// Return an annotation with just the defaults set.
return AnnotationProxy.getProxy(components, annotationClass, dynamicClassLoader, XMLConversionManager.getDefaultManager());
}
boolean isXmlEnum = annotationClass.equals(XmlEnum.class);
// Now overwrite the default values with anything we find in the XJC annotation instance.
for (String key : memberValues.keySet()) {
JAnnotationValue xjcValue = memberValues.get(key);
if (xjcValue instanceof JAnnotationArrayMember) {
Collection<JAnnotationValue> values = ((JAnnotationArrayMember) xjcValue).annotations2();
List<Object> valuesArray = new ArrayList<>(values.size());
for (JAnnotationValue val : values) {
if (val instanceof JAnnotationUse) {
JAnnotationUse xjcAnno = (JAnnotationUse) val;
XJCJavaAnnotationImpl anno = new XJCJavaAnnotationImpl(xjcAnno, dynamicClassLoader);
valuesArray.add(anno.getJavaAnnotation());
} else if (val instanceof JAnnotationStringValue) {
JAnnotationStringValue value = (JAnnotationStringValue) val;
valuesArray.add(value.toString());
} else if (val instanceof JAnnotationClassValue) {
JAnnotationClassValue cval = (JAnnotationClassValue) val;
valuesArray.add(getValueFromClsValue(cval, isXmlEnum));
} else {
throw new RuntimeException("got " + val.getClass().getName());
}
}
components.put(key, valuesArray.toArray(new Object[valuesArray.size()]));
} else if (xjcValue instanceof JAnnotationStringValue) {
JAnnotationStringValue value = (JAnnotationStringValue) xjcValue;
components.put(key, value.toString());
} else if (xjcValue instanceof JAnnotationClassValue) {
JAnnotationClassValue cval = (JAnnotationClassValue) xjcValue;
components.put(key, getValueFromClsValue(cval, isXmlEnum));
} else {
throw new RuntimeException("got " + xjcValue.getClass().getName());
}
}
return AnnotationProxy.getProxy(components, annotationClass, dynamicClassLoader, XMLConversionManager.getDefaultManager());
} catch (Exception e) {
return null;
}
}
Aggregations