Class Classes
Class objects.
This class defines helper methods for working with reflection.
Some functionalities are:
- Add or remove dimension to an array type
(
changeArrayDimension) - Find the common parent of two or more classes
(
findCommonClass,findCommonInterfaces) - Getting the bounds of a parameterized field or method
(
boundOfParameterizedProperty) - Getting a short class name (
getShortName,getShortClassName)
- Since:
- 0.3
Defined in the sis-utility module
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>boundOfParameterizedDeclaration(GenericDeclaration typeOrMethod) Returns a single bound declared in a parameterized class or a parameterized method.static Class<?>Returns the upper bounds of the parameterized type of the given property.static Class<?>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 change) 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.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, ornullifobjectis 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 StringgetShortClassName(Object object) Returns the class name of the given object without package name, but including the enclosing class names if any.static StringgetShortName(Class<?> classe) Returns the name of the given class without package name, but including the names of enclosing classes if any.static <T> Class<? super T>getStandardType(Class<T> type) Returns the first type or super-type (including interface) considered "standard" in Apache SIS sense.static booleanimplementSameInterfaces(Class<?> object1, Class<?> object2, Class<?> baseInterface) Returnstrueif the two specified objects implements exactly the same set of interfaces.static booleanisAssignableToAny(Class<?> type, Class<?>... allowedTypes) Returnstrueif the given type is assignable to one of the given allowed types.static booleanisPossibleGetter(Method method) Returnstrueif the given method may possibly be the getter method for a property.
-
Method Details
-
changeArrayDimension
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 givenchangeis positive, then the array dimension will be increased by that amount. For example, a change of +1 dimension will change anintclass intoint[], and aString[]class intoString[][]. A change of +2 dimensions is like applying two times a change of +1 dimension.The change of dimension can also be negative. For example, a change of -1 dimension will change a
String[]class into aString. More specifically:- If the given
elementis null, then this method returnsnull. - Otherwise if the given
changeis 0, then the givenelementis returned unchanged. - Otherwise if the given
changeis negative, thenClass.getComponentType()is invokedabs(change)times. The result is anullvalue ifabs(change)is greater than the array dimension. - Otherwise if
elementisVoid.TYPE, then this method returnsVoid.TYPEsince arrays ofvoiddo not exist. - Otherwise this method returns a class that represents an array of the given class augmented by the given amount of dimensions.
- Parameters:
element- the type of elements in the array.change- 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.
- If the given
-
boundOfParameterizedProperty
Returns the upper bounds of the parameterized type of the given property. If the property does not have a parameterized type, returnsnull. If the property has more than one parameterized type, then the parameter examined by this method depends on the property type:- If
Map, then this method returns the type of keys in map entries. - For all other types, this method expects exactly one parameterized type
for avoiding ambiguity. If this is not the case,
nullis returned.
Classinstance because of the way parameterized types are implemented in Java (by erasure).Examples
When invoking this method for a field of the following types:Map<String,Number>: returnsString.class, the type of keys.Set<Number>: returnsNumber.class.Set<? extends Number>: returnsNumber.classas well, because that collection cannot contain instances of super-classes.Numberis the upper bound.Set<? super Number>: returnsObject.class, because that collection is allowed to contain such elements.Set: returnsnullbecause that collection is declared with raw type.Long: returnsnullbecause that type is not parameterized.
- Parameters:
field- the field for which to obtain the parameterized type.- Returns:
- the upper bound of parameterized type, or
nullif the given field is not of a parameterized type.
- If
-
boundOfParameterizedProperty
If the given method is a getter or a setter for a parameterized property, returns the upper bounds of the parameterized type. Otherwise returnsnull. This method provides the same semantic thanboundOfParameterizedProperty(Field), but works on a getter or setter method rather than a field. SeeboundOfParameterizedProperty(Field)javadoc for details.This method is used for fetching the type of elements in a collection. This information cannot be obtained from a
Classinstance 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
nullif the given method is not a getter or setter for a property of a parameterized type.
-
boundOfParameterizedDeclaration
Returns a single bound declared in a parameterized class or a parameterized method. ThetypeOrMethodargument is usually aClassfor a collection type. If the given argument is a non-parameterized class, then this method searches for the first parameterized super-class (see example below). If no parameterized declaration is found, then this method returnsnull. If the declaration has more than one parameterized type, then this method applies the same heuristic rule asboundOfParameterizedProperty(Field)(see the javadoc of that method for details).Examples
When invoking this method with the followingClassargument values:List.class: returnsObject.classbecauseListis declared asList<E>(implicitly<E extends Object>).Map.class: returnsObject.classbecauseMapis declared asMap<K,V>and, as an heuristic rule, we return the key type of map entry.PrinterStateReasons.class: returnsPrinterStateReason.classbecausePrinterStateReasonsis not parameterized but extendsHashMap<PrinterStateReason,Severity>.Long.class: returnsnullbecause that type is not parameterized.
boundOfParameterizedProperty(…)cannot be used. -
getClass
Returns the class of the specified object, ornullifobjectis 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();
Numberis replaced by a parameterized typeT.- Type Parameters:
T- the type of the given object.- Parameters:
object- the object for which to get the class, ornull.- Returns:
- the class of the given object, or
nullif the given object was null.
-
getStandardType
Returns the first type or super-type (including interface) considered "standard" in Apache SIS sense. This method applies the following heuristic rules, in that order:- If the given type implements at least one interface having the
UMLannotation, then the first annotated interface is returned. - Otherwise the first public class or parent class is returned.
- Type Parameters:
T- the compile-time type argument.- Parameters:
type- the type for which to get the standard interface or class. May benull.- Returns:
- a standard interface implemented by
type, or otherwise the most specific public class. Isnullif the giventypeargument was null. - Since:
- 1.0
- If the given type implements at least one interface having the
-
getAllInterfaces
Returns every interfaces implemented, directly or indirectly, by the given class or interface. This is similar toClass.getInterfaces()except that this method searches recursively in the super-interfaces. For example if the given type isArrayList, then the returned set will containList(which is implemented directly) together with its parent interfacesCollectionandIterable.Elements ordering
All interfaces implemented directly by the given type are first and in the order they are declared in theimplementsorextendsclause. Parent interfaces are next.- Type Parameters:
T- the compile-time type of theClassargument.- Parameters:
type- the class or interface for which to get all implemented interfaces.- Returns:
- all implemented interfaces (not including the given
typeif it was an interface), or an empty array if none. - See Also:
-
getLeafInterfaces
Returns the interfaces implemented by the given class and assignable to the given base interface, or an empty array 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 theSetandCollectioninterfaces, then the returned array contains only theSetinterface.Example
getLeafInterfaces(ArrayList.class, Collection.class)returns an array of length 1 containingList.class.- Type Parameters:
T- the type of thebaseInterfaceclass argument.- Parameters:
type- a class for which the implemented interfaces are desired, ornull.baseInterface- the base type of the interfaces to search.- Returns:
- the leaf interfaces matching the given criterion, or an empty array if none.
-
findSpecializedClass
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
nullif the given collection does not contain at least one non-null element.
-
findCommonClass
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
nullif the given collection does not contain at least one non-null element.
-
findCommonClass
Returns the most specific class which is assignable from the given classes or a parent of those classes. This method returns eitherc1,c2or a common parent ofc1andc2.This method considers classes only, not the interfaces.
- Parameters:
c1- the first class, ornull.c2- the second class, ornull.- Returns:
- the most specific class common to the supplied classes, or
nullif bothc1andc2are null.
-
findCommonInterfaces
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 theCollectioninterface, then the returned set will contain theCollectiontype but not theIterabletype, 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.
-
implementSameInterfaces
public static boolean implementSameInterfaces(Class<?> object1, Class<?> object2, Class<?> baseInterface) Returnstrueif the two specified objects implements exactly the same set of interfaces. Only interfaces assignable tobaseInterfaceare compared. Declaration order does not matter.Example: in ISO 19111, different interfaces exist for different coordinate system (CS) geometries (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 Systems are of the same kind. }- Parameters:
object1- the first object to check for interfaces.object2- the second object to check for interfaces.baseInterface- the parent of all interfaces to check.- Returns:
trueif both objects implement the same set of interfaces, considering only sub-interfaces ofbaseInterface.
-
getShortName
Returns the name of the given class without package name, but including the names of enclosing classes if any. This method is similar to theClass.getSimpleName()method, except that if the given class is an inner class, then the returned value is prefixed with the outer class name. Another 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 name comparisons 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"- Parameters:
classe- the object class (may benull).- Returns:
- the simple name with outer class name (if any) of the first non-anonymous
class in the hierarchy, or
"<*>"if the given class is null. - See Also:
-
getShortClassName
Returns the class name of the given object without package name, but including the enclosing class names if any. Invoking this method is equivalent to invokinggetShortName(object.getClass())except fornullvalue. SeegetShortName(Class)for more information on the class name returned by this method.- Parameters:
object- the object (may benull).- Returns:
- the simple class name with outer class name (if any) of the first non-anonymous
class in the hierarchy, or
"<*>"if the given object is null. - See Also:
-
isAssignableToAny
Returnstrueif the given type is assignable to one of the given allowed types. More specifically, if at least oneallowedTypes[i]element exists for whichallowedTypes[i].isAssignableFrom(type)returnstrue, then this method returnstrue.Special cases:
- If
typeis null, then this method returnsfalse. - If
allowedTypesis null, then this method returnstrue. This is to be interpreted as "no restriction on the allowed types". - Any null element in the
allowedTypesarray are silently ignored.
- Parameters:
type- the type to be tested, ornull.allowedTypes- the allowed types.- Returns:
trueif the given type is assignable to one of the allowed types.
- If
-
isPossibleGetter
Returnstrueif 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
trueif the given method meets all the following conditions. Note that atruevalue is not a guaranteed that the given method is really a getter. The caller is encouraged to perform additional checks if possible.- The method does no expect any argument.
- The method returns a value (anything except
void). - The method name is not
clone,getClass,hashCode,toStringortoWKT. - The method is not synthetic.
Those conditions may be updated in any future SIS version.
- Parameters:
method- the method to inspect.- Returns:
trueif the given method may possibly be a non-deprecated getter method.
-