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;
@ -59,12 +53,52 @@ public class AnnotationPersistenceXMLMetaDataParser {
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.
*/ */
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,18 +193,21 @@ public class AnnotationPersistenceXMLMetaDataParser {
} }
private void parseXmlRootElement(Class type, XMLMetaData meta) { private void parseXmlRootElement(Class type, XMLMetaData meta) {
if (type.getAnnotation(XmlRootElement.class) != null) { try {
if (type.getAnnotation(xmlRootElementClass) != null) {
meta.setXmlRootElement(true); meta.setXmlRootElement(true);
meta.setXmlname(((XmlRootElement) type.getAnnotation meta.setXmlname((String) xmlRootName.invoke(type.getAnnotation
(XmlRootElement.class)).name()); (xmlRootElementClass), new Object[]{}));
meta.setXmlnamespace(((XmlRootElement) type.getAnnotation meta.setXmlnamespace((String) xmlRootNamespace.invoke(type
(XmlRootElement.class)).namespace()); .getAnnotation(xmlRootElementClass), new Object[]{}));
} }
else { else {
meta.setXmlname(((XmlType) type.getAnnotation meta.setXmlname((String) xmlTypeName.invoke(type.getAnnotation
(XmlType.class)).name()); (xmlTypeClass), new Object[]{}));
meta.setXmlnamespace(((XmlType) type.getAnnotation meta.setXmlnamespace((String) xmlTypeNamespace.invoke(type
(XmlType.class)).namespace()); .getAnnotation(xmlTypeClass), new Object[]{}));
}
} catch (Exception e) {
} }
} }
@ -180,11 +217,12 @@ 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(
xmlAccessorTypeClass), new Object[]{}).toString(), "FIELD"))
members = cls.getDeclaredFields(); members = cls.getDeclaredFields();
else else
members = cls.getDeclaredMethods(); members = cls.getDeclaredMethods();
@ -193,17 +231,19 @@ public class AnnotationPersistenceXMLMetaDataParser {
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
.getAnnotation(xmlElementClass), new Object[]{});
// avoid JAXB XML bind default name // avoid JAXB XML bind default name
if (StringUtils.equals(XMLMetaData.defaultName, xmlname)) if (StringUtils.equals(XMLMetaData.defaultName, xmlname))
xmlname = member.getName(); xmlname = member.getName();
if (((Field) member).getType() if (((Field) member).getType()
.isAnnotationPresent(XmlType.class)) { .isAnnotationPresent(xmlTypeClass)) {
field = _repos.addXMLMetaData(((Field) member).getType() field = _repos.addXMLMetaData(((Field) member).getType()
, member.getName()); , member.getName());
parseXmlRootElement(((Field) member).getType(), field); parseXmlRootElement(((Field) member).getType(), field);
populateFromReflection(((Field) member).getType(), field); populateFromReflection(((Field) member).getType()
, field);
field.setXmltype(XMLMetaData.XMLTYPE); field.setXmltype(XMLMetaData.XMLTYPE);
field.setXmlname(xmlname); field.setXmlname(xmlname);
} }
@ -212,23 +252,27 @@ public class AnnotationPersistenceXMLMetaDataParser {
.getType(), member.getName()); .getType(), member.getName());
field.setXmltype(XMLMetaData.ELEMENT); field.setXmltype(XMLMetaData.ELEMENT);
field.setXmlname(xmlname); field.setXmlname(xmlname);
field.setXmlnamespace(el.getAnnotation(XmlElement.class) field.setXmlnamespace((String) xmlElementNamespace
.namespace()); .invoke(el.getAnnotation(xmlElementClass)
, new Object[]{}));
} }
} }
else if (el.getAnnotation(XmlAttribute.class) != null) { else if (el.getAnnotation(xmlAttributeClass) != null) {
field = _repos.newXMLFieldMetaData(((Field) member).getType() field = _repos.newXMLFieldMetaData(((Field) member)
, member.getName()); .getType(), member.getName());
field.setXmltype(XMLFieldMetaData.ATTRIBUTE); field.setXmltype(XMLFieldMetaData.ATTRIBUTE);
String xmlname = el.getAnnotation(XmlAttribute.class).name(); String xmlname = (String) xmlAttributeName.invoke(
el.getAnnotation(xmlAttributeClass), new Object[]{});
// avoid JAXB XML bind default name // avoid JAXB XML bind default name
if (StringUtils.equals(XMLMetaData.defaultName, xmlname)) if (StringUtils.equals(XMLMetaData.defaultName, xmlname))
xmlname = member.getName(); xmlname = member.getName();
field.setXmlname("@"+xmlname); field.setXmlname("@"+xmlname);
field.setXmlnamespace(el.getAnnotation(XmlAttribute.class) field.setXmlnamespace((String) xmlAttributeNamespace.invoke(
.namespace()); el.getAnnotation(xmlAttributeClass), new Object[]{}));
} }
meta.addField(member.getName(), field); meta.addField(member.getName(), field);
} }
} catch(Exception e) {
}
} }
} }