public final class ReflectionHelper
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
COMMA_STRING |
private static java.lang.String |
EQUALS_STRING |
private static java.util.HashSet<java.lang.Character> |
ESCAPE_CHARACTERS |
private static char[] |
ILLEGAL_CHARACTERS |
private static java.lang.String |
QUOTE_STRING |
private static java.util.HashMap<java.lang.Character,java.lang.Character> |
REPLACE_CHARACTERS |
Constructor and Description |
---|
ReflectionHelper() |
Modifier and Type | Method and Description |
---|---|
private static void |
addAllGenericInterfaces(java.lang.Class<?> rawClass,
java.lang.reflect.Type type,
java.util.Set<java.lang.reflect.Type> closures)
Gets all the interfaces on this particular class (but not any
superclasses of this class).
|
private static void |
addAllInterfaceContracts(java.lang.reflect.Type interfaceType,
java.util.LinkedHashSet<java.lang.reflect.Type> addToMe) |
static void |
addMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadatas,
java.lang.String key,
java.lang.String value)
Adds a value to the list of values associated with this key
|
static boolean |
annotationContainsAll(java.util.Set<java.lang.annotation.Annotation> candidateAnnotations,
java.util.Set<java.lang.annotation.Annotation> requiredAnnotations)
This is used to check on the annotation set.
|
static <T> T |
cast(java.lang.Object o)
Casts this thing to the given type
|
static MethodWrapper |
createMethodWrapper(java.lang.reflect.Method wrapMe)
Creates a method wrapper for the given method
|
static java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
deepCopyMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.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)
|
private static java.lang.String |
escapeString(java.lang.String escapeMe) |
private static java.lang.reflect.ParameterizedType |
fillInPT(java.lang.reflect.ParameterizedType pt,
java.util.HashMap<java.lang.Class<?>,java.lang.reflect.ParameterizedType> class2TypeMap) |
private static java.lang.reflect.Type |
fixGenericArrayTypeVariables(java.lang.reflect.GenericArrayType type,
java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
Replace any TypeVariables in the given type's arguments with
the actual argument types.
|
private static java.lang.reflect.Type |
fixTypeVariables(java.lang.reflect.ParameterizedType type,
java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
Replace any TypeVariables in the given type's arguments with
the actual argument types.
|
static java.util.Set<java.lang.reflect.Type> |
getAdvertisedTypesFromClass(java.lang.reflect.Type type,
java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation)
Returns the set of types this class advertises
|
static java.util.Set<java.lang.reflect.Type> |
getAdvertisedTypesFromObject(java.lang.Object t,
java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation)
Returns the set of types this class advertises
|
private static void |
getAllContractsFromInterfaces(java.lang.Class<?> clazzOrInterface,
java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation,
java.util.Set<java.lang.reflect.Type> addToMe,
java.util.Set<java.lang.Class<?>> alreadyHandled) |
static java.util.Set<java.lang.reflect.Type> |
getAllTypes(java.lang.reflect.Type t) |
private static java.lang.Class<?> |
getArrayOfType(java.lang.Class<?> type) |
static java.util.Set<java.lang.String> |
getContractsFromClass(java.lang.Class<?> clazz,
java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation)
Returns the set of types this class advertises
|
private static java.lang.ClassLoader |
getCurrentContextClassLoader()
Gets the current context class loader with privs
|
static java.lang.reflect.Type |
getFirstTypeArgument(java.lang.reflect.Type type)
Gets the first type argument if this is a parameterized
type, otherwise it returns Object.class
|
static java.lang.String |
getName(java.lang.Class<?> implClass)
Returns the name that should be associated with this class
|
private static java.lang.String |
getNamedName(javax.inject.Named named,
java.lang.Class<?> implClass) |
static java.lang.String |
getNameFromAllQualifiers(java.util.Set<java.lang.annotation.Annotation> qualifiers,
java.lang.reflect.AnnotatedElement parent)
Gets the name from the &46;Named qualifier in this set of qualifiers
|
private static java.lang.reflect.Type[] |
getNewTypeArguments(java.lang.reflect.ParameterizedType type,
java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
Get a new array of type arguments for the given ParameterizedType, replacing any TypeVariables with
actual types.
|
private static java.lang.reflect.Type |
getNewTypeArrayArguments(java.lang.reflect.GenericArrayType gat,
java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
Get a new Type for a GenericArrayType, replacing any TypeVariables with
actual types.
|
static java.util.Set<java.lang.annotation.Annotation> |
getQualifierAnnotations(java.lang.reflect.AnnotatedElement annotatedGuy)
Gets all the qualifier annotations from the object
|
static java.util.Set<java.lang.String> |
getQualifiersFromClass(java.lang.Class<?> clazz)
Gets all the qualifiers from the object
|
static java.util.Set<java.lang.annotation.Annotation> |
getQualifiersFromObject(java.lang.Object t)
Gets all the qualifiers from the object
|
static java.lang.Class<?> |
getRawClass(java.lang.reflect.Type type)
Given the type parameter gets the raw type represented
by the type, or null if this has no associated raw class
|
static java.lang.annotation.Annotation |
getScopeAnnotationFromClass(java.lang.Class<?> clazz)
Gets the scope annotation from the object
|
static java.lang.annotation.Annotation |
getScopeAnnotationFromObject(java.lang.Object t)
Gets the scope annotation from the object
|
static java.lang.annotation.Annotation |
getScopeFromClass(java.lang.Class<?> clazz,
java.lang.annotation.Annotation annoDefault)
Gets the scope annotation from the object
|
static java.lang.annotation.Annotation |
getScopeFromObject(java.lang.Object t,
java.lang.annotation.Annotation annoDefault)
Gets the scope annotation from the object
|
private static java.util.Map<java.lang.String,java.lang.reflect.Type> |
getTypeArguments(java.lang.Class<?> rawClass,
java.lang.reflect.ParameterizedType type)
Gets a mapping of type variable names of the raw class to type arguments of the
parameterized type.
|
private static java.util.Set<java.lang.reflect.Type> |
getTypeClosure(java.lang.reflect.Type ofType)
Returns the type closure of the given class
|
static java.util.Set<java.lang.reflect.Type> |
getTypeClosure(java.lang.reflect.Type ofType,
java.util.Set<java.lang.String> contracts)
Returns the type closure, as restricted by the classes listed in the
set of contracts implemented
|
private static java.util.Set<java.lang.annotation.Annotation> |
internalGetQualifierAnnotations(java.lang.reflect.AnnotatedElement annotatedGuy) |
private static int |
internalReadSet(char[] asChars,
int startIndex,
java.util.Collection<java.lang.String> addToMe)
Writes a set in a way that can be read from an input stream as well.
|
static java.lang.Object |
invoke(java.lang.Object o,
java.lang.reflect.Method m,
java.lang.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 boolean |
isAnnotationAQualifier(java.lang.annotation.Annotation anno)
Returns true if the given annotation is a qualifier
|
private static boolean |
isFilledIn(java.lang.reflect.ParameterizedType pt) |
private static boolean |
isFilledIn(java.lang.reflect.ParameterizedType pt,
java.util.HashSet<java.lang.reflect.ParameterizedType> recursionKiller) |
static boolean |
isPrivate(java.lang.reflect.Member member)
Returns true if the underlying member is private
|
static boolean |
isStatic(java.lang.reflect.Member member)
Returns true if the underlying member is static
|
static java.lang.Object |
makeMe(java.lang.reflect.Constructor<?> c,
java.lang.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 void |
parseServiceMetadataString(java.lang.String metadataField,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadata)
This method parses the string that is found in the VService metadata field.
|
private static int |
readKeyStringListLine(char[] asChars,
int startIndex,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> addToMe) |
static void |
readMetadataMap(java.lang.String line,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> addToMe)
Writes a set in a way that can be read from an input stream as well
|
static void |
readSet(java.lang.String line,
java.util.Collection<java.lang.String> addToMe)
Writes a set in a way that can be read from an input stream as well.
|
static boolean |
removeAllMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadatas,
java.lang.String key)
Removes all the metadata values associated with key
|
static boolean |
removeMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadatas,
java.lang.String key,
java.lang.String value)
Removes the given value from the given key
|
static java.lang.reflect.Type |
resolveField(java.lang.Class<?> topclass,
java.lang.reflect.Field field)
Resolves the generic type of a field given the actual class being instantiated
|
static java.lang.reflect.Type |
resolveMember(java.lang.Class<?> topclass,
java.lang.reflect.Type lookingForType,
java.lang.Class<?> declaringClass)
Resolves the generic type of a type and declaring class given the actual class being instantiated
|
private static void |
setAccessible(java.lang.reflect.AccessibleObject ao)
Sets this accessible object to be accessible using the permissions of
the hk2-locator bundle (which will need the required grant)
|
private static void |
setContextClassLoader(java.lang.Thread t,
java.lang.ClassLoader l)
Sets the context classloader under the privileged of this class
|
static void |
setField(java.lang.reflect.Field field,
java.lang.Object instance,
java.lang.Object value)
Sets the given field to the given value
|
static java.lang.Class<?> |
translatePrimitiveType(java.lang.Class<?> type)
Converts the type to its java form, or returns the original
|
private static java.util.Map<java.lang.String,java.lang.reflect.Type> |
typesFromSubClassToDeclaringClass(java.lang.Class<?> topClass,
java.lang.Class<?> declaringClass) |
private static java.lang.String |
writeList(java.util.List<java.lang.String> list) |
static java.lang.String |
writeMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadata)
Used to write the metadata out
|
static java.lang.String |
writeSet(java.util.Set<?> set)
Writes a set in a way that can be read from an input stream as well
|
static java.lang.String |
writeSet(java.util.Set<?> set,
java.lang.Object excludeMe)
Writes a set in a way that can be read from an input stream as well
|
private static final java.util.HashSet<java.lang.Character> ESCAPE_CHARACTERS
private static final char[] ILLEGAL_CHARACTERS
private static final java.util.HashMap<java.lang.Character,java.lang.Character> REPLACE_CHARACTERS
private static final java.lang.String EQUALS_STRING
private static final java.lang.String COMMA_STRING
private static final java.lang.String QUOTE_STRING
public static java.lang.Class<?> getRawClass(java.lang.reflect.Type type)
type
- The type to find the raw class onpublic static java.lang.reflect.Type resolveField(java.lang.Class<?> topclass, java.lang.reflect.Field field)
topclass
- The instantiation class. Must not be nullfield
- The non-null field whose type to resolvepublic static java.lang.reflect.Type resolveMember(java.lang.Class<?> topclass, java.lang.reflect.Type lookingForType, java.lang.Class<?> declaringClass)
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 nullprivate static java.util.Map<java.lang.String,java.lang.reflect.Type> typesFromSubClassToDeclaringClass(java.lang.Class<?> topClass, java.lang.Class<?> declaringClass)
public static java.lang.reflect.Type getFirstTypeArgument(java.lang.reflect.Type type)
type
- The type to find the first type argument onprivate static java.lang.String getNamedName(javax.inject.Named named, java.lang.Class<?> implClass)
public static java.lang.String getName(java.lang.Class<?> implClass)
implClass
- The class to evaluateprivate static void addAllGenericInterfaces(java.lang.Class<?> rawClass, java.lang.reflect.Type type, java.util.Set<java.lang.reflect.Type> closures)
private static java.lang.reflect.Type fixTypeVariables(java.lang.reflect.ParameterizedType type, java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
private static java.lang.reflect.Type fixGenericArrayTypeVariables(java.lang.reflect.GenericArrayType type, java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
private static java.lang.Class<?> getArrayOfType(java.lang.Class<?> type)
private static java.lang.reflect.Type[] getNewTypeArguments(java.lang.reflect.ParameterizedType type, java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
private static java.lang.reflect.Type getNewTypeArrayArguments(java.lang.reflect.GenericArrayType gat, java.util.Map<java.lang.String,java.lang.reflect.Type> typeArgumentsMap)
private static java.util.Map<java.lang.String,java.lang.reflect.Type> getTypeArguments(java.lang.Class<?> rawClass, java.lang.reflect.ParameterizedType type)
private static java.util.Set<java.lang.reflect.Type> getTypeClosure(java.lang.reflect.Type ofType)
ofType
- The full type closure of the given class
with nothing omitted (normal case). May not be nullpublic static java.util.Set<java.lang.reflect.Type> getTypeClosure(java.lang.reflect.Type ofType, java.util.Set<java.lang.String> contracts)
ofType
- The type to checkcontracts
- The contracts this type is allowed to handlepublic static java.util.Set<java.lang.reflect.Type> getAdvertisedTypesFromClass(java.lang.reflect.Type type, java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation)
type
- The outer type to analyzemarkerAnnotation
- The annotation to use to discover the advertised typesprivate static void getAllContractsFromInterfaces(java.lang.Class<?> clazzOrInterface, java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation, java.util.Set<java.lang.reflect.Type> addToMe, java.util.Set<java.lang.Class<?>> alreadyHandled)
public static java.util.Set<java.lang.reflect.Type> getAdvertisedTypesFromObject(java.lang.Object t, java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation)
t
- the object we are analyzingmarkerAnnotation
- The annotation to use to discover the advertised typespublic static java.util.Set<java.lang.String> getContractsFromClass(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> markerAnnotation)
clazz
- the class we are analyzingmarkerAnnotation
- The annotation to use to discover annotated typespublic static java.lang.annotation.Annotation getScopeAnnotationFromObject(java.lang.Object t)
t
- The object to analyzepublic static java.lang.annotation.Annotation getScopeAnnotationFromClass(java.lang.Class<?> clazz)
clazz
- The class to analyzepublic static java.lang.annotation.Annotation getScopeFromObject(java.lang.Object t, java.lang.annotation.Annotation annoDefault)
t
- The object to analyzeannoDefault
- The default that this should have if no scope could be foundpublic static java.lang.annotation.Annotation getScopeFromClass(java.lang.Class<?> clazz, java.lang.annotation.Annotation annoDefault)
clazz
- The class to analyzeannoDefault
- The scope that should be returned if no scope could be foundpublic static boolean isAnnotationAQualifier(java.lang.annotation.Annotation anno)
anno
- The annotation to checkpublic static java.util.Set<java.lang.annotation.Annotation> getQualifiersFromObject(java.lang.Object t)
t
- The object to analyzepublic static java.util.Set<java.lang.String> getQualifiersFromClass(java.lang.Class<?> clazz)
clazz
- The class to analyzeprivate static java.util.Set<java.lang.annotation.Annotation> internalGetQualifierAnnotations(java.lang.reflect.AnnotatedElement annotatedGuy)
public static java.util.Set<java.lang.annotation.Annotation> getQualifierAnnotations(java.lang.reflect.AnnotatedElement annotatedGuy)
A 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
annotatedGuy
- The thing to analyzepublic static java.lang.String writeSet(java.util.Set<?> set)
set
- The set to writepublic static java.lang.String writeSet(java.util.Set<?> set, java.lang.Object excludeMe)
set
- The set to writeexcludeMe
- An object to exclude from the list of things writtenpublic static void readSet(java.lang.String line, java.util.Collection<java.lang.String> addToMe) throws java.io.IOException
line
- The line to readaddToMe
- The set to add the strings tojava.io.IOException
- On a failureprivate static int internalReadSet(char[] asChars, int startIndex, java.util.Collection<java.lang.String> addToMe) throws java.io.IOException
asChars
- The line to readaddToMe
- The set to add the strings tojava.io.IOException
- On a failureprivate static int readKeyStringListLine(char[] asChars, int startIndex, java.util.Map<java.lang.String,java.util.List<java.lang.String>> addToMe) throws java.io.IOException
java.io.IOException
public static void readMetadataMap(java.lang.String line, java.util.Map<java.lang.String,java.util.List<java.lang.String>> addToMe) throws java.io.IOException
line
- The line to readaddToMe
- The set to add the strings tojava.io.IOException
- On a failureprivate static java.lang.String escapeString(java.lang.String escapeMe)
private static java.lang.String writeList(java.util.List<java.lang.String> list)
public static java.lang.String writeMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadata)
metadata
- The metadata to externalizepublic static void addMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadatas, java.lang.String key, java.lang.String value)
metadatas
- The base metadata objectkey
- The key to which to add the value. May not be nullvalue
- The value to add. May not be nullpublic static boolean removeMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadatas, java.lang.String key, java.lang.String value)
metadatas
- The base metadata objectkey
- The key of the value to remove. May not be nullvalue
- The value to remove. May not be nullpublic static boolean removeAllMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadatas, java.lang.String key)
metadatas
- The base metadata objectkey
- The key of the metadata values to removepublic static java.util.Map<java.lang.String,java.util.List<java.lang.String>> deepCopyMetadata(java.util.Map<java.lang.String,java.util.List<java.lang.String>> copyMe)
copyMe
- The guy to copy (if null, null will be returned)public static void setField(java.lang.reflect.Field field, java.lang.Object instance, java.lang.Object value) throws java.lang.Throwable
field
- The non-null field to setinstance
- The non-null instance to set intovalue
- The value to which the field should be setjava.lang.Throwable
- If there was some exception while setting the fieldpublic static java.lang.Object invoke(java.lang.Object o, java.lang.reflect.Method m, java.lang.Object[] args, boolean neutralCCL) throws java.lang.Throwable
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 calljava.lang.Throwable
- The unwrapped throwable thrown by the methodpublic static boolean isStatic(java.lang.reflect.Member member)
member
- The non-null member to testprivate static void setContextClassLoader(java.lang.Thread t, java.lang.ClassLoader l)
t
- The thread on which to set the classloaderl
- The classloader to setprivate static void setAccessible(java.lang.reflect.AccessibleObject ao)
ao
- The object to changepublic static java.lang.Object makeMe(java.lang.reflect.Constructor<?> c, java.lang.Object[] args, boolean neutralCCL) throws java.lang.Throwable
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 calljava.lang.Throwable
- The unwrapped throwable thrown by the methodpublic static void parseServiceMetadataString(java.lang.String metadataField, java.util.Map<java.lang.String,java.util.List<java.lang.String>> metadata)
metadataField
- A non-null metadata field that normally comes from the Service
metadata fieldmetadata
- The metadata field to add tojava.lang.IllegalStateException
- if a string with an invalid format is foundpublic static java.lang.String getNameFromAllQualifiers(java.util.Set<java.lang.annotation.Annotation> qualifiers, java.lang.reflect.AnnotatedElement parent) throws java.lang.IllegalStateException
qualifiers
- The set of qualifiers that may or may not have Named in itparent
- The parent element for which we are searchingjava.lang.IllegalStateException
- If the parent is annotated with a blank Named but is not
a Class or a Fieldprivate static java.lang.ClassLoader getCurrentContextClassLoader()
public static boolean annotationContainsAll(java.util.Set<java.lang.annotation.Annotation> candidateAnnotations, java.util.Set<java.lang.annotation.Annotation> requiredAnnotations)
candidateAnnotations
- The candidate annotationsrequiredAnnotations
- The required annotationspublic static java.lang.Class<?> translatePrimitiveType(java.lang.Class<?> type)
type
- The type to convertpublic static boolean isPrivate(java.lang.reflect.Member member)
member
- The non-null member to testpublic static java.util.Set<java.lang.reflect.Type> getAllTypes(java.lang.reflect.Type t)
private static java.lang.reflect.ParameterizedType fillInPT(java.lang.reflect.ParameterizedType pt, java.util.HashMap<java.lang.Class<?>,java.lang.reflect.ParameterizedType> class2TypeMap)
private static boolean isFilledIn(java.lang.reflect.ParameterizedType pt, java.util.HashSet<java.lang.reflect.ParameterizedType> recursionKiller)
private static boolean isFilledIn(java.lang.reflect.ParameterizedType pt)
private static void addAllInterfaceContracts(java.lang.reflect.Type interfaceType, java.util.LinkedHashSet<java.lang.reflect.Type> addToMe)
public static MethodWrapper createMethodWrapper(java.lang.reflect.Method wrapMe)
wrapMe
- The non-null method to wrappublic static <T> T cast(java.lang.Object o)
o
- The thing to cast