Class ReflectionTools
- java.lang.Object
-
- de.aristaflow.adept2.util.reflection.ReflectionTools
-
public class ReflectionTools extends Object
Various helper methods for reflection.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ReflectionTools.ClassVisitor<T>
Interface for visiting a class hierarchy and accumulating the result.
-
Constructor Summary
Constructors Constructor Description ReflectionTools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
distance(Class<?> sup, Class<?> sub)
Calculates the distance from the designated subclass to the designated superclass: Equal classes have the distance 0. An interface and a class directly implementing it have the distance 1. A super-class and its subclass have the distance 2. A super-interface and its subinterface have the distance 2. If the subclass cannot be converted to the superclass or one of the classes isnull
, the distance will beInteger.MAX_VALUE
. Distances spanning several classes/instances are totalled.static MethodWrapper
getCorrespondingVarArgsMethod(String name, Class<?>[] paramTypes, Object[] paramValues, Class<?> searchedClass)
Gets the method from the designated class that corresponds to the designated method but has an additional varargs parameter or the designated method has an varargs parameter but the returned method from the designated class has not.static String
getJavadocRefString(Class<?> cls)
Gets the name and path of the Javadoc HTML file (relative, without leading "/" for the designated class.static String
getJavadocRefString(Method m)
Gets fragment identifier of the designated method including the name and path of the Javadoc HTML file (relative, without leading "/" of the corresponding declaring class.protected static StringBuilder
getJavadocRefType(Class<?> cls)
Gets a string representation for the designated class used in Javadoc.static <T> T
visitClassHierarchy(Class<?> cls, ReflectionTools.ClassVisitor<T> visitor, boolean includeTransIfaces)
Visits the (complete) class hierarchy (interfaces, parent classes and their interfaces as well as all parent interfaces if desired) of the designated class with the designated visitor.
-
-
-
Method Detail
-
distance
public static int distance(Class<?> sup, Class<?> sub)
Calculates the distance from the designated subclass to the designated superclass:- Equal classes have the distance 0.
- An interface and a class directly implementing it have the distance 1.
- A super-class and its subclass have the distance 2.
- A super-interface and its subinterface have the distance 2.
- If the subclass cannot be converted to the superclass or one of the classes is
null
, the distance will beInteger.MAX_VALUE
.
- Parameters:
sup
- The class (as fixed point) to which to calculate the distance.sub
- The class from which to calculate the distance.- Returns:
- The distance between the two classes (or interfaces).
-
getCorrespondingVarArgsMethod
public static MethodWrapper getCorrespondingVarArgsMethod(String name, Class<?>[] paramTypes, Object[] paramValues, Class<?> searchedClass)
Gets the method from the designated class that corresponds to the designated method but has an additional varargs parameter or the designated method has an varargs parameter but the returned method from the designated class has not. If no corresponding method is found or there are two methods found with an additional varargs parameter so we cannot decide which one to choose,null
will be returned.
This also applies to constructors. With "corresponding" including name equality, this method is of restricted use for constructors since they need to have the same (class) name. A corresponding method without a varargs parameter will only be searched if parameter values are provided and the last one isnull
or an array only containingnull
. In this case we expect the call to the varargs method equal to calling the method without the last parameter and the varargs parameter to be irrelevant.- Parameters:
name
- The name of the method/constructor to get the corresponding method/constructor for.paramTypes
- The parameter types of the method/constructor to get a corresponding method/constructor for. This must not benull
.paramValues
- The parameter values of the method/constructor. This is only relevant when looking for a corresponding non-varargs method/constructor. In all other cases simply usenull
.searchedClass
- The class to get the corresponding method/constructor having an additional varargs parameter or not having the varargs parameter.- Returns:
- The method/constructor corresponding to the designated method by having an additional
varargs parameter or not having the varargs parameter and the last parameter value
being
null
. If no such method/constructor is found or if there are several methods/constructors found matching this criteria,null
will be returned.
-
visitClassHierarchy
public static <T> T visitClassHierarchy(Class<?> cls, ReflectionTools.ClassVisitor<T> visitor, boolean includeTransIfaces)
Visits the (complete) class hierarchy (interfaces, parent classes and their interfaces as well as all parent interfaces if desired) of the designated class with the designated visitor.- Parameters:
cls
- The class for which to visit the hierarchy.visitor
- The visitor with which to visit each class of the hierarchy.includeTransIfaces
- Whether to include transitive interfaces. If so, parent interfaces of interfaces will be considered. Otherwise only the direct interfaces of this class and its parent classes will be considered.- Returns:
- The result of the visits.
-
getJavadocRefType
protected static StringBuilder getJavadocRefType(Class<?> cls)
Gets a string representation for the designated class used in Javadoc.- Parameters:
cls
- The class for which to get the javadoc-compatible string representation.- Returns:
- A
StringBuilder
containing the javadoc-compatible string representation of the designated class.
-
getJavadocRefString
public static String getJavadocRefString(Class<?> cls)
Gets the name and path of the Javadoc HTML file (relative, without leading "/" for the designated class.- Parameters:
cls
- The class for which to get the name and path of the corresponding Javadoc HTML file.- Returns:
- The name and path of the Javadoc HTML file (relative, without leading "/" for the designated class.
-
getJavadocRefString
public static String getJavadocRefString(Method m)
Gets fragment identifier of the designated method including the name and path of the Javadoc HTML file (relative, without leading "/" of the corresponding declaring class.- Parameters:
m
- The method for which to get the path and the fragment identifier of the corresponding Javadoc file.- Returns:
- The fragment identifier of the designated method including the name and path of the Javadoc HTML file (relative, without leading "/" of the corresponding declaring class.
-
-