use of com.sun.codemodel.JAnnotationValue 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;
}
}
use of com.sun.codemodel.JAnnotationValue in project jstuff by sebthom.
the class FieldInstantiatingPlugin method run.
@Override
public boolean run(final Outline outline, final Options options, final ErrorHandler errorHandler) throws SAXException {
// collect all types defined in the XSD
final List<JType> typeDefs = new ArrayList<>();
for (final ClassOutline classDef : outline.getClasses()) {
typeDefs.add(classDef.implClass);
}
// scan all XSD based classes for field references to other XSD based classes
for (final ClassOutline classDef : outline.getClasses()) {
for (final JFieldVar fieldDecl : classDef.implClass.fields().values()) {
/*
* @XmlElementRefs({
* @XmlElementRef(name = "bike", namespace = "my-config", type = JAXBElement.class, required = false),
* @XmlElementRef(name = "car", namespace = "my-config", type = JAXBElement.class, required = false)
* })
* private List<JAXBElement<?>> bikesAndCars;
*/
final Field memberValueFields = Fields.find(JAnnotationUse.class, "memberValues");
for (final JAnnotationUse a : fieldDecl.annotations()) {
if (//
jakarta.xml.bind.annotation.XmlElementRefs.class.getName().equals(a.getAnnotationClass().binaryName()) || //
"javax.xml.bind.annotation.XmlElementRefs".equals(a.getAnnotationClass().binaryName())) {
for (final JAnnotationUse xmlElementRefAnno : ((JAnnotationArrayMember) a.getAnnotationMembers().get("value")).annotations()) {
final JAnnotationValue requiredAttribute = xmlElementRefAnno.getAnnotationMembers().get("required");
if (requiredAttribute != null) {
((Map<?, ?>) Fields.read(xmlElementRefAnno, memberValueFields)).remove("required");
}
}
}
}
if (!typeDefs.contains(fieldDecl.type())) {
continue;
}
FieldOutline fieldDef = null;
for (final FieldOutline f : classDef.getDeclaredFields()) {
if (f.getPropertyInfo().getName(false).equals(fieldDecl.name())) {
fieldDef = f;
}
}
if (fieldDef == null)
throw new IllegalStateException("FieldOutline not found for " + fieldDecl.name());
boolean doInstantiate = false;
for (final CPluginCustomization pc : findCustomizations(fieldDef.getPropertyInfo().getCustomizations(), CUSTOMIZATION_ENABLED_TAG)) {
pc.markAsAcknowledged();
doInstantiate = true;
}
// initialize field
if (doInstantiate) {
LOG.info("%s#%s = new %s()", classDef.implClass.name(), fieldDecl.name(), fieldDecl.type().name());
fieldDecl.init(JExpr._new(fieldDecl.type()));
} else {
LOG.info("Not instantiating %s#%s", classDef.implClass.name(), fieldDecl.name());
}
}
}
return true;
}
Aggregations