
org.eclipse.persistence.jaxb.javamodel.oxm.OXMJavaClassImpl Maven / Gradle / Ivy
/******************************************************************************* * Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * Rick Barkhouse - 2.2 - Initial implementation ******************************************************************************/ package org.eclipse.persistence.jaxb.javamodel.oxm; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import javax.xml.bind.JAXBElement; import org.eclipse.persistence.internal.security.PrivilegedAccessHelper; import org.eclipse.persistence.jaxb.compiler.XMLProcessor; import org.eclipse.persistence.jaxb.javamodel.JavaAnnotation; import org.eclipse.persistence.jaxb.javamodel.JavaClass; import org.eclipse.persistence.jaxb.javamodel.JavaConstructor; import org.eclipse.persistence.jaxb.javamodel.JavaField; import org.eclipse.persistence.jaxb.javamodel.JavaMethod; import org.eclipse.persistence.jaxb.javamodel.JavaModel; import org.eclipse.persistence.jaxb.javamodel.JavaPackage; import org.eclipse.persistence.jaxb.xmlmodel.JavaAttribute; import org.eclipse.persistence.jaxb.xmlmodel.JavaType; import org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes; import org.eclipse.persistence.jaxb.xmlmodel.XmlAnyAttribute; import org.eclipse.persistence.jaxb.xmlmodel.XmlAnyElement; import org.eclipse.persistence.jaxb.xmlmodel.XmlAttribute; import org.eclipse.persistence.jaxb.xmlmodel.XmlElement; import org.eclipse.persistence.jaxb.xmlmodel.XmlElementRef; import org.eclipse.persistence.jaxb.xmlmodel.XmlElements; import org.eclipse.persistence.jaxb.xmlmodel.XmlInverseReference; import org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes; import org.eclipse.persistence.jaxb.xmlmodel.XmlValue; /** * INTERNAL: *
Class, otherwise* Purpose:
* *JavaClass
implementation wrapping MOXy'sxmlmodel.JavaType
. * Used when bootstrapping aDynamicJAXBContext
from XML Bindings. ** Responsibilities: *
*
* * * @since EclipseLink 2.2 * * @see org.eclipse.persistence.jaxb.javamodel.JavaClass * @see org.eclipse.persistence.jaxb.xmlmodel.JavaType */ public class OXMJavaClassImpl implements JavaClass { private JavaType javaType; private String javaName; private List- Provide Class information from the underlying
*JavaType
.enumValues; private JavaModel javaModel; /** * Construct a new instance of OXMJavaClassImpl
. * * @param aJavaType - the XJCJavaType
to be wrapped. */ public OXMJavaClassImpl(JavaType aJavaType) { this.javaType = aJavaType; } /** * Construct a new instance ofOXMJavaClassImpl
. * * @param aJavaTypeName - the name of the JavaType to create. */ public OXMJavaClassImpl(String aJavaTypeName) { this.javaName = aJavaTypeName; } /** * Construct a new instance ofOXMJavaClassImpl
* representing a Javaenum
. * * @param aJavaTypeName - the name of the JavaType to create. * @param enumValues - the list of values for thisenum
. */ public OXMJavaClassImpl(String aJavaTypeName, ListenumValues) { this.javaName = aJavaTypeName; this.enumValues = enumValues; } // ======================================================================== /** * Return the "actual" type from a parameterized type. For example, if this * JavaClass
representsList<Employee
, this method will return the *Employee
JavaClass
. * * @return aCollection
containing the actual type'sJavaClass
. */ public CollectiongetActualTypeArguments() { Object jType = null; if (this.javaType != null) { jType = this.javaType; } else { try { Class> jTypeClass = PrivilegedAccessHelper.getClassForName(this.javaName); jType = PrivilegedAccessHelper.newInstanceFromClass(jTypeClass); } catch (Exception e) { return new ArrayList (); } } ArrayList argCollection = new ArrayList (); if (jType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) jType; Type[] params = pType.getActualTypeArguments(); for (Type type : params) { if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; argCollection.add(this.javaModel.getClass(pt.getRawType().getClass())); } else if (type instanceof WildcardType) { Type[] upperTypes = ((WildcardType) type).getUpperBounds(); if (upperTypes.length >0) { Type upperType = upperTypes[0]; if (upperType instanceof Class>) { argCollection.add(this.javaModel.getClass(upperType.getClass())); } } } else if (type instanceof Class>) { argCollection.add(this.javaModel.getClass(type.getClass())); } else if (type instanceof GenericArrayType) { Class> genericTypeClass = (Class>) ((GenericArrayType) type).getGenericComponentType(); genericTypeClass = java.lang.reflect.Array.newInstance(genericTypeClass, 0).getClass(); argCollection.add(this.javaModel.getClass(genericTypeClass.getClass())); } } } return argCollection; } /** * If this JavaClass
is an array type, return the type of the * array components. * * @return always returnsnull
, asJavaTypes
do not represent arrays. */ public JavaClass getComponentType() { return null; } /** * Return theJavaConstructor
for thisJavaClass
that has the * provided parameter types. * * @param parameterTypes the parameter list used to identify the constructor. * * @return theJavaConstructor
with the signature matching parameterTypes. */ public JavaConstructor getConstructor(JavaClass[] parameterTypes) { return new OXMJavaConstructorImpl(this); } /** * Return all of theJavaConstructors
for this JavaClass. * * @return ACollection
containing thisJavaClass'
JavaConstructors
. */ public CollectiongetConstructors() { ArrayList constructors = new ArrayList (1); constructors.add(new OXMJavaConstructorImpl(this)); return constructors; } /** * Return this JavaClass'
inner classes. * * @return always returns an emptyArrayList
asJavaTypes
do not represent inner classes. */ public CollectiongetDeclaredClasses() { return new ArrayList (); } /** * Return the declared JavaConstructor
for thisJavaClass
. * * @return theJavaConstructor
for thisJavaClass
. */ public JavaConstructor getDeclaredConstructor(JavaClass[] parameterTypes) { return new OXMJavaConstructorImpl(this); } /** * Return all of the declaredJavaConstructors
for thisJavaClass
. * * @return ACollection
containing thisJavaClass'
JavaConstructors
. */ public CollectiongetDeclaredConstructors() { ArrayList constructors = new ArrayList (1); constructors.add(new OXMJavaConstructorImpl(this)); return constructors; } /** * Return the declared JavaField
for thisJavaClass
, identified * byfieldName
. * * @param name the name of theJavaField
to return. * * @return theJavaField
namedfieldName
from thisJavaClass
. */ public JavaField getDeclaredField(String name) { CollectionallFields = getDeclaredFields(); for (Iterator iterator = allFields.iterator(); iterator.hasNext();) { JavaField field = iterator.next(); if (field.getName().equals(name)) { return field; } } return null; } /** * Return all of the declared JavaFields
for thisJavaClass
. * * @return ACollection
containing thisJavaClass'
JavaFields
. */ public CollectiongetDeclaredFields() { List fieldsToReturn = new ArrayList (); if (this.enumValues != null) { for (Iterator iterator = this.enumValues.iterator(); iterator.hasNext();) { fieldsToReturn.add(new OXMJavaFieldImpl(iterator.next(), JAVA_LANG_OBJECT, this)); } } else { JavaAttributes javaAttributes = this.javaType.getJavaAttributes(); if(null != javaAttributes) { List > fields = javaAttributes.getJavaAttribute(); for (Iterator > iterator = fields.iterator(); iterator.hasNext();) { JAXBElement extends JavaAttribute> jaxbElement = iterator.next(); JavaAttribute att = (JavaAttribute) jaxbElement.getValue(); if (att instanceof XmlElement) { XmlElement xme = (XmlElement) att; String fieldName = xme.getJavaAttribute(); String fieldType = xme.getType(); fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlElements) { XmlElements xmes = (XmlElements) att; String fieldName = xmes.getJavaAttribute(); String fieldType = JAVA_LANG_OBJECT; fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlElementRef) { XmlElementRef xmer = (XmlElementRef) att; String fieldName = xmer.getJavaAttribute(); String fieldType = xmer.getType(); fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlAttribute) { XmlAttribute xma = (XmlAttribute) att; String fieldName = xma.getJavaAttribute(); String fieldType = xma.getType(); fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlValue) { XmlValue xmv = (XmlValue) att; String fieldName = xmv.getJavaAttribute(); String fieldType = xmv.getType(); fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlAnyElement) { XmlAnyElement xmae = (XmlAnyElement) att; String fieldName = xmae.getJavaAttribute(); String fieldType = JAVA_LANG_OBJECT; fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlAnyAttribute) { XmlAnyAttribute xmaa = (XmlAnyAttribute) att; String fieldName = xmaa.getJavaAttribute(); String fieldType = JAVA_UTIL_MAP; fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlJoinNodes) { XmlJoinNodes xmjn = (XmlJoinNodes) att; String fieldName = xmjn.getJavaAttribute(); String fieldType = xmjn.getType(); fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } else if (att instanceof XmlInverseReference) { XmlInverseReference xmir = (XmlInverseReference) att; String fieldName = xmir.getJavaAttribute(); String fieldType = xmir.getType(); fieldsToReturn.add(new OXMJavaFieldImpl(fieldName, fieldType, this)); } } } } return fieldsToReturn; } /** * Return the declared JavaMethod
for thisJavaClass
, * identified byname
, with the signature matchingargs
. * * @param name the name of theJavaMethod
to return. * @param args the parameter list used to identify the method. * * @return always returnsnull
, asJavaTypes
do not have methods. */ public JavaMethod getDeclaredMethod(String name, JavaClass[] args) { return null; } /** * Return all of the declaredJavaMethods
for thisJavaClass
. * * @return always returns an emptyArrayList
, asJavaTypes
do not have methods. */ public CollectiongetDeclaredMethods() { return new ArrayList (); } /** * Return the JavaMethod
for thisJavaClass
, * identified byname
, with the signature matchingargs
. * * @param name the name of theJavaMethod
to return. * @param args the parameter list used to identify the method. * * @return always returnsnull
, asJavaTypes
do not have methods. */ public JavaMethod getMethod(String name, JavaClass[] args) { return null; } /** * Return all of theJavaMethods
for thisJavaClass
. * * @return always returns an emptyArrayList
, asJavaTypes
do not have methods. */ public CollectiongetMethods() { return new ArrayList (); } /** * Returns the Java language modifiers for this JavaClass
, encoded in an integer. * * @return always returns0
asJavaTypes
do not have modifiers. * * @see java.lang.reflect.Modifier */ public int getModifiers() { return 0; } /** * Returns the name of thisJavaClass
. * * @return theString
name of thisJavaClass
. */ public String getName() { if (this.javaType != null) { return this.javaType.getName(); } return this.javaName; } /** * Returns theJavaPackage
that thisJavaClass
belongs to. * * @return theJavaPackage
of thisJavaClass
. */ public JavaPackage getPackage() { return new OXMJavaPackageImpl(getPackageName()); } /** * Returns the package name of thisJavaClass
. * * @return theString
name of thisJavaClass'
JavaPackage
. */ public String getPackageName() { int lastDotIndex = getQualifiedName().lastIndexOf(DOT); if (lastDotIndex == -1) { return EMPTY_STRING; } return getQualifiedName().substring(0, lastDotIndex); } /** * Returns the fully-qualified name of thisJavaClass
. * * @return theString
name of thisJavaClass
. */ public String getQualifiedName() { return getName(); } /** * Returns the raw name of thisJavaClass
. Array types will * have "[]" appended to the name. * * @return theString
raw name of thisJavaClass
. */ public String getRawName() { return getName(); } /** * Returns the super class of thisJavaClass
. * * @returnJavaClass
representing the super class of thisJavaClass
. */ public JavaClass getSuperclass() { if (this.javaModel == null) { return null; } if (this.javaType != null) { if (!(this.javaType.getSuperType().equals(XMLProcessor.DEFAULT))) { return this.javaModel.getClass(javaType.getSuperType()); } } return this.javaModel.getClass(JAVA_LANG_OBJECT); } @Override public Type[] getGenericInterfaces() { return new Type[0]; } public Type getGenericSuperclass() { return null; } /** * Indicates if thisJavaClass
has actual type arguments, i.e. is a * parameterized type (for example,List<Employee
). * * @return always returnsfalse
asJavaTypes
are not parameterized. */ public boolean hasActualTypeArguments() { return false; } /** * Indicates if thisJavaClass
isabstract
. * * @return always returnsfalse
asJavaTypes
are neverabstract
. */ public boolean isAbstract() { return false; } /** * Indicates if thisJavaClass
is anAnnotation
. * * @return always returnsfalse
asJavaTypes
are neverAnnotations
. */ public boolean isAnnotation() { return false; } /** * Indicates if thisJavaClass
is an Array type. * * @return always returnsfalse
, asJavaTypes
do not represent arrays. */ public boolean isArray() { return false; } /** * Indicates if thisJavaClass
is either the same as, or is a superclass of, * thejavaClass
argument. * * @param javaClass theClass
to test. * * @returntrue
if thisJavaClass
is assignable from *javaClass
, otherwisefalse
. * * @see java.lang.Class#isAssignableFrom(Class) */ @SuppressWarnings("unchecked") public boolean isAssignableFrom(JavaClass arg0) { String thisJavaName = EMPTY_STRING; String argJavaName = arg0.getName(); if (this.javaName != null) { thisJavaName = this.javaName; } else { thisJavaName = this.javaType.getName(); } if (thisJavaName.startsWith(JAVA) && argJavaName.startsWith(JAVA)) { // Only try class lookup if this is a JDK class, because // we won't ever find classes for dynamically generated types. try { Class thisClass = PrivilegedAccessHelper.getClassForName(thisJavaName); Class argClass = PrivilegedAccessHelper.getClassForName(argJavaName); return thisClass.isAssignableFrom(argClass); } catch (Exception e) { return false; } } else { return thisJavaName.equals(argJavaName); } } /** * Indicates if thisJavaClass
is anenum
. * * @returntrue
if thisJavaClass
is anenum
, otherwisefalse
. */ public boolean isEnum() { return this.enumValues != null; } /** * Indicates if thisJavaClass
isfinal
. * * @returntrue
if thisJavaClass
isfinal
, otherwisefalse
. */ public boolean isFinal() { return false; } /** * Indicates if thisJavaClass
is aninterface
. * * @returntrue
if thisJavaClass
is aninterface
, otherwisefalse
. */ public boolean isInterface() { return false; } /** * Indicates if thisJavaClass
is an innerClass
. * * @returntrue
if thisJavaClass
is an innerfalse
. */ public boolean isMemberClass() { return false; } /** * Indicates if thisJavaClass
represents a primitive type. * * @returntrue
if thisJavaClass
represents a primitive type, otherwisefalse
. */ public boolean isPrimitive() { return false; } /** * Indicates if thisJavaClass
isprivate
. * * @returntrue
if thisJavaClass
isprivate
, otherwisefalse
. */ public boolean isPrivate() { return false; } /** * Indicates if thisJavaClass
isprotected
. * * @returntrue
if thisJavaClass
isprotected
, otherwisefalse
. */ public boolean isProtected() { return false; } /** * Indicates if thisJavaClass
ispublic
. * * @returntrue
if thisJavaClass
ispublic
, otherwisefalse
. */ public boolean isPublic() { return false; } /** * Indicates if thisJavaClass
isstatic
. * * @returntrue
if thisJavaClass
isstatic
, otherwisefalse
. */ public boolean isStatic() { return false; } /** * Not supported. */ public boolean isSynthetic() { throw new UnsupportedOperationException("isSynthetic"); } /** * If thisJavaClass
is annotated with anAnnotation
matchingaClass
, * return itsJavaAnnotation
representation. * * @param aClass aJavaClass
representing theAnnotation
to look for. * * @return always returnsnull
, asJavaTypes
do not haveAnnotations
. */ public JavaAnnotation getAnnotation(JavaClass aClass) { return null; } /** * Return all of theAnnotations
for thisJavaClass
. * * @return always returns an emptyArrayList
, asJavaTypes
do not haveAnnotations
. */ public CollectiongetAnnotations() { return new ArrayList (); } /** * If this JavaClass
declares anAnnotation
matchingaClass
, * return itsJavaAnnotation
representation. * * @param aClass aJavaClass
representing theAnnotation
to look for. * * @return always returnsnull
, asJavaTypes
do not haveAnnotations
. */ public JavaAnnotation getDeclaredAnnotation(JavaClass arg0) { return null; } /** * Return all of the declaredAnnotations
for thisJavaClass
. * * @return always returns an emptyArrayList
, asJavaTypes
do not haveAnnotations
. */ public CollectiongetDeclaredAnnotations() { return new ArrayList (); } /** * Get this JavaClass'
JavaModel
. * * @return TheJavaModel
associated with thisJavaClass
. */ public void setJavaModel(JavaModel model) { this.javaModel = model; } /** * Set this
JavaClass'
JavaModel
. * * @param javaModel TheJavaModel
to set. */ public JavaModel getJavaModel() { return this.javaModel; } // ======================================================================== private static String EMPTY_STRING = ""; private static String JAVA = "java"; private static String DOT = "."; private static String JAVA_LANG_OBJECT = "java.lang.Object"; private static String JAVA_UTIL_MAP = "java.util.Map"; }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy