public final class Classes extends Static
Class
objects.
This class defines helper methods for working with reflection.
Some functionalities are:
changeArrayDimension
)findCommonClass
,
findCommonInterfaces
)boundOfParameterizedProperty
)getShortName
,
getShortClassName
)Defined in the sis-utility
module
Modifier and Type | Method and Description |
---|---|
static Class<?> |
boundOfParameterizedProperty(Field field)
Returns the upper bounds of the parameterized type of the given property.
|
static Class<?> |
boundOfParameterizedProperty(Method method)
If the given method is a getter or a setter for a parameterized property, returns the
upper bounds of the parameterized type.
|
static Class<?> |
changeArrayDimension(Class<?> element,
int dimension)
Changes the array dimension by the given amount.
|
static Class<?> |
findCommonClass(Class<?> c1,
Class<?> c2)
Returns the most specific class which is assignable from
the given classes or a parent of those classes.
|
static Class<?> |
findCommonClass(Iterable<?> objects)
Returns the most specific class which is assignable from
the type of all given objects.
|
static Set<Class<?>> |
findCommonInterfaces(Class<?> c1,
Class<?> c2)
Returns the interfaces which are implemented by the two given classes.
|
static Class<?> |
findSpecializedClass(Iterable<?> objects)
Returns the most specific class implemented by the objects in the given collection.
|
static <T> Class<? super T>[] |
getAllInterfaces(Class<T> type)
Returns every interfaces implemented, directly or indirectly, by the given class or interface.
|
static <T> Class<? extends T> |
getClass(T object)
Returns the class of the specified object, or
null if object is null. |
static <T> Class<? extends T>[] |
getLeafInterfaces(Class<?> type,
Class<T> baseInterface)
Returns the interfaces implemented by the given class and assignable to the given base
interface, or an empty array if none.
|
static String |
getShortClassName(Object object)
Returns the class name of the given object without package name, but including the enclosing class names
if any.
|
static String |
getShortName(Class<?> classe)
Returns the name of the given class without package name, but including the names of enclosing
classes if any.
|
static boolean |
implementSameInterfaces(Class<?> object1,
Class<?> object2,
Class<?> baseInterface)
Returns
true if the two specified objects implements exactly the same set
of interfaces. |
static boolean |
isAssignableToAny(Class<?> type,
Class<?>... allowedTypes)
Returns
true if the given type is assignable to one of the given allowed types. |
static boolean |
isPossibleGetter(Method method)
Returns
true if the given method may possibly be the getter method for a property. |
public static Class<?> changeArrayDimension(Class<?> element, int dimension)
dimension
is positive, then
the array dimension will be increased by that amount. For example a change of dimension 1
will change a int
class into int[]
, and a String[]
class into
String[][]
. A change of dimension 2 is like applying a change of dimension 1 two
times.
The change of dimension can also be negative. For example a change of dimension -1 will
change a String[]
class into a String
. More specifically:
element
is null, then this method returns null
.dimension
change is 0, then the given element
is returned unchanged.dimension
change is negative, then
Class.getComponentType()
is invoked abs(dimension)
times.
The result is a null
value if abs(dimension)
is greater
than the array dimension.element
is Void.TYPE
, then this method returns
Void.TYPE
since arrays of void
don't exist.element
- the type of elements in the array.dimension
- the change of dimension, as a negative or positive number.null
.public static Class<?> boundOfParameterizedProperty(Field field)
null
.
This method is typically used for fetching the type of elements in a collection.
We do not provide a method working from a Class
instance because of the way
parameterized types are implemented in Java (by erasure).
Set<Number>
returns Number.class
.Set<? extends Number>
returns Number.class
as well, since that
collection can not (in theory) contain instances of super-classes; Number
is the upper bound.Set<? super Number>
returns Object.class
, because that collection
is allowed to contain such elements.Set
returns null
because that collection is un-parameterized.field
- the field for which to obtain the parameterized type.null
if the given field
is not of a parameterized type.public static Class<?> boundOfParameterizedProperty(Method method)
null
. This method
provides the same semantic than boundOfParameterizedProperty(Field)
, but
works on a getter or setter method rather then the field. See the javadoc of above
method for more details.
This method is typically used for fetching the type of elements in a collection.
We do not provide a method working from a Class
instance because of the way
parameterized types are implemented in Java (by erasure).
method
- the getter or setter method for which to obtain the parameterized type.null
if the given method
does not operate on an object of a parameterized type.@Workaround(library="JDK", version="1.7") public static <T> Class<? extends T> getClass(T object)
null
if object
is null.
This method is also useful for fetching the class of an object known only by its bound
type. As of Java 6, the usual pattern:
Number n = 0; Class<? extends Number> c = n.getClass();doesn't seem to work if
Number
is replaced by a parameterized type T
.T
- the type of the given object.object
- the object for which to get the class, or null
.null
if the given object was null.public static <T> Class<? super T>[] getAllInterfaces(Class<T> type)
Class.getInterfaces()
except that this method searches recursively
in the super-interfaces. For example if the given type is ArrayList
, then
the returned set will contains List
(which is implemented directly)
together with its parent interfaces Collection
and Iterable
.T
- the compile-time type of the Class
argument.type
- the class or interface for which to get all implemented interfaces.type
if it was an interface),
or an empty array if none.Class.getInterfaces()
public static <T> Class<? extends T>[] getLeafInterfaces(Class<?> type, Class<T> baseInterface)
Set
and Collection
interfaces, then the returned
array contains only the Set
interface.
getLeafInterfaces(ArrayList.class, Collection.class)
returns an array of length 1
containing List.class
.T
- the type of the baseInterface
class argument.type
- a class for which the implemented interfaces are desired, or null
.baseInterface
- the base type of the interfaces to search.public static Class<?> findSpecializedClass(Iterable<?> objects)
This method searches for classes only, not interfaces.
objects
- a collection of objects. May contains duplicated values and null values.null
if the given collection does not contain
at least one non-null element.public static Class<?> findCommonClass(Iterable<?> objects)
This method searches for classes only, not interfaces.
objects
- a collection of objects. May contains duplicated values and null values.null
if the
given collection does not contain at least one non-null element.public static Class<?> findCommonClass(Class<?> c1, Class<?> c2)
c1
,
c2
or a common parent of c1
and c2
.
This method considers classes only, not the interfaces.
c1
- the first class, or null
.c2
- the second class, or null
.null
if both c1
and c2
are null.public static Set<Class<?>> findCommonInterfaces(Class<?> c1, Class<?> c2)
Collection
interface, then the returned set will contains the Collection
type but not the Iterable
type, since it is implied by the collection type.c1
- the first class.c2
- the second class.public static boolean implementSameInterfaces(Class<?> object1, Class<?> object2, Class<?> baseInterface)
true
if the two specified objects implements exactly the same set
of interfaces. Only interfaces assignable to baseInterface
are compared.
Declaration order doesn't matter.
CartesianCS
, PolarCS
, etc.). One can check if two implementations have
the same geometry with the following code:
if (implementSameInterfaces(cs1, cs2, CoordinateSystem.class)) { // The two Coordinate System are of the same kind. }
object1
- the first object to check for interfaces.object2
- the second object to check for interfaces.baseInterface
- the parent of all interfaces to check.true
if both objects implement the same set of interfaces,
considering only sub-interfaces of baseInterface
.public static String getShortName(Class<?> classe)
Class.getSimpleName()
method, except that
if the given class is an inner class, then the returned value is prefixed with the outer class
name. An other difference is that if the given class is local or anonymous, then this method
returns the name of the parent class.
The following table compares the various kind of names for some examples:
Class | getName() |
getSimpleName() |
getCanonicalName() |
getShortName() |
---|---|---|---|---|
String |
"java.lang.String" |
"String" |
"java.lang.String" |
"String" |
double[] |
"[D" |
"double[]" |
"double[]" |
"double[]" |
Point2D.Double |
"java.awt.geom.Point2D$Double" |
"Double" |
"java.awt.geom.Point2D.Double" |
"Point2D.Double" |
Anonymous Comparable |
"com.mycompany.myclass$1" |
"" |
null |
"Object" |
classe
- the object class (may be null
)."<*>"
if the given class is null.getShortClassName(Object)
,
Class.getSimpleName()
public static String getShortClassName(Object object)
getShortName(object.getClass())
except for
null
value. See getShortName(Class)
for more information on the class name returned by
this method.object
- the object (may be null
)."<*>"
if the given object is null.getShortName(Class)
public static boolean isAssignableToAny(Class<?> type, Class<?>... allowedTypes)
true
if the given type is assignable to one of the given allowed types.
More specifically, if at least one allowedTypes[i]
element exists for which
allowedTypes[i].isAssignableFrom(type)
returns true
, then this method returns true
.
Special cases:
type
is null, then this method returns false
.allowedTypes
is null, then this method returns true
.
This is to be interpreted as "no restriction on the allowed types".allowedTypes
array are silently ignored.type
- the type to be tested, or null
.allowedTypes
- the allowed types.true
if the given type is assignable to one of the allowed types.public static boolean isPossibleGetter(Method method)
true
if the given method may possibly be the getter method for a property.
This method implements the algorithm used by SIS in order to identify getter methods in
metadata interfaces. We do not rely on naming convention
(method names starting with "get
" or "is
" prefixes) because not every methods
follow such convention (e.g. ConformanceResult.pass()
).
The current implementation returns true
if the given method meets all the
following conditions. Note that a true
value is not a guaranteed that the given
method is really a getter. The caller is encouraged to perform additional checks if
possible.
void
).clone
, getClass
,
hashCode
, toString
or
toWKT
.Those conditions may be updated in any future SIS version.
method
- the method to inspect.true
if the given method may possibly be a non-deprecated getter method.Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.