|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.meta.reflect.Reflection
public class Reflection
Various reflection functionality.
Implementation notes:
Though all methods are declared static, instances of this class
cannot be created, and hence it is in effect an application of
the Singleton pattern.
Constructor Summary | |
---|---|
private |
Reflection()
Private off-limit constructor. |
Method Summary | ||
---|---|---|
static Class<?> |
forName(CharSequence className)
Returns the Class object associated with the class or
interface with the name supplied as className . |
|
static Set<Class<?>> |
getAssignableTo(Class<?> clazz)
Returns an ordered set of the classes and/or interfaces the Class supplied as clazz can be assigned
to. |
|
static
|
getAssignableTo(Class<?> clazz,
C classes)
Returns a collection of the classes and/or interfaces the Class supplied as clazz can be assigned
to. |
|
static Class<?> |
getCallerClass()
Returns the caller class of the calling context of the immediate calling context that invoked this method. |
|
static Class<?> |
getClass(Class<?> clazz)
Returns the first reachable named class or interface that can identify clazz . |
|
static Constructor<?> |
getConstructor(Class<?> clazz,
Class<?>... parameterTypes)
Returns the constructor declared in the class supplied as clazz accepting the formal parameter types supplied
as parameterTypes , ignoring primitive types. |
|
static
|
getConstructors(Class<?> clazz,
Class<A> annotation)
Returns the constructors declared in the class supplied as clazz
annotated with the annotation type supplied as annotation . |
|
static
|
getCopyConstructor(Class<T> clazz)
Returns the copy constructor declared in clazz , if any. |
|
static List<Field> |
getFields(Class<?> clazz)
Returns all declared fields in clazz , or in
a super-class or implemented interface. |
|
static Set<Class<?>> |
getInterfaces(Class<?> clazz)
Returns an ordered set of the interfaces the Class supplied
as clazz can be assigned to, including interfaces
implemented by super-classes of clazz . |
|
static
|
getInterfaces(Class<?> clazz,
C interfaces)
Returns an ordered set of the interfaces the Class supplied
as clazz can be assigned to, including interfaces
implemented by super-classes of clazz . |
|
static Method |
getMethod(Class<?> clazz,
String name,
Class<?>... parameterTypes)
Returns the method with the name supplied as name that
is declared in the class supplied as clazz accepting
the formal parameter types supplied as parameterTypes ,
ignoring primitive types. |
|
static List<Method> |
getMethods(Class<?> clazz)
Returns all declared methods in clazz , or in
a super-class or implemented interface. |
|
static
|
getMethods(Class<?> clazz,
Class<A> annotation)
Returns the methods declared in the class supplied as clazz
annotated with the annotation type supplied as annotation . |
|
static
|
getOverriding(Class<T> clazz,
String name,
Class<?>... types)
Returns a (key,value) placeholder, where the key is the first super class of clazz , or clazz it self, declaring a non-abstract method with
the name supplied as name and the formal parameter types supplied as types ,
providing it is accessible to clazz , and where the value is the first super
class of that class declaring a similar method. |
|
static
|
getSuperClasses(Class<T> clazz)
Returns a list of all super-classes of clazz ,
excluding clazz , where the last index is always
be Object.class . |
|
static boolean |
hasAccess(Class<?> caller,
Class<?> clazz)
Checks whether or not caller can access clazz
based on Java's visibility rules. |
|
private static boolean |
hasAccess(Class<?> caller,
Class<?> clazz,
int modifiers)
Checks whether or not the access modifiers supplied as modifiers
allows caller to access to the owner of the modifiers declared
in, or being, clazz . |
|
static boolean |
hasAccess(Class<?> caller,
Member member)
Checks whether or not caller can access member based
on Java's visibility rules. |
|
static
|
invoke(Object target,
E member,
Object... arguments)
Returns the result of invoking the accessible member supplied as member . |
|
static boolean |
isInstance(boolean nullValuesMatch,
Class<?>[] types,
Object... values)
Returns true if each value in values is assignable to
the corresponding type at the same index in types ,
respectively, false if not. |
|
static boolean |
isInstance(Class<?>[] types,
Object... values)
Returns true if each value in values is assignable to
the corresponding type at the same index in types ,
respectively, false if not. |
|
static boolean |
isOverriding(Class<?> clazz,
String name,
Class<?>... types)
Determines if a given accessible non-abstract method is overriding a similar method in a super class based on Java's visibility rules. |
|
static boolean |
isSubClass(Class<?> clazz,
Class<?> subClass)
Returns true if and only if subClass is a sub-class
of clazz , false otherwise. |
|
static boolean |
isSuperClass(Class<?> clazz,
Class<?> superClass)
Returns true if and only if superClass is a super-class
of clazz , false otherwise. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
private Reflection()
Method Detail |
---|
public static final Class<?> forName(CharSequence className) throws ClassNotFoundException
Class
object associated with the class or
interface with the name supplied as className
.
Unlike the standard Class.forName(String)
method, this
method trims className
before trying the load the class
and it also handles primitive class names, e.g. int
for
Integer.TYPE
etc.
className
- The class name; cannot be null.
Class
object; never null.
NullPointerException
- If className
is null.
ClassNotFoundException
- If no such class could be
found.public static final Set<Class<?>> getAssignableTo(Class<?> clazz)
Class
supplied as clazz
can be assigned
to. The contained classes either represent super-classes or implemented interfaces, including those implemented by super-classes.
The returned set lists the classes and interfaces in a depth first manner, i.e., super-classes precedes sub-classes. A given interface is only included once in the set, corresponding to the first time visited.
clazz
- The class to fetch the assignable classes for;
cannot be null.
Object
).
NullPointerException
- If clazz
is null.public static final <C extends Collection<Class<?>>> C getAssignableTo(Class<?> clazz, C classes)
Class
supplied as clazz
can be assigned
to. The contained classes either represent super-classes or implemented interfaces, including those implemented by super-classes.
The classes and interfaces are added to classes
in a depth
first manner, super-classes precedes sub-classes, but the
collection type may or may not respect the insertion order.
Furthermore, a given interface may be included more than one time in
the returned collection, again depending on the type of collection.
C
- The actual collection type to use.clazz
- The class to fetch the assignable classes for;
cannot be null.classes
- The collection to add to classes and interfaces to;
cannot be null.
classes
collection; never null, and never empty
(will always at least contain Object
).
NullPointerException
- If either argument is null.public static final Class<?> getCallerClass()
The calling context can be either a static initialisation block, an instance initialisation block, a constructor, or a method. The caller class thus represents the class declaring such a member.
Example:
.. public void foo { // Calling context this.bar(); } public void bar { // Immediate calling context Class<?> callerClass = Reflection.getCallerClass(); // Represents the class of foo, *not* of bar .. } ..
CallerClass
,
Caller
public static final Class<?> getClass(Class<?> clazz)
clazz
.
If clazz
is null, then null is returned. If
clazz
is an anonymous class, the first non-anonymous
super class is used or the first and only
interface implemented.
clazz
- The class; can be null.
public static final Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException
clazz
accepting the formal parameter types supplied
as parameterTypes
, ignoring primitive types.
In case several constructors match, the constructor declared
first in clazz
is returned.
clazz
- The class; cannot be null.parameterTypes
- The formal parameter types for the constructor
to fetch.
NullPointerException
- If clazz
is null.
NoSuchMethodException
- If no such constructor can be found.public static <A extends Annotation> Constructor<?>[] getConstructors(Class<?> clazz, Class<A> annotation)
clazz
annotated with the annotation type supplied as annotation
.
A
- The type of annotation.clazz
- The class; cannot be null.annotation
- The class representing the annotation type; cannot be null.
NullPointerException
- If either argument is null.getMethods(Class, Class)
public static final <T> Constructor<T> getCopyConstructor(Class<T> clazz)
clazz
, if any.
A copy constructor of clazz
is defined as a constructor
accepting a single parameter of the exact same as clazz
.
The constructor will be made accessible, if not already.
T
- The type of class
.clazz
- The class; cannot be null.
clazz
.
NullPointerException
- If clazz
is null.public static final List<Field> getFields(Class<?> clazz)
clazz
, or in
a super-class or implemented interface. The fields
are returned in a depth-first order, i.e. fields
from super-classes or interfaces precede those from
sub-classes (clazz
).
clazz
- The class to fetch the fields for; cannot be null.
NullPointerException
- If clazz
is null.public static final Set<Class<?>> getInterfaces(Class<?> clazz)
Class
supplied
as clazz
can be assigned to, including interfaces
implemented by super-classes of clazz
. The returned set lists the interfaces in a depth first manner, i.e., interfaces from super-classes precedes interfaces from sub-classes. The order per class is the declaration order. A given interface is only included once in the set, corresponding to the first time visited.
clazz
- The class to fetch the assignable interfaces for;
cannot be null.
NullPointerException
- If clazz
is null.public static final <C extends Collection<Class<?>>> C getInterfaces(Class<?> clazz, C interfaces)
Class
supplied
as clazz
can be assigned to, including interfaces
implemented by super-classes of clazz
.
The ordering of the interfaces is determined by the collection
supplied as interfaces
, but the interfaces will be
added in a depth-first manner, i.e., interfaces from super-classes
will be added before interfaces from sub-classes. The order per class
is the declaration order.
If interfaces
is a non-set collection, duplicates may occur.
C
- The actual collection type to use.clazz
- The class to fetch the assignable interfaces for;
cannot be null.interfaces
- The collection to add the interfaces to;
cannot be null.
interfaces
collection; never null, but can be empty.
NullPointerException
- If either argument is null.public static final Method getMethod(Class<?> clazz, String name, Class<?>... parameterTypes) throws NoSuchMethodException
name
that
is declared in the class supplied as clazz
accepting
the formal parameter types supplied as parameterTypes
,
ignoring primitive types.
In case several methods match, the method declared first in
clazz
is returned.
clazz
- The class; cannot be null.name
- The method name; cannot be null.parameterTypes
- The formal parameter types for the method
to fetch.
NullPointerException
- If clazz
or name
are null.
NoSuchMethodException
- If no such method can be found.public static final List<Method> getMethods(Class<?> clazz)
clazz
, or in
a super-class or implemented interface. The methods
are returned in a depth-first order, i.e. methods
from super-classes or interfaces precede those from
sub-classes (clazz
). A unique method is only included once, but overridden versions may be included as well.
clazz
- The class to fetch the methods for; cannot be null.
clazz
represents a marker interface.
NullPointerException
- If clazz
is null.public static <A extends Annotation> Method[] getMethods(Class<?> clazz, Class<A> annotation)
clazz
annotated with the annotation type supplied as annotation
.
A
- The type of annotation.clazz
- The class; cannot be null.annotation
- The class representing the annotation type; cannot be null.
NullPointerException
- If either argument is null.getConstructors(Class, Class)
public static final <T> KeyValue<Class<? super T>,Class<? super T>> getOverriding(Class<T> clazz, String name, Class<?>... types)
clazz
, or clazz
it self, declaring a non-abstract method with
the name supplied as name
and the formal parameter types supplied as types
,
providing it is accessible to clazz
, and where the value is the first super
class of that class declaring a similar method. Hence, the class stored as the key declares
the identified overriding method, and the class stored as the value declares the similar
method being overridden. If the value of the returned (key,value) placeholder is null, the method does not override any similar method from a super class. If the value is not null, the class stored as the value will be a regular super class of the class stored as the key.
If the returned placeholder is null, no such accessible method could be found in
clazz
, or in any of its super classes.
Java's normal visibility rules are applied in the check.
T
- The type of the initial class.clazz
- The initial class to start the search for the method; cannot be null.name
- Method name; cannot be null.types
- Formal types of the method parameters, if any; can be empty.
clazz
it self. The value will
be null if the specified method does not override any similar method from a
super class.
NullPointerException
- If clazz
or name
are null.isOverriding(Class, String, Class...)
,
hasAccess(Class, Member)
public static final <T> List<Class<? super T>> getSuperClasses(Class<T> clazz)
clazz
,
excluding clazz
, where the last index is always
be Object.class
.
T
- The type of clazz
.clazz
- The class to fetch the super-classes for;
cannot be null.
clazz.equals(Object.class)
is true.
NullPointerException
- If clazz
is null.public static final boolean hasAccess(Class<?> caller, Class<?> clazz)
caller
can access clazz
based on Java's visibility rules.
Caution: the privilege check is performed without
the use of a security manager, and is only based on the
access modifiers and packages of caller
and
object
!
caller
- The caller class; cannot be null.clazz
- The clazz to test; cannot be null.
caller
has access, false if not.
NullPointerException
- If either parameter is null.hasAccess(Class, Member)
private static final boolean hasAccess(Class<?> caller, Class<?> clazz, int modifiers)
modifiers
allows caller
to access to the owner of the modifiers declared
in, or being, clazz
.
An owner will thus be a member, e.g. method, or a class, e.g. clazz
it self.
caller
- The caller class; cannot be null.clazz
- The clazz to test; cannot be null.modifiers
- The access modifiers.
modifiers
allows caller
to access the owner
of the modifiers, false if not.
NullPointerException
- If either caller
or clazz
are null.public static final boolean hasAccess(Class<?> caller, Member member)
caller
can access member
based
on Java's visibility rules.
Caution: the access check is performed without
the use of a security manager, and is only based on the
access modifiers and packages of of caller
and member
!
caller
- The caller class; cannot be null.member
- The member to test; cannot be null.
caller
has access, false if not.
NullPointerException
- If either parameter is null.hasAccess(Class, Class)
public static final <E extends AccessibleObject & Member> Object invoke(Object target, E member, Object... arguments)
member
. The member can either be a method, constructor, or field.
If the member is a non-static method, constructor, or non-static
field, target
will be used as the target instance.
The member will be made accessible if not already.
Checked exceptions are suppressed to runtime exceptions.
E
- The type of member.target
- The target if member
is non static;
can be null if member
is static.member
- The member to invoke; cannot be null.arguments
- The arguments for member
; can be null
if member
allows it. Unused if
member
is a field.
member
; can be null.
NullPointerException
- If member
is null, or if
target
and/or arguments
is null but
not allowed by member
.
RuntimeException
- If the invocation fails.public static final boolean isInstance(boolean nullValuesMatch, Class<?>[] types, Object... values)
values
is assignable to
the corresponding type at the same index in types
,
respectively, false if not.
If objects
is null and nullValuesMatch
is true
and types
is empty, true will be returned; false otherwise.
If nullValuesMatch
is true, a null value will match the
corresponding type in types
at the same index.
The comparison is done ignoring primitives.
nullValuesMatch
- True if null values in values
will
count as a match to the corresponding type,
false if not.types
- The types; cannot be null.values
- The values; can be null.
NullPointerException
- If types
is null.public static final boolean isInstance(Class<?>[] types, Object... values)
values
is assignable to
the corresponding type at the same index in types
,
respectively, false if not.
If objects
is null or contains null values, this method will
always return false.
The comparison is done ignoring primitives.
types
- The types; cannot be null.values
- The values; cannot be null.
NullPointerException
- If types
is null.public static final boolean isOverriding(Class<?> clazz, String name, Class<?>... types)
The overriding method, e.g. the method defined by name
and the formal parameter types supplied as types
overriding
a similar method in a super class, need not be declared in clazz
,
but may be declared in any super class of it, providing it is accessible
to clazz
.
clazz
- The initial class to start the search for the method; cannot be null.name
- Method name; cannot be null.types
- Formal types of the method parameters, if any; can be empty.
clazz
, but can also be declared in any super class of it.
NullPointerException
- If clazz
or name
are null.getOverriding(Class, String, Class...)
,
hasAccess(Class, Member)
public static final boolean isSubClass(Class<?> clazz, Class<?> subClass)
subClass
is a sub-class
of clazz
, false otherwise.
For this method to return true, subClass
must at some
point extend clazz
, directly or indirectly. Implementing
common interfaces is not enough.
clazz
- The parent class; cannot be null.subClass
- The sub-class; cannot be null.
subClass
is a sub-class of clazz
,
false if not.
NullPointerException
- If either argument is null.public static final boolean isSuperClass(Class<?> clazz, Class<?> superClass)
superClass
is a super-class
of clazz
, false otherwise.
For this method to return true, clazz
must at some
point extend superClass
, directly or indirectly. Implementing
common interfaces is not enough.
clazz
- The parent class; cannot be null.superClass
- The super-class; cannot be null.
superClass
is a super-class of clazz
,
false if not.
NullPointerException
- If either argument is null.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |