org.geotoolkit.util.converter
Class Classes

Object
  extended by Static
      extended by Classes

public final class Classes
extends Static

Miscellaneous static methods working on Class objects. This class defines helper methods for working with reflection. Some functionalities are:

Since:
2.5
Version:
3.20
Author:
Martin Desruisseaux (IRD, Geomatys)
Module:
utility/geotk-utility (download)    View source code for this class

Method Summary
static
<U> Class<? extends U>
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
<T> Class<? extends T>
getClass(T object)
          Returns the class of the specified object, or null if object is null.
static
<T> Set<Class<? extends T>>
getClasses(Collection<? extends T> objects)
          Returns the classes of all objects in the given collection.
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 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

changeArrayDimension

public static Class<?> changeArrayDimension(Class<?> element,
                                            int dimension)
Changes the array dimension by the given amount. The given class can be a primitive type, a Java object, or an array of the above. If the given 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:

Parameters:
element - The type of elements in the array.
dimension - The change of dimension, as a negative or positive number.
Returns:
The type of an array of the given element type augmented by the given number of dimensions (which may be negative), or null.
Since:
3.03

boundOfParameterizedAttribute

public static Class<?> boundOfParameterizedAttribute(Field field)
Returns the upper bounds of the parameterized type of the given attribute. If the attribute does not have a parameterized type, returns 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:

Parameters:
field - The field for which to obtain the parameterized type.
Returns:
The upper bound of parameterized type, or null if the given field is not of a parameterized type.

boundOfParameterizedAttribute

public 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. Otherwise returns 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).

Parameters:
method - The getter or setter method for which to obtain the parameterized type.
Returns:
The upper bound of parameterized type, or null if the given method do not operate on an object of a parameterized type.

asSubclassOrNull

public static <U> Class<? extends U> asSubclassOrNull(Class<?> type,
                                                      Class<U> sub)
Casts the 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.

Type Parameters:
U - The compile-time bounds of the sub argument.
Parameters:
type - The class to cast to a sub-class, or null.
sub - The subclass to cast to, or null.
Returns:
The type argument casted to a subclass of the sub argument, or null if this cast can not be performed.
Since:
3.09
See Also:
Class.asSubclass(Class)

getClass

public static <T> Class<? extends T> getClass(T object)
Returns the class of the specified object, or 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.

Type Parameters:
T - The type of the given object.
Parameters:
object - The object for which to get the class, or null.
Returns:
The class of the given object, or null if the given object was null.

getClasses

public static <T> Set<Class<? extends T>> getClasses(Collection<? extends T> objects)
Returns the classes of all objects in the given collection. If the given collection contains some null elements, then the returned set will contains a null element as well. The returned set is modifiable and can be freely updated by the caller.

Note that interfaces are not included in the returned set.

Type Parameters:
T - The base type of elements in the given collection.
Parameters:
objects - The collection of objects.
Returns:
The set of classes of all objects in the given collection.
Since:
3.00

getAllInterfaces

public static Set<Class<?>> getAllInterfaces(Class<?> type)
Returns the set of every interfaces implemented by the given class or interface. This is similar to 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.

Parameters:
type - The class or interface for which to get all implemented interfaces.
Returns:
All implemented interfaces (not including the given type if it was an interface), or an empty set if none. Callers can freely modify the returned set.
Since:
3.01

getLeafInterfaces

public 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 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.

Type Parameters:
T - The type of the baseInterface class argument.
Parameters:
type - A class for which the implemented interface is desired.
baseInterface - The base type of the interface to search.
Returns:
The leaf interfaces matching the given criterion, or null if none. If non-null, than the array is guaranteed to contain at least one element.
Since:
3.18

findSpecializedClass

public static Class<?> findSpecializedClass(Collection<?> objects)
Returns the most specific class implemented by the objects in the given collection. If there is more than one specialized class, returns their most specific common super class.

This method searches for classes only, not interfaces.

Parameters:
objects - A collection of objects. May contains duplicated values and null values.
Returns:
The most specialized class, or null if the given collection does not contain at least one non-null element.
Since:
3.01 (derived from 2.5)

findCommonClass

public static Class<?> findCommonClass(Collection<?> objects)
Returns the most specific class which is assignable from the type of all given objects. If no element in the given collection has a type assignable from the type of all other elements, then this method searches for a common super class.

This method searches for classes only, not interfaces.

Parameters:
objects - A collection of objects. May contains duplicated values and null values.
Returns:
The most specific class common to all supplied objects, or null if the given collection does not contain at least one non-null element.
Since:
3.01 (derived from 2.5)

findCommonClass

public 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. This method returns either c1, c2 or a common parent of c1 and c2.

This method considers classes only, not the interfaces.

Parameters:
c1 - The first class, or null.
c2 - The second class, or null.
Returns:
The most specific class common to the supplied classes, or null if both c1 and c2 are null.
Since:
3.01 (derived from 3.00)

findCommonInterfaces

public static Set<Class<?>> findCommonInterfaces(Class<?> c1,
                                                 Class<?> c2)
Returns the interfaces which are implemented by the two given classes. The returned set does not include the parent interfaces. For example if the two given objects implement the Collection interface, then the returned set will contains the Collection type but not the Iterable type, since it is implied by the collection type.

Parameters:
c1 - The first class.
c2 - The second class.
Returns:
The interfaces common to both classes, or an empty set if none. Callers can freely modify the returned set.
Since:
3.01

implementSameInterfaces

public static boolean implementSameInterfaces(Class<?> object1,
                                              Class<?> object2,
                                              Class<?> base)
Returns 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.
}

Parameters:
object1 - The first object to check for interfaces.
object2 - The second object to check for interfaces.
base - The parent of all interfaces to check.
Returns:
true if both objects implement the same set of interfaces, considering only sub-interfaces of base.
Since:
3.01 (derived from 2.5)

getShortName

public static String getShortName(Class<?> classe)
Returns a short class name for the specified class. This method will omit the package name. For example, it will return "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".

Parameters:
classe - The object class (may be null).
Returns:
A short class name for the specified object, or "<*>" if the given class was null.
See Also:
getShortClassName(Object), Class.getSimpleName()

getShortClassName

public static String getShortClassName(Object object)
Returns a short class name for the specified object. This method will omit the package name. For example, it will return "String" instead of "java.lang.String" for a String object.

Parameters:
object - The object (may be null).
Returns:
A short class name for the specified object.
See Also:
getShortName(Class)

isAssignableTo

public static boolean isAssignableTo(Class<?> type,
                                     Class<?>... allowedTypes)
Returns 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:

Parameters:
type - The type to be tested, or null.
allowedTypes - The allowed types.
Returns:
true if the given type is assignable to one of the allowed types.
Since:
3.12

isPossibleGetter

public static boolean isPossibleGetter(Method method)
Returns 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.

Those conditions may be updated in any future Geotk version.

Parameters:
method - The method to inspect.
Returns:
true if the given method may possibly be a non-deprecated getter method.
Since:
3.20


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.