Package org.jboss.weld.util.reflection
Class Reflections
java.lang.Object
org.jboss.weld.util.reflection.Reflections
Utility class for static reflection-type operations
- Author:
- Pete Muir, Ales Justin, Marko Luksa
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Annotation[]static final Class<?>[]static final Type[] -
Method Summary
Modifier and TypeMethodDescriptionbuildTypeMap(Set<Type> types) static <T> Tstatic voidTriggers loading of declaring class (if any) of the given class recursively.static booleancontainsWildcards(Type[] types) static Stringdecapitalize(String name) Copy of java.beans.Introspector#decapitalize(name) to reduce java.desktop module dependency.static voidensureAccessible(AccessibleObject accessibleObject) Set theaccessibleflag for this accessible object.static voidensureAccessible(AccessibleObject accessibleObject, Object instance) Set theaccessibleflag for this accessible object.static MethodfindDeclaredMethodByName(Class<?> clazz, String methodName) Searches for a declared method with a given name.static <T extends AccessibleObject & Member>
TgetAccessibleCopyOfMember(T member) Creates a copy of the given Java member (field, method, constructor) and makes it accessible.static Type[]getActualTypeArguments(Class<?> clazz) Gets the actual type arguments of a classstatic Type[]getActualTypeArguments(Type type) Gets the actual type arguments of a TypegetInterfaceClosure(Class<?> clazz) static intgetNesting(Class<?> clazz) static MethodgetNonPrivateNonStaticFinalMethod(Class<?> type) static StringgetPropertyName(Method method) Gets the property name from a getter method.static <T> Class<T>getRawType(Type type) static booleanhasDeclaredField(Class<?> javaClass, String name) Checks if given Java class contains a declared field with provided name.static <T> TinvokeAndUnwrap(Object instance, Method method, Object... parameters) Invokes the method on a given instance passing in given parameters.static booleanisAbstract(Class<?> clazz) static booleanisAbstract(Method method) Checks if a method is abstractstatic booleanisArrayType(Class<?> rawType) Checks if raw type is array typestatic booleanisCacheable(Annotation[] annotations) static booleanisCacheable(Collection<Annotation> annotations) static booleanisClassLoadable(String className, org.jboss.weld.resources.spi.ResourceLoader resourceLoader) static booleanstatic booleanChecks if class is finalstatic booleanChecks if member is finalstatic booleanisPackagePrivate(int mod) static booleanisParameterizedType(Class<?> type) Checks if type is parameterized typestatic booleanisParameterizedTypeWithWildcard(Class<?> type) static booleanisPrimitive(Type type) static booleanChecks if member is privatestatic booleanisSerializable(Class<?> clazz) static booleanChecks if type is staticstatic booleanChecks if member is staticstatic booleanisStaticNestedClass(Class<?> javaClass) static booleanisTopLevelOrStaticNestedClass(Class<?> javaClass) static booleanisTransient(Member member) static booleanisTypeOrAnyMethodFinal(Class<?> type) Checks if type or member is finalstatic booleanisUnboundedTypeVariable(Type type) static booleanisUnboundedWildcard(Type type) static <T> Class<T>Tries to load a class using the specified ResourceLoader.static FieldlookupField(Class<?> javaClass, String fieldName) Looks up a field with given name within the class and any of its superclasses.static MethodlookupMethod(Class<?> javaClass, String methodName, Class<?>[] parameterTypes) Looks up a method with given name and parameters within the class and any of its superclasses.static ExceptionUnwraps the givenInvocationTargetException.static MethodwrapException(Class<?> javaClass, String methodName, Class<?>... parameterTypes) Attempts to look up a method based on provided name and parameter types within given class.
-
Field Details
-
EMPTY_TYPES
-
EMPTY_ANNOTATIONS
-
EMPTY_CLASSES
-
-
Method Details
-
buildTypeMap
-
isCacheable
-
isCacheable
-
cast
-
getPropertyName
Gets the property name from a getter method. We extend JavaBean conventions, allowing the getter method to have parameters- Parameters:
method- The getter method- Returns:
- The name of the property. Returns null if method wasn't JavaBean getter-styled
-
isFinal
Checks if class is final- Parameters:
clazz- The class to check- Returns:
- True if final, false otherwise
-
getNesting
-
isFinal
Checks if member is final- Parameters:
member- The member to check- Returns:
- True if final, false otherwise
-
isPrivate
Checks if member is private- Parameters:
member- The member to check- Returns:
- True if final, false otherwise
-
isTypeOrAnyMethodFinal
Checks if type or member is final- Parameters:
type- Type or member- Returns:
- True if final, false otherwise
-
getNonPrivateNonStaticFinalMethod
-
isPackagePrivate
public static boolean isPackagePrivate(int mod) -
isStatic
Checks if type is static- Parameters:
type- Type to check- Returns:
- True if static, false otherwise
-
isStatic
Checks if member is static- Parameters:
member- Member to check- Returns:
- True if static, false otherwise
-
isTransient
-
isAbstract
Checks if a method is abstract- Parameters:
method- the method- Returns:
- true if abstract
-
isAbstract
-
getActualTypeArguments
Gets the actual type arguments of a class- Parameters:
clazz- The class to examine- Returns:
- The type arguments
-
getActualTypeArguments
Gets the actual type arguments of a Type- Parameters:
type- The type to examine- Returns:
- The type arguments
-
isArrayType
Checks if raw type is array type- Parameters:
rawType- The raw type to check- Returns:
- True if array, false otherwise
-
isParameterizedType
Checks if type is parameterized type- Parameters:
type- The type to check- Returns:
- True if parameterized, false otherwise
-
isParameterizedTypeWithWildcard
-
containsWildcards
-
isSerializable
-
isPrimitive
-
getRawType
-
isClassLoadable
public static boolean isClassLoadable(String className, org.jboss.weld.resources.spi.ResourceLoader resourceLoader) -
loadClass
public static <T> Class<T> loadClass(String className, org.jboss.weld.resources.spi.ResourceLoader resourceLoader) Tries to load a class using the specified ResourceLoader. Returns null if the class is not found.- Parameters:
className-resourceLoader-- Returns:
- the loaded class or null if the given class cannot be loaded
-
isUnboundedWildcard
-
isUnboundedTypeVariable
-
isStaticNestedClass
- Parameters:
javaClass-- Returns:
trueif the given class is a static nested class,falseotherwise
-
isTopLevelOrStaticNestedClass
- Parameters:
javaClass-- Returns:
trueif the given class is a top-level or static nested class,falseotherwise
-
invokeAndUnwrap
public static <T> T invokeAndUnwrap(Object instance, Method method, Object... parameters) throws Throwable Invokes the method on a given instance passing in given parameters. If the invocation yieldsInvocationTargetException, the exception is unwrapped. It is a responsibility of the caller to make sure that the method is accessible to the caller.- Throws:
Throwable
-
checkDeclaringClassLoadable
Triggers loading of declaring class (if any) of the given class recursively. If the class cannot be loaded, the underlyingLinkageErroris propagated.- Parameters:
class- the given class- Throws:
LinkageError- or its subclass if a declaring class cannot be loaded
-
findDeclaredMethodByName
Searches for a declared method with a given name. If the class declares multiple methods with the given name, there is no guarantee as of which methods is returned. Null is returned if the class does not declare a method with the given name.- Parameters:
clazz- the given classmethodName- the given method name- Returns:
- method with the given name declared by the given class or null if no such method exists
-
unwrapInvocationTargetException
public static Exception unwrapInvocationTargetException(InvocationTargetException e) throws Exception Unwraps the givenInvocationTargetException.ErrorandExceptionare unwrapped right away,Throwableis wrapped withinWeldException. This method never returns - it always throws the unwrapped cause instead. The return type matches the throws part of the signature just to simplify usage.- Parameters:
the- given exception- Returns:
- Throws:
Exception- unwrapped cause ofInvocationTargetException
-
getInterfaceClosure
-
isDefault
-
decapitalize
Copy of java.beans.Introspector#decapitalize(name) to reduce java.desktop module dependency. Utility method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".
- Parameters:
name- The string to be decapitalized.- Returns:
- The decapitalized version of the string.
-
hasDeclaredField
Checks if given Java class contains a declared field with provided name.- Parameters:
javaClass- java classname- name of the field- Returns:
- true if the class declares a field with provided name; false otherwise
-
ensureAccessible
Set theaccessibleflag for this accessible object. UsesAccessibleObject.isAccessible()to check accessibility.- Parameters:
accessibleObject-
-
ensureAccessible
Set theaccessibleflag for this accessible object. UsesAccessibleObject.canAccess(Object)to check accessibility.- Parameters:
accessibleObject-instance- instance of the accessible object you are trying to access
-
getAccessibleCopyOfMember
Creates a copy of the given Java member (field, method, constructor) and makes it accessible.- Type Parameters:
T-- Parameters:
member-- Returns:
-
lookupField
Looks up a field with given name within the class and any of its superclasses.- Parameters:
javaClass-fieldName-- Returns:
- Throws:
NoSuchMethodExceptionNoSuchFieldException
-
lookupMethod
public static Method lookupMethod(Class<?> javaClass, String methodName, Class<?>[] parameterTypes) throws NoSuchMethodException Looks up a method with given name and parameters within the class and any of its superclasses.- Parameters:
javaClass-methodName-parameterTypes-- Returns:
- Throws:
NoSuchMethodException
-
wrapException
public static Method wrapException(Class<?> javaClass, String methodName, Class<?>... parameterTypes) Attempts to look up a method based on provided name and parameter types within given class. If the method exists, it is returned; otherwise, the exception throws is wrapped inReflectionLogger.noSuchMethodWrapper(NoSuchMethodException, String)- Parameters:
javaClass- class that should contain the methodmethodName- method nameparameterTypes- method parameter types- Returns:
-