Module org.glassfish.hk2.utilities
Class ReflectionHelper
java.lang.Object
org.glassfish.hk2.utilities.reflection.ReflectionHelper
- Author:
- jwells
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidAdds a value to the list of values associated with this keystatic booleanannotationContainsAll(Set<Annotation> candidateAnnotations, Set<Annotation> requiredAnnotations) This is used to check on the annotation set.static <T> TCasts this thing to the given typestatic MethodWrappercreateMethodWrapper(Method wrapMe) Creates a method wrapper for the given methoddeepCopyMetadata(Map<String, List<String>> copyMe) This method does a deep copy of the incoming meta-data, (which basically means we will also make copies of the value list)getAdvertisedTypesFromClass(Type type, Class<? extends Annotation> markerAnnotation) Returns the set of types this class advertisesgetAdvertisedTypesFromObject(Object t, Class<? extends Annotation> markerAnnotation) Returns the set of types this class advertisesgetAllTypes(Type t) getContractsFromClass(Class<?> clazz, Class<? extends Annotation> markerAnnotation) Returns the set of types this class advertisesstatic TypegetFirstTypeArgument(Type type) Gets the first type argument if this is a parameterized type, otherwise it returns Object.classstatic StringReturns the name that should be associated with this classstatic StringgetNameFromAllQualifiers(Set<Annotation> qualifiers, AnnotatedElement parent) Gets the name from the &46;Named qualifier in this set of qualifiersstatic Set<Annotation>getQualifierAnnotations(AnnotatedElement annotatedGuy) Gets all the qualifier annotations from the objectgetQualifiersFromClass(Class<?> clazz) Gets all the qualifiers from the objectstatic Set<Annotation>Gets all the qualifiers from the objectstatic Class<?>getRawClass(Type type) Given the type parameter gets the raw type represented by the type, or null if this has no associated raw classstatic AnnotationgetScopeAnnotationFromClass(Class<?> clazz) Gets the scope annotation from the objectstatic AnnotationGets the scope annotation from the objectstatic AnnotationgetScopeFromClass(Class<?> clazz, Annotation annoDefault) Gets the scope annotation from the objectstatic AnnotationgetScopeFromObject(Object t, Annotation annoDefault) Gets the scope annotation from the objectgetTypeClosure(Type ofType, Set<String> contracts) Returns the type closure, as restricted by the classes listed in the set of contracts implementedstatic ObjectThis version of invoke is CCL neutral (it will return with the same CCL as what it went in with)static booleanReturns true if the given annotation is a qualifierstatic booleanReturns true if the underlying member is privatestatic booleanReturns true if the underlying member is staticstatic ObjectmakeMe(Constructor<?> c, Object[] args, boolean neutralCCL) This version of invoke is CCL neutral (it will return with the same CCL as what it went in with)static voidThis method parses the string that is found in the VService metadata field.static voidWrites a set in a way that can be read from an input stream as wellstatic voidreadSet(String line, Collection<String> addToMe) Writes a set in a way that can be read from an input stream as well.static booleanRemoves all the metadata values associated with keystatic booleanRemoves the given value from the given keystatic TyperesolveField(Class<?> topclass, Field field) Resolves the generic type of a field given the actual class being instantiatedstatic TyperesolveKnownType(TypeVariable<?> userType, ParameterizedType knownType, Class<?> knownDeclaringClass) If you have a class that declares type variables (knownDeclaringClass) and the type the user has told us it should be (knownType) then return the replaced type for the given userType.static TyperesolveMember(Class<?> topclass, Type lookingForType, Class<?> declaringClass) Resolves the generic type of a type and declaring class given the actual class being instantiatedstatic voidSets the given field to the given valuestatic Class<?>translatePrimitiveType(Class<?> type) Converts the type to its java form, or returns the originalstatic StringwriteMetadata(Map<String, List<String>> metadata) Used to write the metadata outstatic StringWrites a set in a way that can be read from an input stream as wellstatic StringWrites a set in a way that can be read from an input stream as well
-
Constructor Details
-
ReflectionHelper
public ReflectionHelper()
-
-
Method Details
-
getRawClass
Given the type parameter gets the raw type represented by the type, or null if this has no associated raw class- Parameters:
type- The type to find the raw class on- Returns:
- The raw class associated with this type
-
resolveField
Resolves the generic type of a field given the actual class being instantiated- Parameters:
topclass- The instantiation class. Must not be nullfield- The non-null field whose type to resolve- Returns:
- The resolved field type by way of its subclasses. Will not return null, but may return the original fields generic type
-
resolveMember
Resolves the generic type of a type and declaring class given the actual class being instantiated- Parameters:
topclass- The instantiation class. Must not be nulllookingForType- The type to resolve. Must not be nulldeclaringClass- The class of the entity declaring the lookingForType. Must not be null- Returns:
- The resolved type by way of its subclasses. Will not return null but may return lookingForType if it could not be further resolved
-
resolveKnownType
public static Type resolveKnownType(TypeVariable<?> userType, ParameterizedType knownType, Class<?> knownDeclaringClass) If you have a class that declares type variables (knownDeclaringClass) and the type the user has told us it should be (knownType) then return the replaced type for the given userType. Returns null if there is no match for the userType- Parameters:
userType- The user type to replace. May not be nullknownType- The user defined known final type of the knownDeclaringClass. May not be nullknownDeclaringClass- The declaringClass for which knownType is the resolved ParameterizedType for it. May not be null- Returns:
- null if userType is not related, or the given hardened parameterized type from the knownType list
-
getFirstTypeArgument
Gets the first type argument if this is a parameterized type, otherwise it returns Object.class- Parameters:
type- The type to find the first type argument on- Returns:
- If this is a class, Object.class. If this is a parameterized type, the type of the first actual argument
-
getName
Returns the name that should be associated with this class- Parameters:
implClass- The class to evaluate- Returns:
- The name this class should have
-
getTypeClosure
Returns the type closure, as restricted by the classes listed in the set of contracts implemented- Parameters:
ofType- The type to checkcontracts- The contracts this type is allowed to handle- Returns:
- The type closure restricted to the contracts
-
getAdvertisedTypesFromClass
public static Set<Type> getAdvertisedTypesFromClass(Type type, Class<? extends Annotation> markerAnnotation) Returns the set of types this class advertises- Parameters:
type- The outer type to analyzemarkerAnnotation- The annotation to use to discover the advertised types- Returns:
- The type itself and the contracts it implements
-
getAdvertisedTypesFromObject
public static Set<Type> getAdvertisedTypesFromObject(Object t, Class<? extends Annotation> markerAnnotation) Returns the set of types this class advertises- Parameters:
t- the object we are analyzingmarkerAnnotation- The annotation to use to discover the advertised types- Returns:
- The type itself and the contracts it implements
-
getContractsFromClass
public static Set<String> getContractsFromClass(Class<?> clazz, Class<? extends Annotation> markerAnnotation) Returns the set of types this class advertises- Parameters:
clazz- the class we are analyzingmarkerAnnotation- The annotation to use to discover annotated types- Returns:
- The type itself and the contracts it implements
-
getScopeAnnotationFromObject
Gets the scope annotation from the object- Parameters:
t- The object to analyze- Returns:
- The class of the scope annotation
-
getScopeAnnotationFromClass
Gets the scope annotation from the object- Parameters:
clazz- The class to analyze- Returns:
- The class of the scope annotation
-
getScopeFromObject
Gets the scope annotation from the object- Parameters:
t- The object to analyzeannoDefault- The default that this should have if no scope could be found- Returns:
- The class of the scope annotation
-
getScopeFromClass
Gets the scope annotation from the object- Parameters:
clazz- The class to analyzeannoDefault- The scope that should be returned if no scope could be found- Returns:
- The class of the scope annotation
-
isAnnotationAQualifier
Returns true if the given annotation is a qualifier- Parameters:
anno- The annotation to check- Returns:
- true if this is an annotation
-
getQualifiersFromObject
Gets all the qualifiers from the object- Parameters:
t- The object to analyze- Returns:
- The set of qualifiers. Will not return null but may return an empty set
-
getQualifiersFromClass
Gets all the qualifiers from the object- Parameters:
clazz- The class to analyze- Returns:
- The set of qualifiers. Will not return null but may return an empty set
-
getQualifierAnnotations
Gets all the qualifier annotations from the objectA strange behavior of this method is that if the annotatedGuy is a field and that field has the Named annotation on it with no value, then that Named annotation will NOT be added to the return list. This is because we have no access at this level to AnnotationLiteral, and hence cannot create a NamedImpl with which to fix the annotation. It is the responsibility of the caller of this method to add in the NamedImpl in that circumstance
- Parameters:
annotatedGuy- The thing to analyze- Returns:
- The set of qualifiers. Will not return null but may return an empty set
-
writeSet
Writes a set in a way that can be read from an input stream as well- Parameters:
set- The set to write- Returns:
- a representation of a list
-
writeSet
Writes a set in a way that can be read from an input stream as well- Parameters:
set- The set to writeexcludeMe- An object to exclude from the list of things written- Returns:
- a representation of a list
-
readSet
Writes a set in a way that can be read from an input stream as well. The values in the set may not contain the characters "{},"- Parameters:
line- The line to readaddToMe- The set to add the strings to- Throws:
IOException- On a failure
-
readMetadataMap
public static void readMetadataMap(String line, Map<String, List<String>> addToMe) throws IOExceptionWrites a set in a way that can be read from an input stream as well- Parameters:
line- The line to readaddToMe- The set to add the strings to- Throws:
IOException- On a failure
-
writeMetadata
Used to write the metadata out- Parameters:
metadata- The metadata to externalize- Returns:
- The metadata in an externalizable format
-
addMetadata
Adds a value to the list of values associated with this key- Parameters:
metadatas- The base metadata objectkey- The key to which to add the value. May not be nullvalue- The value to add. May not be null
-
removeMetadata
Removes the given value from the given key- Parameters:
metadatas- The base metadata objectkey- The key of the value to remove. May not be nullvalue- The value to remove. May not be null- Returns:
- true if the value was removed
-
removeAllMetadata
Removes all the metadata values associated with key- Parameters:
metadatas- The base metadata objectkey- The key of the metadata values to remove- Returns:
- true if any value was removed
-
deepCopyMetadata
This method does a deep copy of the incoming meta-data, (which basically means we will also make copies of the value list)- Parameters:
copyMe- The guy to copy (if null, null will be returned)- Returns:
- A deep copy of the metadata
-
setField
Sets the given field to the given value- Parameters:
field- The non-null field to setinstance- The non-null instance to set intovalue- The value to which the field should be set- Throws:
Throwable- If there was some exception while setting the field
-
invoke
This version of invoke is CCL neutral (it will return with the same CCL as what it went in with)- Parameters:
m- the method to invokeo- the object on which to invoke itargs- The arguments to invoke (may not be null)neutralCCL- true if the ContextClassLoader shoult remain null with this call- Returns:
- The return from the invocation
- Throws:
Throwable- The unwrapped throwable thrown by the method
-
isStatic
Returns true if the underlying member is static- Parameters:
member- The non-null member to test- Returns:
- true if the member is static
-
makeMe
This version of invoke is CCL neutral (it will return with the same CCL as what it went in with)- Parameters:
c- the constructor to callargs- The arguments to invoke (may not be null)neutralCCL- true if the context class loader should remain null through this call- Returns:
- The return from the invocation
- Throws:
Throwable- The unwrapped throwable thrown by the method
-
parseServiceMetadataString
public static void parseServiceMetadataString(String metadataField, Map<String, List<String>> metadata) This method parses the string that is found in the VService metadata field.- Parameters:
metadataField- A non-null metadata field that normally comes from the Service metadata fieldmetadata- The metadata field to add to- Throws:
IllegalStateException- if a string with an invalid format is found
-
getNameFromAllQualifiers
public static String getNameFromAllQualifiers(Set<Annotation> qualifiers, AnnotatedElement parent) throws IllegalStateException Gets the name from the &46;Named qualifier in this set of qualifiers- Parameters:
qualifiers- The set of qualifiers that may or may not have Named in itparent- The parent element for which we are searching- Returns:
- null if no Named was found, or the appropriate name otherwise
- Throws:
IllegalStateException- If the parent is annotated with a blank Named but is not a Class or a Field
-
annotationContainsAll
public static boolean annotationContainsAll(Set<Annotation> candidateAnnotations, Set<Annotation> requiredAnnotations) This is used to check on the annotation set. It must be done under protection because the annotations may attempt to discover if they are equal using getDeclaredMembers permission- Parameters:
candidateAnnotations- The candidate annotationsrequiredAnnotations- The required annotations- Returns:
- true if the candidate set contains the entire required set
-
translatePrimitiveType
Converts the type to its java form, or returns the original- Parameters:
type- The type to convert- Returns:
- The translated type or the type itself
-
isPrivate
Returns true if the underlying member is private- Parameters:
member- The non-null member to test- Returns:
- true if the member is private
-
getAllTypes
-
createMethodWrapper
Creates a method wrapper for the given method- Parameters:
wrapMe- The non-null method to wrap- Returns:
- A method wrapper that has a proper equals/hashCode
-
cast
Casts this thing to the given type- Parameters:
o- The thing to cast- Returns:
- A casted version of o
-