Class ClassInspector
java.lang.Object
org.apache.derby.iapi.services.loader.ClassInspector
Methods to find out relationships between classes and methods within a class.
All class names within this interface are treated as java language class names,
e.g. int, COM.foo.Myclass, int[], java.lang.Object[]. That is java internal
class names as defined in the class file format are not understood.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
accessible
(String className) Does the named class exist, and is it accessible?private void
addResolvedTypes
(HashMap<Type, Type> resolvedTypes, Type genericType) Given a generic type, add its parameter types to an evolving map of resolved types.boolean
assignableTo
(String fromClassName, String toClassName) Is one named class assignable to another named class or interface?private Class[]
Get the bounds for a single type variable.protected boolean
classConvertableFromTo
(Class fromClass, Class toClass, boolean mixTypes) Can we convert a fromClass to toClass.findPublicConstructor
(String receiverType, String[] parmTypes, String[] primParmTypes, boolean[] isParam) Find a public constructor that implements a given signature.findPublicField
(String receiverType, String fieldName, boolean staticField) Find a public field for a class.findPublicMethod
(String receiverType, String methodName, String[] parmTypes, String[] primParmTypes, boolean[] isParam, boolean staticMethod, boolean repeatLastParameter, boolean hasVarargs) Find a public method that implements a given signature.private Class[][]
Get the type bounds for all of the type variables of the given parameterized type.Class
<?> Get (load) the class for the given class name.getDeclaringClass
(Member method) Get the declaring class for a method.Class<?>[]
getGenericParameterTypes
(Class parameterizedType, Class implementation) Given an implementation of a parameterized interface, return the actual types of the interface type variables.getParameterTypes
(Class<?> parameterizedType, HashMap<Type, Type> resolvedTypes) Given a map of resolved types, compose them together in order to resolve the actual concrete types that are plugged into the parameterized type.String[]
getParameterTypes
(Member method) Get the parameter types for a method described by a Member as a String[].private Class
getRawType
(Type bound) Get the raw type of a type bound.getResolvedTypes
(ArrayList<Class<?>> chain) Given an inheritance chain of types, stretching from a superclass down to a terminal concrete class, construct a map of generic types to their resolved types.Get the Java name of the return type from a Member representing a method or the type of a Member representing a field.Class[][]
getTypeBounds
(Class parameterizedInterface, Class implementation) Given an implementation of a parameterized interface, return the bounds on the type variables.getTypeChain
(Class<?> chainEnd, Class<?> start) Construct an inheritance chain of types stretching from a supertype down to a concrete implementation.boolean
instanceOf
(String className, Object obj) Is the given object an instance of the named class?boolean
isVarArgsMethod
(Member member) Return true if the method or constructor supports varargs.static boolean
primitiveType
(String typeName) Determine whether a type is a Java primitive, like int or booleanstatic String
readableClassName
(Class clazz) Translate a JVM-style type descriptor to a Java-language-style type name.private Member
resolveMethod
(Class receiverClass, String methodName, Class[] paramClasses, Class[] primParamClasses, boolean[] isParam, boolean staticMethod, boolean repeatLastParameter, Member[] methods, boolean hasVarargs) Tricky function to resolve a method.private boolean
signatureConvertableFromTo
(Class[] fromTypes, Class[] primFromTypes, Class[] toTypes, boolean[] isParam, boolean mixTypes) Can we convert a signature from fromTypes(primFromTypes) to toTypes.
-
Field Details
-
primTypeNames
-
nonPrimTypeNames
-
OBJECT_TYPE_NAME
- See Also:
-
STRING_TYPE_NAME
- See Also:
-
BIGDECIMAL_TYPE_NAME
- See Also:
-
cf
-
-
Constructor Details
-
ClassInspector
DO NOT USE! use the method in ClassFactory.
-
-
Method Details
-
instanceOf
Is the given object an instance of the named class?- Parameters:
className
- The name of the classobj
- The object to test to see if it's an instance of the named class- Returns:
- true if obj is an instanceof className, false if not
- Throws:
ClassNotFoundException
-
assignableTo
Is one named class assignable to another named class or interface?- Parameters:
fromClassName
- The name of the class to be assignedtoClassName
- The name of the class to be assigned to- Returns:
- true if an object of type fromClass can be assigned to an object of type toClass, false if not.
-
accessible
Does the named class exist, and is it accessible?- Parameters:
className
- The name of the class to test for existence- Returns:
- true if the class exists and is accessible, false if not
- Throws:
ClassNotFoundException
-
getType
Get the Java name of the return type from a Member representing a method or the type of a Member representing a field.- Parameters:
member
- A Member representing the method for which we want the return type.- Returns:
- A Java-language-style string describing the return type of the method (for example, it returns "int" instead of "I".
-
findPublicMethod
public Member findPublicMethod(String receiverType, String methodName, String[] parmTypes, String[] primParmTypes, boolean[] isParam, boolean staticMethod, boolean repeatLastParameter, boolean hasVarargs) throws ClassNotFoundException, StandardException Find a public method that implements a given signature. The signature is given using the full Java class names of the types.
A untyped null parameter is indicated by passing in an empty string ("") as its class name.
If receiverType represents an interface then the methods of java.lang.Object are included in the candidate list.
If the caller is simply checking to see that a public method with the specified name exists, regardless of the signature, exists, then the caller should pass in a null for parmTypes. (This is useful for checking the validity of a method alias when creating one.)
We use a two-pass algorithm to resolve methods. In the first pass, we use all "object" types to try to match a method. If this fails, in the second pass, an array of "primitive" types (if the parameter has one, otherwise the same object type is used) is passed in, as well as the "object" type array. For each parameter of a method, we try to match it against either the "object" type, or the "primitive" type. Of all the qualified candidate methods found, we choose the closest one to the input parameter types. This involves comparing methods whose parameters are mixed "object" and "primitive" types in the second pass. This is eventually handled in classConvertableFromTo.- Parameters:
receiverType
- The class name of the receivermethodName
- The name of the methodparmTypes
- An array of class names representing the parameter types. Pass a zero-element array if there are no parameters. Pass a null if it is okay to match any signature.primParmTypes
- This is used in the second pass of the two-pass method resolution algorithm. Use primitive type if it has one, otherwise use same object typeisParam
- Array of booleans telling whether parameter is a ?.staticMethod
- Find a static method.repeatLastParameter
- If true the last parameter may be repeated any number of times (total count must be greater than one). If false the last parameter is matched as usual. This also requires an exact match on the last parameter type.- Returns:
- A Member representing the matching method. Returns null if no such method.
- Throws:
ClassNotFoundException
- One or more of the classes does not exist.StandardException
- Thrown on ambiguous method invocation.- See Also:
-
findPublicField
public Member findPublicField(String receiverType, String fieldName, boolean staticField) throws StandardException Find a public field for a class. This follows the semantics of the java compiler for locating a field. This means if a field fieldName exists in the class with package, private or protected then an error is raised. Even if the field hides a field fieldName in a super-class/super--interface. See the JVM spec on fields.- Parameters:
receiverType
- The class name of the receiverfieldName
- The name of the fieldstaticField
- Find a static field- Returns:
- A Member representing the matching field.
- Throws:
StandardException
- Class or field does not exist or is not public or a security exception.- See Also:
-
findPublicConstructor
public Member findPublicConstructor(String receiverType, String[] parmTypes, String[] primParmTypes, boolean[] isParam) throws ClassNotFoundException, StandardException Find a public constructor that implements a given signature. The signature is given using the full Java class names of the types.
A untyped null parameter is indicated by passing in an empty string ("") as its class name.- Parameters:
receiverType
- The class name of the receiverparmTypes
- An array of class names representing the parameter types. Pass a zero-element array if there are no parameters.primParmTypes
- This is used in the second pass of the two-pass method resolution algorithm. Use primitive type if it has one, otherwise use same object typeisParam
- Array of booleans telling whether parameter is a ?.- Returns:
- A Member representing the matching constructor. Returns null if no such constructor.
- Throws:
ClassNotFoundException
- One or more of the classes does not exist.StandardException
- Thrown on ambiguous constructor invocation.- See Also:
-
getTypeBounds
public Class[][] getTypeBounds(Class parameterizedInterface, Class implementation) throws StandardException Given an implementation of a parameterized interface, return the bounds on the type variables. May return null if type resolution fails.- Throws:
StandardException
-
isVarArgsMethod
Return true if the method or constructor supports varargs. -
getGenericParameterTypes
public Class<?>[] getGenericParameterTypes(Class parameterizedType, Class implementation) throws StandardException Given an implementation of a parameterized interface, return the actual types of the interface type variables. May return null or an array of nulls if type resolution fails.- Throws:
StandardException
-
getParameterTypes
Get the parameter types for a method described by a Member as a String[].- Parameters:
method
- A Member describing a method- Returns:
- A String[] describing the parameters of the method
-
primitiveType
Determine whether a type is a Java primitive, like int or boolean- Parameters:
typeName
- The name of the Java type- Returns:
- true if it's a primitive type
-
resolveMethod
private Member resolveMethod(Class receiverClass, String methodName, Class[] paramClasses, Class[] primParamClasses, boolean[] isParam, boolean staticMethod, boolean repeatLastParameter, Member[] methods, boolean hasVarargs) throws StandardException Tricky function to resolve a method. If primParamClasses is null we know it's first pass. First pass try to match as all "object" types, second pass try to match any combination of "object" and "primitive" types. Find the closest match among all the qualified candidates. If there's a tie, it's ambiguous. The preceding paragraph is a bit misleading. As of release 10.4, the second pass did not consider arbitrary combinations of primitive and wrapper types. This is because the first pass removed from consideration candidates which would be allowed under ANSI rules. As a fix for bug DERBY-3652, we now allow primitive and wrapper type matches during the first pass. The ANSI rules are documented in DERBY-3652.- Parameters:
receiverClass
- the class who holds the methodsmethodName
- the name of methodparamClasses
- object type classes of input parametersprimParamClasses
- primitive type classes or nullisParam
- isParam (for ?) arraystaticMethod
- static method or notmethods
- method stack- Returns:
- the matched method
- Throws:
StandardException
-
getClass
Get (load) the class for the given class name. This method converts any java language class name into a Class object. This includes cases like String[] and primitive types. This will attempt to load the class from the application set.- Throws:
ClassNotFoundException
- Class cannot be found, or a SecurityException or LinkageException was thrown loading the class.
-
signatureConvertableFromTo
private boolean signatureConvertableFromTo(Class[] fromTypes, Class[] primFromTypes, Class[] toTypes, boolean[] isParam, boolean mixTypes) Can we convert a signature from fromTypes(primFromTypes) to toTypes. "mixTypes" is a flag to show if object/primitive type conversion is possible; this is used for comparing two candidate methods in the second pass of the two pass method resolution.- Parameters:
fromTypes
- from types' classesprimFromTypes
- primitive from types or nulltoTypes
- to types' classesisParam
- is parameter (?) or notmixTypes
- mixing object/primitive types for comparison
-
classConvertableFromTo
Can we convert a fromClass to toClass. "mixTypes" is a flag to show if object/primitive type conversion is possible; this is used for comparing two candidate methods in the second pass of the two pass method resolution.- Parameters:
fromClass
- from classtoClass
- to classmixTypes
- mixing object/primitive types for comparison
-
readableClassName
Translate a JVM-style type descriptor to a Java-language-style type name.- Parameters:
clazz
- The String that contains the JVM type name- Returns:
- The Java-language-style type name
-
getDeclaringClass
Get the declaring class for a method.- Parameters:
method
- A Member describing a method- Returns:
- A String with the declaring class
- See Also:
-
findTypeBounds
Get the type bounds for all of the type variables of the given parameterized type. -
boundType
Get the bounds for a single type variable. -
getRawType
Get the raw type of a type bound. -
getTypeChain
Construct an inheritance chain of types stretching from a supertype down to a concrete implementation. -
getResolvedTypes
Given an inheritance chain of types, stretching from a superclass down to a terminal concrete class, construct a map of generic types to their resolved types. -
addResolvedTypes
Given a generic type, add its parameter types to an evolving map of resolved types. Some of the resolved types may be generic type variables which will need further resolution from other generic types. -
getParameterTypes
private ArrayList<Class<?>> getParameterTypes(Class<?> parameterizedType, HashMap<Type, Type> resolvedTypes) Given a map of resolved types, compose them together in order to resolve the actual concrete types that are plugged into the parameterized type.
-