|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectStatic
Classes
public final class Classes
Miscellaneous static methods working on Class objects.
This class defines helper methods for working with reflection.
Some functionalities are:
changeArrayDimension)findCommonClass,
(findCommonInterfaces)boundOfParameterizedAttribute)getShortName,
getShortClassName)
| utility/geotk-utility (download) | View source code for this class |
| Method Summary | ||
|---|---|---|
static
|
asSubclassOrNull(Class<?> type,
Class<U> sub)
Casts the type class to represent a subclass of the class represented by the
sub argument. |
|
static Class<?> |
boundOfParameterizedAttribute(Field field)
Returns the upper bounds of the parameterized type of the given attribute. |
|
static Class<?> |
boundOfParameterizedAttribute(Method method)
If the given method is a getter or a setter for a parameterized attribute, 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(Collection<?> 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(Collection<?> objects)
Returns the most specific class implemented by the objects in the given collection. |
|
static Set<Class<?>> |
getAllInterfaces(Class<?> type)
Returns the set of every interfaces implemented by the given class or interface. |
|
static
|
getClass(T object)
Returns the class of the specified object, or null if object is null. |
|
static
|
getClasses(Collection<? extends T> objects)
Returns the classes of all objects in the given collection. |
|
static
|
getLeafInterfaces(Class<?> type,
Class<T> baseInterface)
Returns the interfaces implemented by the given class and assignable to the given base interface, or null if none. |
|
static String |
getShortClassName(Object object)
Returns a short class name for the specified object. |
|
static String |
getShortName(Class<?> classe)
Returns a short class name for the specified class. |
|
static boolean |
implementSameInterfaces(Class<?> object1,
Class<?> object2,
Class<?> base)
Returns true if the two specified objects implements exactly the same set of
interfaces. |
|
static boolean |
isAssignableTo(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 an attribute. |
|
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
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<?> boundOfParameterizedAttribute(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).
Examples: When invoking this method for a field of the type below:
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<?> boundOfParameterizedAttribute(Method method)
null. This method
provides the same semantic than boundOfParameterizedAttribute(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
do not operate on an object of a parameterized type.
public static <U> Class<? extends U> asSubclassOrNull(Class<?> type,
Class<U> sub)
type class to represent a subclass of the class represented by the
sub argument. Checks that the cast is valid, and returns null if it
is not.
This method performs the same work than
type.asSubclass(sub),
except that null is returned instead than throwing an exception
if the cast is not valid or if any of the argument is null.
U - The compile-time bounds of the sub argument.type - The class to cast to a sub-class, or null.sub - The subclass to cast to, or null.
type argument casted to a subclass of the sub argument,
or null if this cast can not be performed.Class.asSubclass(Class)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:
doesn't seem to work ifNumber n = 0; Class<? extends Number> c = n.getClass();
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> Set<Class<? extends T>> getClasses(Collection<? extends T> objects)
Note that interfaces are not included in the returned set.
T - The base type of elements in the given collection.objects - The collection of objects.
public static Set<Class<?>> getAllInterfaces(Class<?> 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.
type - The class or interface for which to get all implemented interfaces.
type if it was an
interface), or an empty set if none. Callers can freely modify the returned set.
public static <T> Class<? extends T>[] getLeafInterfaces(Class<?> type,
Class<T> baseInterface)
null if none. If more than one interface extends the given base,
then the most specialized interfaces are returned. For example if the given class
implements both the Set and Collection interfaces, then the returned
array contains only the Set interface.
Example
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 interface is desired.baseInterface - The base type of the interface to search.
null if none.
If non-null, than the array is guaranteed to contain at least one element.public static Class<?> findSpecializedClass(Collection<?> 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(Collection<?> 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<?> base)
true if the two specified objects implements exactly the same set of
interfaces. Only interfaces assignable to base are compared. Declaration order
doesn't matter. For example in ISO 19111, different interfaces exist for different coordinate
system geometries (CartesianCS, PolarCS, etc.). We can check if two
CS implementations has 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.base - The parent of all interfaces to check.
true if both objects implement the same set of interfaces,
considering only sub-interfaces of base.public static String getShortName(Class<?> classe)
"String" instead
of "java.lang.String" for a String object. It will also name
array according Java language usage, for example "double[]" instead
of "[D".
This method is similar to the Class.getSimpleName() method, except that
if the given class is an inner class, then the returned value is prefixed with
the outer class name. For example this method returns "Point2D.Double"
instead of "Double".
classe - The object class (may be null).
"<*>" if the
given class was null.getShortClassName(Object),
Class.getSimpleName()public static String getShortClassName(Object object)
"String"
instead of "java.lang.String" for a String object.
object - The object (may be null).
getShortName(Class)
public static boolean isAssignableTo(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 an attribute.
This method implements the algorithm used by Geotk 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 Geotk version.
method - The method to inspect.
true if the given method may possibly be a non-deprecated getter method.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||