try {
_constructor = _class.getDeclaredConstructor(NO_PARAMS);
_constructor.setAccessible(true);
}
catch (Exception e2) {
throw new JAXBException(L.l("{0}: Zero-arg constructor not found",
_class.getName()), e2);
}
}
_typeName = JAXBUtil.getXmlSchemaDatatype(_class, _context);
// Special case: when name="", this is an "anonymous" type, bound
// exclusively to a particular element name
if (! "".equals(_typeName.getLocalPart()))
_context.addXmlType(_typeName, this);
// Check for the complete name of the element...
String namespace = _context.getTargetNamespace();
// look at package defaults first...
XmlSchema schema = (XmlSchema) _package.getAnnotation(XmlSchema.class);
if (schema != null && ! "".equals(schema.namespace()))
namespace = schema.namespace();
// then look at class specific overrides.
XmlRootElement xre = _class.getAnnotation(XmlRootElement.class);
if (xre != null) {
String localName = null;
if ("##default".equals(xre.name()))
localName = JAXBUtil.identifierToXmlName(_class);
else
localName = xre.name();
if (! "##default".equals(xre.namespace()))
namespace = xre.namespace();
if (namespace == null)
_elementName = new QName(localName);
else
_elementName = new QName(namespace, localName);
_context.addRootElement(this);
}
// order the elements, if specified
XmlAccessOrder accessOrder = XmlAccessOrder.UNDEFINED;
XmlAccessorOrder packageOrder =
_package.getAnnotation(XmlAccessorOrder.class);
XmlAccessorOrder classOrder =
_class.getAnnotation(XmlAccessorOrder.class);
if (packageOrder != null)
accessOrder = packageOrder.value();
if (classOrder != null)
accessOrder = classOrder.value();
// try property orders too
XmlType xmlType = (XmlType) _class.getAnnotation(XmlType.class);
HashMap<String,Integer> orderMap = null;
if (xmlType != null &&
(xmlType.propOrder().length != 1 ||
! "".equals(xmlType.propOrder()[0]))) {
// non-default propOrder
orderMap = new HashMap<String,Integer>();
for (int i = 0; i < xmlType.propOrder().length; i++)
orderMap.put(xmlType.propOrder()[i], i);
}
// Collect the fields/properties of the class
if (orderMap != null) {
for (int i = 0; i < orderMap.size(); i++)
_elementMappings.add(null);
}
XmlAccessorType accessorType =
_class.getAnnotation(XmlAccessorType.class);
XmlAccessType accessType = (accessorType == null ?
XmlAccessType.PUBLIC_MEMBER :
accessorType.value());
if (accessType != XmlAccessType.FIELD) {
// getter/setter
TreeSet<Method> methodSet = new TreeSet<Method>(methodComparator);
Method[] declared = _class.getDeclaredMethods();
for (Method m : declared)
methodSet.add(m);
Method[] methods = new Method[methodSet.size()];
methodSet.toArray(methods);
AccessibleObject.setAccessible(methods, true);
while (methodSet.size() > 0) {
Method m = methodSet.first();
methodSet.remove(m);
String name = null;
Method get = null;
Method set = null;
if (m.getName().startsWith("get")) {
get = m;
if (Void.TYPE.equals(get.getReturnType()))
continue;
name = get.getName().substring(3); // 3 == "get".length());
Class cl = get.getDeclaringClass();
try {
set = cl.getDeclaredMethod("set" + name, get.getReturnType());
}
catch (NoSuchMethodException e) {
continue;
}
if (! methodSet.remove(set))
continue;
}
else if (m.getName().startsWith("set")) {
set = m;
Class[] parameterTypes = set.getParameterTypes();
if (parameterTypes.length != 1)
continue;
name = set.getName().substring(3); // 3 == "set".length());
Class cl = set.getDeclaringClass();
try {
get = cl.getDeclaredMethod("get" + name);
}
catch (NoSuchMethodException e) {
continue;
}
if (! parameterTypes[0].equals(get.getReturnType()))
continue;
if (! methodSet.remove(get))
continue;
}
else
continue;
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
// JAXB specifies that a "class" property must be specified as "clazz"
// because of Object.getClass()
if ("class".equals(name))
continue;
// XXX special cases for Throwable specified in JAX-WS
// Should it be in the general JAXB?
if (Throwable.class.isAssignableFrom(_class) &&
("stackTrace".equals(name) ||
"cause".equals(name) ||
"localizedMessage".equals(name)))
continue;
// XXX PUBLIC_MEMBER
if (accessType == XmlAccessType.NONE &&
! JAXBUtil.isJAXBAnnotated(get) &&
! JAXBUtil.isJAXBAnnotated(set))
continue;
// jaxb/0456
if (Modifier.isStatic(get.getModifiers()) &&
JAXBUtil.isJAXBAnnotated(get))
throw new JAXBException(L.l("JAXB annotations cannot be applied to static methods: {0}", get));
// jaxb/0457
if (Modifier.isStatic(set.getModifiers()) &&
JAXBUtil.isJAXBAnnotated(set))
throw new JAXBException(L.l("JAXB annotations cannot be applied to static methods: {0}", set));
// jaxb/0374
if (Modifier.isStatic(set.getModifiers()) ||
Modifier.isStatic(get.getModifiers()))
continue;
if (get != null && get.isAnnotationPresent(XmlTransient.class))
continue;
if (set != null && set.isAnnotationPresent(XmlTransient.class))
continue;
get.setAccessible(true);
set.setAccessible(true);
Accessor a = new GetterSetterAccessor(name, get, set);
if (orderMap != null) {
Integer i = orderMap.remove(name);
if (i != null)
a.setOrder(i.intValue());
// XXX else throw something?
}
processAccessor(a);
}
}
if (accessType != XmlAccessType.PROPERTY) {
// XXX Don't overwrite property accessors
HashSet<Field> fieldSet = new HashSet<Field>();
Field[] declared = _class.getDeclaredFields();
for (Field f : declared)
fieldSet.add(f);
Field[] fields = new Field[fieldSet.size()];
fieldSet.toArray(fields);
AccessibleObject.setAccessible(fields, true);
for (Field f : fields) {
if (f.isAnnotationPresent(XmlLocation.class))
{
if (! f.getType().equals(Location.class))
throw new JAXBException(L.l("Fields annotated by @Location must have type javax.xml.stream.Location"));
_locationAccessor = new FieldAccessor(f);
}
// special case: jaxb/0250
// fields which are static are skipped _unless_ they are also
// both final and attributes
if (Modifier.isStatic(f.getModifiers()) &&
! (Modifier.isFinal(f.getModifiers()) &&
f.isAnnotationPresent(XmlAttribute.class)))
continue;
if (f.isAnnotationPresent(XmlTransient.class))
continue;
// jaxb/0176: transient modifier ignored
if (accessType == XmlAccessType.PUBLIC_MEMBER &&
! Modifier.isPublic(f.getModifiers()) &&
! JAXBUtil.isJAXBAnnotated(f))
continue;
if (accessType == XmlAccessType.NONE && ! JAXBUtil.isJAXBAnnotated(f))
continue;
Accessor a = new FieldAccessor(f);
if (orderMap != null) {
Integer i = orderMap.remove(f.getName());
if (i != null)
a.setOrder(i.intValue());
// XXX else throw something?
}
processAccessor(a);
}
}
// do ordering if necessary
if (orderMap == null && accessOrder == XmlAccessOrder.ALPHABETICAL)
Collections.sort(_elementMappings, XmlMapping.nameComparator);
}
catch (JAXBException e) {
throw e;
}
catch (Exception e) {
throw new JAXBException(L.l("{0}: Initialization error",
_class.getName()), e);
}
if (! Object.class.equals(_class.getSuperclass()))
_parent = _context.getSkeleton(_class.getSuperclass());