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

Interface Navigator<T,C,F,M>

public interface Navigator<T,C,F,M>

Provides unified view of the underlying reflection library, such as {@code java.lang.reflect} and/or APT.

This interface provides navigation over the reflection model to decouple the caller from any particular implementation. This allows the JAXB RI to reuse much of the code between the compile time (which works on top of APT) and the run-time (which works on top of {@code java.lang.reflect})

Navigator instances are stateless and immutable.

Parameterization

C

A Java class declaration (not an interface, a class and an enum.)

T

A Java type. This includs declaration, but also includes such things like arrays, primitive types, parameterized types, and etc.

Field Summary
static ReflectionNavigatorREFLECTION
The singleton instance.
Method Summary
CasDecl(T type)
If the given type is an use of class declaration, returns the type casted as {@code C}.
CasDecl(Class c)
Gets the {@code C} representation for the given class.
<P> Terasure(T contentInMemoryType)
Computes the erasure
CfindClass(String className, C referencePoint)
Finds the class/interface/enum/annotation of the given name.
TgetBaseClass(T type, C baseType)
Gets the parameterization of the given base type.
LocationgetClassLocation(C clazz)
Returns a location of the specified class.
StringgetClassName(C clazz)
Gets the fully-qualified name of the class.
StringgetClassShortName(C clazz)
Gets the short name of the class ("Object" for Object.)
TgetComponentType(T t)
Gets the component type of the array.
FgetDeclaredField(C clazz, String fieldName)
Gets the named field declared on the given class.
Collection<? extends F>getDeclaredFields(C clazz)
Gets all the declared fields of the given class.
Collection<? extends M>getDeclaredMethods(C 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.)
CgetDeclaringClassForField(F field)
Gets the class that declares the given field.
CgetDeclaringClassForMethod(M method)
Gets the class that declares the given method.
F[]getEnumConstants(C clazz)
Gets the enumeration constants from an enum class.
LocationgetFieldLocation(F field)
StringgetFieldName(F field)
Gets the name of the field.
TgetFieldType(F f)
Gets the type of the field.
LocationgetMethodLocation(M getter)
StringgetMethodName(M m)
Gets the name of the method, such as "toString" or "equals".
T[]getMethodParameters(M method)
Returns the list of parameters to the method.
StringgetPackageName(C clazz)
Gets the package name of the given class.
TgetPrimitive(Class primitiveType)
Returns the representation for the given primitive type.
TgetReturnType(M m)
Gets the return type of a method.
CgetSuperClass(C clazz)
Gets the base class of the specified class.
TgetTypeArgument(T t, int i)
Gets the i-th type argument from a parameterized type.
StringgetTypeName(T rawType)
Gets the display name of the type object
TgetVoidType()
Gets the representation of the primitive "void" type.
booleanhasDefaultConstructor(C clazz)
Returns true if the given class has a no-arg default constructor.
booleanisAbstract(C clazz)
Returns true if this is an abstract class.
booleanisArray(T t)
Checks if the type is an array type.
booleanisArrayButNotByteArray(T t)
Checks if the type is an array type but not byte[].
booleanisBridgeMethod(M method)
Returns true if this method is a bridge method as defined in JLS.
booleanisEnum(C clazz)
Returns true if this is an enum class.
booleanisFinal(C clazz)
Returns true if this is a final class.
booleanisFinalMethod(M method)
Returns true if the method is final.
booleanisInnerClass(C clazz)
Returns true if the given class is an inner class.
booleanisInterface(C clazz)
Returns true if 'clazz' is an interface.
booleanisOverriding(M method, C base)
Returns true if the given method is overriding another one defined in the base class 'base' or its ancestors.
booleanisParameterizedType(T t)
Returns true if t is a parameterized type.
booleanisPrimitive(T t)
Checks if the given type is a primitive type.
booleanisPublicField(F field)
Returns true if the field is public.
booleanisPublicMethod(M method)
Returns true if the method is public.
booleanisStaticField(F field)
Returns true if the field is static.
booleanisStaticMethod(M method)
Returns true if the method is static.
booleanisSubClassOf(T sub, T sup)
Checks if {@code sub} is a sub-type of {@code sup}.
booleanisTransient(F f)
Returns true if the field is transient.
Tref(Class c)
Gets the representation of the given Java type in {@code T}.
Tuse(C c)
Gets the T for the given C.

Field Detail

REFLECTION

public static final ReflectionNavigator REFLECTION
The singleton instance.

Method Detail

asDecl

public C asDecl(T type)
If the given type is an use of class declaration, returns the type casted as {@code C}. Otherwise null.

TODO: define the exact semantics.

asDecl

public C asDecl(Class c)
Gets the {@code 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.

erasure

public <P> T erasure(T contentInMemoryType)
Computes the erasure

findClass

public C findClass(String className, C referencePoint)
Finds the class/interface/enum/annotation of the given name.

Parameters: referencePoint The class that refers to the specified class.

Returns: null if not found.

getBaseClass

public T getBaseClass(T type, C baseType)
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>
 

Parameters: type The type that derives from {@code baseType} baseType The class whose parameterization we are interested in.

Returns: The use of {@code baseType} in {@code type}. or null if the type is not assignable to the base type.

getClassLocation

public Location getClassLocation(C clazz)
Returns a location of the specified class.

getClassName

public String getClassName(C clazz)
Gets the fully-qualified name of the class. ("java.lang.Object" for Object)

getClassShortName

public String getClassShortName(C clazz)
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".

getComponentType

public T getComponentType(T t)
Gets the component type of the array.

Parameters: t must be an array.

getDeclaredField

public F getDeclaredField(C clazz, String fieldName)
Gets the named field declared on the given class. This method doesn't visit ancestors, but does recognize non-public fields.

Returns: null if not found

getDeclaredFields

public Collection<? extends F> getDeclaredFields(C clazz)
Gets all the declared fields of the given class.

getDeclaredMethods

public Collection<? extends M> getDeclaredMethods(C 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.)

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

Returns: can be empty but always non-null.

getDeclaringClassForField

public C getDeclaringClassForField(F field)
Gets the class that declares the given field.

getDeclaringClassForMethod

public C getDeclaringClassForMethod(M method)
Gets the class that declares the given method.

getEnumConstants

public F[] getEnumConstants(C clazz)
Gets the enumeration constants from an enum class.

Parameters: clazz must derive from Enum.

Returns: can be empty but never null.

getFieldLocation

public Location getFieldLocation(F field)

getFieldName

public String getFieldName(F field)
Gets the name of the field.

getFieldType

public T getFieldType(F f)
Gets the type of the field.

getMethodLocation

public Location getMethodLocation(M getter)

getMethodName

public String getMethodName(M m)
Gets the name of the method, such as "toString" or "equals".

getMethodParameters

public T[] getMethodParameters(M method)
Returns the list of parameters to the method.

getPackageName

public String getPackageName(C clazz)
Gets the package name of the given class.

Returns: i.e. "", "java.lang" but not null.

getPrimitive

public T getPrimitive(Class primitiveType)
Returns the representation for the given primitive type.

Parameters: primitiveType must be Class objects like Integer#TYPE.

getReturnType

public T getReturnType(M m)
Gets the return type of a method.

getSuperClass

public C getSuperClass(C clazz)
Gets the base class of the specified class.

Returns: null if the parameter represents Object.

getTypeArgument

public T getTypeArgument(T t, int i)
Gets the i-th type argument from a parameterized type. For example, {@code getTypeArgument([Map],0)=Integer}

Throws: IllegalArgumentException If t is not a parameterized type IndexOutOfBoundsException If i is out of range.

See Also: Navigator

getTypeName

public String getTypeName(T rawType)
Gets the display name of the type object

Returns: a human-readable name that the type represents.

getVoidType

public T getVoidType()
Gets the representation of the primitive "void" type.

hasDefaultConstructor

public boolean hasDefaultConstructor(C clazz)
Returns true if the given class has a no-arg default constructor. The constructor does not need to be public.

isAbstract

public boolean isAbstract(C clazz)
Returns true if this is an abstract class.

isArray

public boolean isArray(T t)
Checks if the type is an array type.

isArrayButNotByteArray

public boolean isArrayButNotByteArray(T t)
Checks if the type is an array type but not byte[].

isBridgeMethod

public boolean isBridgeMethod(M method)
Returns true if this method is a bridge method as defined in JLS.

isEnum

public boolean isEnum(C clazz)
Returns true if this is an enum class.

isFinal

public boolean isFinal(C clazz)
Returns true if this is a final class.

isFinalMethod

public boolean isFinalMethod(M method)
Returns true if the method is final.

isInnerClass

public boolean isInnerClass(C clazz)
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.)

isInterface

public boolean isInterface(C clazz)
Returns true if 'clazz' is an interface.

isOverriding

public boolean isOverriding(M method, C base)
Returns true if the given method is overriding another one defined in the base class 'base' or its ancestors.

isParameterizedType

public boolean isParameterizedType(T t)
Returns true if t is a parameterized type.

isPrimitive

public boolean isPrimitive(T t)
Checks if the given type is a primitive type.

isPublicField

public boolean isPublicField(F field)
Returns true if the field is public.

isPublicMethod

public boolean isPublicMethod(M method)
Returns true if the method is public.

isStaticField

public boolean isStaticField(F field)
Returns true if the field is static.

isStaticMethod

public boolean isStaticMethod(M method)
Returns true if the method is static.

isSubClassOf

public boolean isSubClassOf(T sub, T sup)
Checks if {@code sub} is a sub-type of {@code sup}. TODO: should this method take T or C?

isTransient

public boolean isTransient(F f)
Returns true if the field is transient.

ref

public T ref(Class c)
Gets the representation of the given Java type in {@code T}.

Parameters: c can be a primitive, array, class, or anything. (therefore the return type has to be T, not C)

use

public T use(C c)
Gets the T for the given C.