com.sun.xml.bind.v2.model.nav
Class ReflectionNavigator

java.lang.Object
  extended by com.sun.xml.bind.v2.model.nav.ReflectionNavigator
All Implemented Interfaces:
Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

public final class ReflectionNavigator
extends java.lang.Object
implements Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

Navigator implementation for java.lang.reflect.


Field Summary
 
Fields inherited from interface com.sun.xml.bind.v2.model.nav.Navigator
REFLECTION
 
Method Summary
 java.lang.Class asDecl(java.lang.Class c)
          Gets the C representation for the given class.
 java.lang.Class asDecl(java.lang.reflect.Type t)
          If the given type is an use of class declaration, returns the type casted as C.
 java.lang.reflect.Type createParameterizedType(java.lang.Class rawType, java.lang.reflect.Type... arguments)
          Returns the Type object that represents clazz&lt;T1,T2,T3>.
<T> java.lang.Class<T>
erasure(java.lang.reflect.Type t)
          Returns the runtime representation of the given type.
 java.lang.Class findClass(java.lang.String className, java.lang.Class referencePoint)
          Finds the class/interface/enum/annotation of the given name.
 java.lang.reflect.Type getBaseClass(java.lang.reflect.Type t, java.lang.Class sup)
          Gets the parameterization of the given base type.
 Location getClassLocation(java.lang.Class clazz)
          Returns a location of the specified class.
 java.lang.String getClassName(java.lang.Class clazz)
          Gets the fully-qualified name of the class.
 java.lang.String getClassShortName(java.lang.Class clazz)
          Gets the short name of the class ("Object" for Object.) For nested classes, this method should just return the inner name.
 java.lang.reflect.Type getComponentType(java.lang.reflect.Type t)
          Gets the component type of the array.
 java.lang.reflect.Field getDeclaredField(java.lang.Class clazz, java.lang.String fieldName)
          Gets the named field declared on the given class.
 java.util.Collection<? extends java.lang.reflect.Field> getDeclaredFields(java.lang.Class clazz)
          Gets all the declared fields of the given class.
 java.util.Collection<? extends java.lang.reflect.Method> getDeclaredMethods(java.lang.Class clazz)
          Gets all the declared methods of the given class (regardless of their access modifiers, regardless of whether they override methods of the base classes.)
 java.lang.Class getDeclaringClassForField(java.lang.reflect.Field field)
          Gets the class that declares the given field.
 java.lang.Class getDeclaringClassForMethod(java.lang.reflect.Method method)
          Gets the class that declares the given method.
 java.lang.reflect.Field[] getEnumConstants(java.lang.Class clazz)
          Gets the enumeration constants from an enum class.
 Location getFieldLocation(java.lang.reflect.Field field)
           
 java.lang.String getFieldName(java.lang.reflect.Field field)
          Gets the name of the field.
 java.lang.reflect.Type getFieldType(java.lang.reflect.Field field)
          Gets the type of the field.
 Location getMethodLocation(java.lang.reflect.Method method)
           
 java.lang.String getMethodName(java.lang.reflect.Method method)
          Gets the name of the method, such as "toString" or "equals".
 java.lang.reflect.Type[] getMethodParameters(java.lang.reflect.Method method)
          Returns the list of parameters to the method.
 java.lang.String getPackageName(java.lang.Class clazz)
          Gets the package name of the given class.
 java.lang.reflect.Type getPrimitive(java.lang.Class primitiveType)
          Returns the representation for the given primitive type.
 java.lang.reflect.Type getReturnType(java.lang.reflect.Method method)
          Gets the return type of a method.
 java.lang.Class getSuperClass(java.lang.Class clazz)
          Gets the base class of the specified class.
 java.lang.reflect.Type getTypeArgument(java.lang.reflect.Type type, int i)
          Gets the i-th type argument from a parameterized type.
 java.lang.String getTypeName(java.lang.reflect.Type type)
          Gets the display name of the type object
 java.lang.reflect.Type getVoidType()
          Gets the representation of the primitive "void" type.
 boolean hasDefaultConstructor(java.lang.Class c)
          Returns true if the given class has a no-arg default constructor.
 boolean isAbstract(java.lang.Class clazz)
          Returns true if this is an abstract class.
 boolean isArray(java.lang.reflect.Type t)
          Checks if the type is an array type.
 boolean isArrayButNotByteArray(java.lang.reflect.Type t)
          Checks if the type is an array type but not byte[].
 boolean isBridgeMethod(java.lang.reflect.Method method)
          Returns true if this method is a bridge method as defined in JLS.
 boolean isEnum(java.lang.Class c)
          Returns true if this is an enum class.
 boolean isFinal(java.lang.Class clazz)
          Returns true if this is a final class.
 boolean isFinalMethod(java.lang.reflect.Method method)
          Returns true if the method is final.
 boolean isInnerClass(java.lang.Class clazz)
          Returns true if the given class is an inner class.
 boolean isInterface(java.lang.Class clazz)
          Returns true if 'clazz' is an interface.
 boolean isOverriding(java.lang.reflect.Method method, java.lang.Class base)
          Returns true if the given method is overriding another one defined in the base class 'base' or its ancestors.
 boolean isParameterizedType(java.lang.reflect.Type type)
          Returns true if t is a parameterized type.
 boolean isPrimitive(java.lang.reflect.Type type)
          Checks if the given type is a primitive type.
 boolean isPublicField(java.lang.reflect.Field field)
          Returns true if the field is public.
 boolean isPublicMethod(java.lang.reflect.Method method)
          Returns true if the method is public.
 boolean isStaticField(java.lang.reflect.Field field)
          Returns true if the field is static.
 boolean isStaticMethod(java.lang.reflect.Method method)
          Returns true if the method is static.
 boolean isSubClassOf(java.lang.reflect.Type sub, java.lang.reflect.Type sup)
          Checks if sub is a sub-type of sup.
 boolean isTransient(java.lang.reflect.Field f)
          Returns true if the field is transient.
 java.lang.Class ref(java.lang.Class c)
          Gets the representation of the given Java type in T.
 java.lang.Class use(java.lang.Class c)
          Gets the T for the given C.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getSuperClass

public java.lang.Class getSuperClass(java.lang.Class clazz)
Description copied from interface: Navigator
Gets the base class of the specified class.

Specified by:
getSuperClass in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Returns:
null if the parameter represents Object.

getBaseClass

public java.lang.reflect.Type getBaseClass(java.lang.reflect.Type t,
                                           java.lang.Class sup)
Description copied from interface: Navigator
Gets the parameterization of the given base type.

For example, given the following


 interface Foo<T> extends List<List<T>> {}
 interface Bar extends Foo<String> {}
 
This method works like this:

 getBaseClass( Bar, List ) = List<List<String>
 getBaseClass( Bar, Foo  ) = Foo<String>
 getBaseClass( Foo<? extends Number>, Collection ) = Collection<List<? extends Number>>
 getBaseClass( ArrayList<? extends BigInteger>, List ) = List<? extends BigInteger>
 

Specified by:
getBaseClass in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Parameters:
t - The type that derives from baseType
sup - The class whose parameterization we are interested in.
Returns:
The use of baseType in type. or null if the type is not assignable to the base type.

getClassName

public java.lang.String getClassName(java.lang.Class clazz)
Description copied from interface: Navigator
Gets the fully-qualified name of the class. ("java.lang.Object" for Object)

Specified by:
getClassName in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getTypeName

public java.lang.String getTypeName(java.lang.reflect.Type type)
Description copied from interface: Navigator
Gets the display name of the type object

Specified by:
getTypeName in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Returns:
a human-readable name that the type represents.

getClassShortName

public java.lang.String getClassShortName(java.lang.Class clazz)
Description copied from interface: Navigator
Gets the short name of the class ("Object" for Object.) For nested classes, this method should just return the inner name. (for example "Inner" for "com.acme.Outer$Inner".

Specified by:
getClassShortName in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getDeclaredFields

public java.util.Collection<? extends java.lang.reflect.Field> getDeclaredFields(java.lang.Class clazz)
Description copied from interface: Navigator
Gets all the declared fields of the given class.

Specified by:
getDeclaredFields in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getDeclaredField

public java.lang.reflect.Field getDeclaredField(java.lang.Class clazz,
                                                java.lang.String fieldName)
Description copied from interface: Navigator
Gets the named field declared on the given class. This method doesn't visit ancestors, but does recognize non-public fields.

Specified by:
getDeclaredField in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Returns:
null if not found

getDeclaredMethods

public java.util.Collection<? extends java.lang.reflect.Method> getDeclaredMethods(java.lang.Class clazz)
Description copied from interface: Navigator
Gets all the declared methods of the given class (regardless of their access modifiers, regardless of whether they override methods of the base classes.)

Note that this method does not list methods declared on base classes.

Specified by:
getDeclaredMethods in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Returns:
can be empty but always non-null.

getDeclaringClassForField

public java.lang.Class getDeclaringClassForField(java.lang.reflect.Field field)
Description copied from interface: Navigator
Gets the class that declares the given field.

Specified by:
getDeclaringClassForField in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getDeclaringClassForMethod

public java.lang.Class getDeclaringClassForMethod(java.lang.reflect.Method method)
Description copied from interface: Navigator
Gets the class that declares the given method.

Specified by:
getDeclaringClassForMethod in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getFieldType

public java.lang.reflect.Type getFieldType(java.lang.reflect.Field field)
Description copied from interface: Navigator
Gets the type of the field.

Specified by:
getFieldType in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getFieldName

public java.lang.String getFieldName(java.lang.reflect.Field field)
Description copied from interface: Navigator
Gets the name of the field.

Specified by:
getFieldName in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getMethodName

public java.lang.String getMethodName(java.lang.reflect.Method method)
Description copied from interface: Navigator
Gets the name of the method, such as "toString" or "equals".

Specified by:
getMethodName in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getReturnType

public java.lang.reflect.Type getReturnType(java.lang.reflect.Method method)
Description copied from interface: Navigator
Gets the return type of a method.

Specified by:
getReturnType in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getMethodParameters

public java.lang.reflect.Type[] getMethodParameters(java.lang.reflect.Method method)
Description copied from interface: Navigator
Returns the list of parameters to the method.

Specified by:
getMethodParameters in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isStaticMethod

public boolean isStaticMethod(java.lang.reflect.Method method)
Description copied from interface: Navigator
Returns true if the method is static.

Specified by:
isStaticMethod in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isFinalMethod

public boolean isFinalMethod(java.lang.reflect.Method method)
Description copied from interface: Navigator
Returns true if the method is final.

Specified by:
isFinalMethod in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isSubClassOf

public boolean isSubClassOf(java.lang.reflect.Type sub,
                            java.lang.reflect.Type sup)
Description copied from interface: Navigator
Checks if sub is a sub-type of sup. TODO: should this method take T or C?

Specified by:
isSubClassOf in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

ref

public java.lang.Class ref(java.lang.Class c)
Description copied from interface: Navigator
Gets the representation of the given Java type in T.

Specified by:
ref in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Parameters:
c - can be a primitive, array, class, or anything. (therefore the return type has to be T, not C)

use

public java.lang.Class use(java.lang.Class c)
Description copied from interface: Navigator
Gets the T for the given C.

Specified by:
use in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

asDecl

public java.lang.Class asDecl(java.lang.reflect.Type t)
Description copied from interface: Navigator
If the given type is an use of class declaration, returns the type casted as C. Otherwise null.

TODO: define the exact semantics.

Specified by:
asDecl in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

asDecl

public java.lang.Class asDecl(java.lang.Class c)
Description copied from interface: Navigator
Gets the C representation for the given class. The behavior is undefined if the class object represents primitives, arrays, and other types that are not class declaration.

Specified by:
asDecl in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

erasure

public <T> java.lang.Class<T> erasure(java.lang.reflect.Type t)
Returns the runtime representation of the given type. This corresponds to the notion of the erasure in JSR-14.

Because of the difference in the way APT and the Java reflection treats primitive type and array type, we can't define this method on Navigator.

It made me realize how difficult it is to define the common navigation layer for two different underlying reflection library. The other way is to throw away the entire parameterization and go to the wrapper approach.

Specified by:
erasure in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isAbstract

public boolean isAbstract(java.lang.Class clazz)
Description copied from interface: Navigator
Returns true if this is an abstract class.

Specified by:
isAbstract in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isFinal

public boolean isFinal(java.lang.Class clazz)
Description copied from interface: Navigator
Returns true if this is a final class.

Specified by:
isFinal in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

createParameterizedType

public java.lang.reflect.Type createParameterizedType(java.lang.Class rawType,
                                                      java.lang.reflect.Type... arguments)
Returns the Type object that represents clazz&lt;T1,T2,T3>.


isArray

public boolean isArray(java.lang.reflect.Type t)
Description copied from interface: Navigator
Checks if the type is an array type.

Specified by:
isArray in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isArrayButNotByteArray

public boolean isArrayButNotByteArray(java.lang.reflect.Type t)
Description copied from interface: Navigator
Checks if the type is an array type but not byte[].

Specified by:
isArrayButNotByteArray in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getComponentType

public java.lang.reflect.Type getComponentType(java.lang.reflect.Type t)
Description copied from interface: Navigator
Gets the component type of the array.

Specified by:
getComponentType in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Parameters:
t - must be an array.

getTypeArgument

public java.lang.reflect.Type getTypeArgument(java.lang.reflect.Type type,
                                              int i)
Description copied from interface: Navigator
Gets the i-th type argument from a parameterized type. For example, getTypeArgument([Map<Integer,String>],0)=Integer

Specified by:
getTypeArgument in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
See Also:
Navigator.isParameterizedType(Object)

isParameterizedType

public boolean isParameterizedType(java.lang.reflect.Type type)
Description copied from interface: Navigator
Returns true if t is a parameterized type.

Specified by:
isParameterizedType in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isPrimitive

public boolean isPrimitive(java.lang.reflect.Type type)
Description copied from interface: Navigator
Checks if the given type is a primitive type.

Specified by:
isPrimitive in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getPrimitive

public java.lang.reflect.Type getPrimitive(java.lang.Class primitiveType)
Description copied from interface: Navigator
Returns the representation for the given primitive type.

Specified by:
getPrimitive in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Parameters:
primitiveType - must be Class objects like Integer.TYPE.

getClassLocation

public Location getClassLocation(java.lang.Class clazz)
Description copied from interface: Navigator
Returns a location of the specified class.

Specified by:
getClassLocation in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getFieldLocation

public Location getFieldLocation(java.lang.reflect.Field field)
Specified by:
getFieldLocation in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getMethodLocation

public Location getMethodLocation(java.lang.reflect.Method method)
Specified by:
getMethodLocation in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

hasDefaultConstructor

public boolean hasDefaultConstructor(java.lang.Class c)
Description copied from interface: Navigator
Returns true if the given class has a no-arg default constructor. The constructor does not need to be public.

Specified by:
hasDefaultConstructor in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isStaticField

public boolean isStaticField(java.lang.reflect.Field field)
Description copied from interface: Navigator
Returns true if the field is static.

Specified by:
isStaticField in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isPublicMethod

public boolean isPublicMethod(java.lang.reflect.Method method)
Description copied from interface: Navigator
Returns true if the method is public.

Specified by:
isPublicMethod in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isPublicField

public boolean isPublicField(java.lang.reflect.Field field)
Description copied from interface: Navigator
Returns true if the field is public.

Specified by:
isPublicField in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isEnum

public boolean isEnum(java.lang.Class c)
Description copied from interface: Navigator
Returns true if this is an enum class.

Specified by:
isEnum in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getEnumConstants

public java.lang.reflect.Field[] getEnumConstants(java.lang.Class clazz)
Description copied from interface: Navigator
Gets the enumeration constants from an enum class.

Specified by:
getEnumConstants in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Parameters:
clazz - must derive from Enum.
Returns:
can be empty but never null.

getVoidType

public java.lang.reflect.Type getVoidType()
Description copied from interface: Navigator
Gets the representation of the primitive "void" type.

Specified by:
getVoidType in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

getPackageName

public java.lang.String getPackageName(java.lang.Class clazz)
Description copied from interface: Navigator
Gets the package name of the given class.

Specified by:
getPackageName in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
Returns:
i.e. "", "java.lang" but not null.

findClass

public java.lang.Class findClass(java.lang.String className,
                                 java.lang.Class referencePoint)
Description copied from interface: Navigator
Finds the class/interface/enum/annotation of the given name.

Specified by:
findClass in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>
referencePoint - The class that refers to the specified class.
Returns:
null if not found.

isBridgeMethod

public boolean isBridgeMethod(java.lang.reflect.Method method)
Description copied from interface: Navigator
Returns true if this method is a bridge method as defined in JLS.

Specified by:
isBridgeMethod in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isOverriding

public boolean isOverriding(java.lang.reflect.Method method,
                            java.lang.Class base)
Description copied from interface: Navigator
Returns true if the given method is overriding another one defined in the base class 'base' or its ancestors.

Specified by:
isOverriding in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isInterface

public boolean isInterface(java.lang.Class clazz)
Description copied from interface: Navigator
Returns true if 'clazz' is an interface.

Specified by:
isInterface in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isTransient

public boolean isTransient(java.lang.reflect.Field f)
Description copied from interface: Navigator
Returns true if the field is transient.

Specified by:
isTransient in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>

isInnerClass

public boolean isInnerClass(java.lang.Class clazz)
Description copied from interface: Navigator
Returns true if the given class is an inner class. This is only used to improve the error diagnostics, so it's OK to fail to detect some inner classes as such. Note that this method should return false for nested classes (static classes.)

Specified by:
isInnerClass in interface Navigator<java.lang.reflect.Type,java.lang.Class,java.lang.reflect.Field,java.lang.reflect.Method>