OPENJPA240 applied patch by Catalina Wei to remove static dependency on JAXB classes

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@563031 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Marc Prud'hommeaux 2007-08-06 05:53:17 +00:00
parent 9382677eb1
commit 156a7ffdd0
2 changed files with 106 additions and 67 deletions

View File

@ -45,11 +45,6 @@
<version>1.0</version> <version>1.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -21,13 +21,7 @@ package org.apache.openjpa.persistence;
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Member; import java.lang.reflect.Member;
import java.lang.reflect.Method;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.openjpa.conf.OpenJPAConfiguration; import org.apache.openjpa.conf.OpenJPAConfiguration;
@ -58,6 +52,23 @@ public class AnnotationPersistenceXMLMetaDataParser {
// the class we were invoked to parse // the class we were invoked to parse
private Class _cls = null; private Class _cls = null;
private FieldMetaData _fmd = null; private FieldMetaData _fmd = null;
// cache the JAXB Xml... classes if they are present so we do not
// have a hard-wired dependency on JAXB here
private Class xmlTypeClass = null;
private Class xmlRootElementClass = null;
private Class xmlAccessorTypeClass = null;
private Class xmlAttributeClass = null;
private Class xmlElementClass = null;
private Method xmlTypeName = null;
private Method xmlTypeNamespace = null;
private Method xmlRootName = null;
private Method xmlRootNamespace = null;
private Method xmlAttributeName = null;
private Method xmlAttributeNamespace = null;
private Method xmlElementName = null;
private Method xmlElementNamespace = null;
private Method xmlAccessorValue = null;
/** /**
* Constructor; supply configuration. * Constructor; supply configuration.
@ -65,6 +76,29 @@ public class AnnotationPersistenceXMLMetaDataParser {
public AnnotationPersistenceXMLMetaDataParser(OpenJPAConfiguration conf) { public AnnotationPersistenceXMLMetaDataParser(OpenJPAConfiguration conf) {
_conf = conf; _conf = conf;
_log = conf.getLog(OpenJPAConfiguration.LOG_METADATA); _log = conf.getLog(OpenJPAConfiguration.LOG_METADATA);
try {
xmlTypeClass = Class.forName(
"javax.xml.bind.annotation.XmlType");
xmlTypeName = xmlTypeClass.getMethod("name", null);
xmlTypeNamespace = xmlTypeClass.getMethod("namespace", null);
xmlRootElementClass = Class.forName(
"javax.xml.bind.annotation.XmlRootElement");
xmlRootName = xmlRootElementClass.getMethod("name", null);
xmlRootNamespace = xmlRootElementClass.getMethod("namespace", null);
xmlAccessorTypeClass = Class.forName(
"javax.xml.bind.annotation.XmlAccessorType");
xmlAccessorValue = xmlAccessorTypeClass.getMethod("value", null);
xmlAttributeClass = Class.forName(
"javax.xml.bind.annotation.XmlAttribute");
xmlAttributeName = xmlAttributeClass.getMethod("name", null);
xmlAttributeNamespace = xmlAttributeClass.getMethod("namespace"
, null);
xmlElementClass = Class.forName(
"javax.xml.bind.annotation.XmlElement");
xmlElementName = xmlElementClass.getMethod("name", null);
xmlElementNamespace = xmlElementClass.getMethod("namespace", null);
} catch (Exception e) {
}
} }
/** /**
@ -135,7 +169,7 @@ public class AnnotationPersistenceXMLMetaDataParser {
*/ */
private XMLMetaData parseXMLClassAnnotations() { private XMLMetaData parseXMLClassAnnotations() {
// check immediately whether the class has JAXB XML annotations // check immediately whether the class has JAXB XML annotations
if (!_cls.isAnnotationPresent(XmlType.class)) if (!_cls.isAnnotationPresent(xmlTypeClass))
return null; return null;
// find / create metadata // find / create metadata
@ -159,19 +193,22 @@ public class AnnotationPersistenceXMLMetaDataParser {
} }
private void parseXmlRootElement(Class type, XMLMetaData meta) { private void parseXmlRootElement(Class type, XMLMetaData meta) {
if (type.getAnnotation(XmlRootElement.class) != null) { try {
meta.setXmlRootElement(true); if (type.getAnnotation(xmlRootElementClass) != null) {
meta.setXmlname(((XmlRootElement) type.getAnnotation meta.setXmlRootElement(true);
(XmlRootElement.class)).name()); meta.setXmlname((String) xmlRootName.invoke(type.getAnnotation
meta.setXmlnamespace(((XmlRootElement) type.getAnnotation (xmlRootElementClass), new Object[]{}));
(XmlRootElement.class)).namespace()); meta.setXmlnamespace((String) xmlRootNamespace.invoke(type
.getAnnotation(xmlRootElementClass), new Object[]{}));
}
else {
meta.setXmlname((String) xmlTypeName.invoke(type.getAnnotation
(xmlTypeClass), new Object[]{}));
meta.setXmlnamespace((String) xmlTypeNamespace.invoke(type
.getAnnotation(xmlTypeClass), new Object[]{}));
}
} catch (Exception e) {
} }
else {
meta.setXmlname(((XmlType) type.getAnnotation
(XmlType.class)).name());
meta.setXmlnamespace(((XmlType) type.getAnnotation
(XmlType.class)).namespace());
}
} }
private void populateFromReflection(Class cls, XMLMetaData meta) { private void populateFromReflection(Class cls, XMLMetaData meta) {
@ -180,55 +217,62 @@ public class AnnotationPersistenceXMLMetaDataParser {
Class superclass = cls.getSuperclass(); Class superclass = cls.getSuperclass();
// handle inheritance at sub-element level // handle inheritance at sub-element level
if (superclass.isAnnotationPresent(XmlType.class)) if (superclass.isAnnotationPresent(xmlTypeClass))
populateFromReflection(superclass, meta); populateFromReflection(superclass, meta);
if (((XmlAccessorType) cls.getAnnotation(XmlAccessorType.class)).value() try {
== XmlAccessType.FIELD) if (StringUtils.equals(xmlAccessorValue.invoke(cls.getAnnotation(
members = cls.getDeclaredFields(); xmlAccessorTypeClass), new Object[]{}).toString(), "FIELD"))
else members = cls.getDeclaredFields();
members = cls.getDeclaredMethods(); else
members = cls.getDeclaredMethods();
for (int i = 0; i < members.length; i++) { for (int i = 0; i < members.length; i++) {
Member member = members[i]; Member member = members[i];
AnnotatedElement el = (AnnotatedElement) member; AnnotatedElement el = (AnnotatedElement) member;
XMLMetaData field = null; XMLMetaData field = null;
if (el.getAnnotation(XmlElement.class) != null) { if (el.getAnnotation(xmlElementClass) != null) {
String xmlname = el.getAnnotation(XmlElement.class).name(); String xmlname = (String) xmlElementName.invoke(el
// avoid JAXB XML bind default name .getAnnotation(xmlElementClass), new Object[]{});
if (StringUtils.equals(XMLMetaData.defaultName, xmlname)) // avoid JAXB XML bind default name
xmlname = member.getName(); if (StringUtils.equals(XMLMetaData.defaultName, xmlname))
if (((Field) member).getType() xmlname = member.getName();
.isAnnotationPresent(XmlType.class)) { if (((Field) member).getType()
field = _repos.addXMLMetaData(((Field) member).getType() .isAnnotationPresent(xmlTypeClass)) {
, member.getName()); field = _repos.addXMLMetaData(((Field) member).getType()
parseXmlRootElement(((Field) member).getType(), field); , member.getName());
populateFromReflection(((Field) member).getType(), field); parseXmlRootElement(((Field) member).getType(), field);
field.setXmltype(XMLMetaData.XMLTYPE); populateFromReflection(((Field) member).getType()
field.setXmlname(xmlname); , field);
field.setXmltype(XMLMetaData.XMLTYPE);
field.setXmlname(xmlname);
}
else {
field = _repos.newXMLFieldMetaData(((Field) member)
.getType(), member.getName());
field.setXmltype(XMLMetaData.ELEMENT);
field.setXmlname(xmlname);
field.setXmlnamespace((String) xmlElementNamespace
.invoke(el.getAnnotation(xmlElementClass)
, new Object[]{}));
}
} }
else { else if (el.getAnnotation(xmlAttributeClass) != null) {
field = _repos.newXMLFieldMetaData(((Field) member) field = _repos.newXMLFieldMetaData(((Field) member)
.getType(), member.getName()); .getType(), member.getName());
field.setXmltype(XMLMetaData.ELEMENT); field.setXmltype(XMLFieldMetaData.ATTRIBUTE);
field.setXmlname(xmlname); String xmlname = (String) xmlAttributeName.invoke(
field.setXmlnamespace(el.getAnnotation(XmlElement.class) el.getAnnotation(xmlAttributeClass), new Object[]{});
.namespace()); // avoid JAXB XML bind default name
if (StringUtils.equals(XMLMetaData.defaultName, xmlname))
xmlname = member.getName();
field.setXmlname("@"+xmlname);
field.setXmlnamespace((String) xmlAttributeNamespace.invoke(
el.getAnnotation(xmlAttributeClass), new Object[]{}));
} }
meta.addField(member.getName(), field);
} }
else if (el.getAnnotation(XmlAttribute.class) != null) { } catch(Exception e) {
field = _repos.newXMLFieldMetaData(((Field) member).getType() }
, member.getName());
field.setXmltype(XMLFieldMetaData.ATTRIBUTE);
String xmlname = el.getAnnotation(XmlAttribute.class).name();
// avoid JAXB XML bind default name
if (StringUtils.equals(XMLMetaData.defaultName, xmlname))
xmlname = member.getName();
field.setXmlname("@"+xmlname);
field.setXmlnamespace(el.getAnnotation(XmlAttribute.class)
.namespace());
}
meta.addField(member.getName(), field);
}
} }
} }