Class ReflectionHelper

java.lang.Object
org.glassfish.hk2.utilities.reflection.ReflectionHelper

public final class ReflectionHelper extends Object
  • Field Details

  • Constructor Details

    • ReflectionHelper

      public ReflectionHelper()
  • Method Details

    • getRawClass

      public static Class<?> getRawClass(Type type)
      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

      public static Type resolveField(Class<?> topclass, Field field)
      Resolves the generic type of a field given the actual class being instantiated
      Parameters:
      topclass - The instantiation class. Must not be null
      field - 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

      public static Type resolveMember(Class<?> topclass, Type lookingForType, Class<?> declaringClass)
      Resolves the generic type of a type and declaring class given the actual class being instantiated
      Parameters:
      topclass - The instantiation class. Must not be null
      lookingForType - The type to resolve. Must not be null
      declaringClass - 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 null
      knownType - The user defined known final type of the knownDeclaringClass. May not be null
      knownDeclaringClass - 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
    • typesFromSubClassToDeclaringClass

      private static Map<String,Type> typesFromSubClassToDeclaringClass(Class<?> topClass, Class<?> declaringClass)
    • getFirstTypeArgument

      public static Type getFirstTypeArgument(Type type)
      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
    • getNamedName

      private static String getNamedName(javax.inject.Named named, Class<?> implClass)
    • getName

      public static String getName(Class<?> implClass)
      Returns the name that should be associated with this class
      Parameters:
      implClass - The class to evaluate
      Returns:
      The name this class should have
    • addAllGenericInterfaces

      private static void addAllGenericInterfaces(Class<?> rawClass, Type type, Set<Type> closures)
      Gets all the interfaces on this particular class (but not any superclasses of this class).
    • fixTypeVariables

      private static Type fixTypeVariables(ParameterizedType type, Map<String,Type> typeArgumentsMap)
      Replace any TypeVariables in the given type's arguments with the actual argument types. Return the given type if no replacing is required.
    • fixGenericArrayTypeVariables

      private static Type fixGenericArrayTypeVariables(GenericArrayType type, Map<String,Type> typeArgumentsMap)
      Replace any TypeVariables in the given type's arguments with the actual argument types. Return the given type if no replacing is required.
    • getArrayOfType

      private static Class<?> getArrayOfType(Class<?> type)
    • getNewTypeArguments

      private static Type[] getNewTypeArguments(ParameterizedType type, Map<String,Type> typeArgumentsMap)
      Get a new array of type arguments for the given ParameterizedType, replacing any TypeVariables with actual types. The types should be found in the given arguments map, keyed by variable name. Return null if no arguments needed to be replaced.
    • getNewTypeArrayArguments

      private static Type getNewTypeArrayArguments(GenericArrayType gat, Map<String,Type> typeArgumentsMap)
      Get a new Type for a GenericArrayType, replacing any TypeVariables with actual types. The types should be found in the given arguments map, keyed by variable name. Return null if no arguments needed to be replaced.
    • getTypeArguments

      private static Map<String,Type> getTypeArguments(Class<?> rawClass, ParameterizedType type)
      Gets a mapping of type variable names of the raw class to type arguments of the parameterized type.
    • getTypeClosure

      private static Set<Type> getTypeClosure(Type ofType)
      Returns the type closure of the given class
      Parameters:
      ofType - The full type closure of the given class with nothing omitted (normal case). May not be null
      Returns:
      The non-null (and never empty) set of classes that this class can be assigned to
    • getTypeClosure

      public static Set<Type> getTypeClosure(Type ofType, Set<String> contracts)
      Returns the type closure, as restricted by the classes listed in the set of contracts implemented
      Parameters:
      ofType - The type to check
      contracts - 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 analyze
      markerAnnotation - The annotation to use to discover the advertised types
      Returns:
      The type itself and the contracts it implements
    • getAllContractsFromInterfaces

      private static void getAllContractsFromInterfaces(Class<?> clazzOrInterface, Class<? extends Annotation> markerAnnotation, Set<Type> addToMe, Set<Class<?>> alreadyHandled)
    • 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 analyzing
      markerAnnotation - 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 analyzing
      markerAnnotation - The annotation to use to discover annotated types
      Returns:
      The type itself and the contracts it implements
    • getScopeAnnotationFromObject

      public static Annotation getScopeAnnotationFromObject(Object t)
      Gets the scope annotation from the object
      Parameters:
      t - The object to analyze
      Returns:
      The class of the scope annotation
    • getScopeAnnotationFromClass

      public static Annotation getScopeAnnotationFromClass(Class<?> clazz)
      Gets the scope annotation from the object
      Parameters:
      clazz - The class to analyze
      Returns:
      The class of the scope annotation
    • getScopeFromObject

      public static Annotation getScopeFromObject(Object t, Annotation annoDefault)
      Gets the scope annotation from the object
      Parameters:
      t - The object to analyze
      annoDefault - The default that this should have if no scope could be found
      Returns:
      The class of the scope annotation
    • getScopeFromClass

      public static Annotation getScopeFromClass(Class<?> clazz, Annotation annoDefault)
      Gets the scope annotation from the object
      Parameters:
      clazz - The class to analyze
      annoDefault - The scope that should be returned if no scope could be found
      Returns:
      The class of the scope annotation
    • isAnnotationAQualifier

      public static boolean isAnnotationAQualifier(Annotation anno)
      Returns true if the given annotation is a qualifier
      Parameters:
      anno - The annotation to check
      Returns:
      true if this is an annotation
    • getQualifiersFromObject

      public static Set<Annotation> getQualifiersFromObject(Object t)
      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

      public static Set<String> getQualifiersFromClass(Class<?> clazz)
      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
    • internalGetQualifierAnnotations

      private static Set<Annotation> internalGetQualifierAnnotations(AnnotatedElement annotatedGuy)
    • getQualifierAnnotations

      public static Set<Annotation> getQualifierAnnotations(AnnotatedElement annotatedGuy)
      Gets all the qualifier annotations from the object

      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

      Parameters:
      annotatedGuy - The thing to analyze
      Returns:
      The set of qualifiers. Will not return null but may return an empty set
    • writeSet

      public static String writeSet(Set<?> set)
      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

      public static String writeSet(Set<?> set, Object excludeMe)
      Writes a set in a way that can be read from an input stream as well
      Parameters:
      set - The set to write
      excludeMe - An object to exclude from the list of things written
      Returns:
      a representation of a list
    • readSet

      public static void readSet(String line, Collection<String> addToMe) throws IOException
      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 read
      addToMe - The set to add the strings to
      Throws:
      IOException - On a failure
    • internalReadSet

      private static int internalReadSet(char[] asChars, int startIndex, Collection<String> addToMe) throws IOException
      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:
      asChars - The line to read
      addToMe - The set to add the strings to
      Returns:
      The number of characters read until the end of the set
      Throws:
      IOException - On a failure
    • readKeyStringListLine

      private static int readKeyStringListLine(char[] asChars, int startIndex, Map<String,List<String>> addToMe) throws IOException
      Throws:
      IOException
    • readMetadataMap

      public static void readMetadataMap(String line, Map<String,List<String>> addToMe) throws IOException
      Writes a set in a way that can be read from an input stream as well
      Parameters:
      line - The line to read
      addToMe - The set to add the strings to
      Throws:
      IOException - On a failure
    • escapeString

      private static String escapeString(String escapeMe)
    • writeList

      private static String writeList(List<String> list)
    • writeMetadata

      public static String writeMetadata(Map<String,List<String>> metadata)
      Used to write the metadata out
      Parameters:
      metadata - The metadata to externalize
      Returns:
      The metadata in an externalizable format
    • addMetadata

      public static void addMetadata(Map<String,List<String>> metadatas, String key, String value)
      Adds a value to the list of values associated with this key
      Parameters:
      metadatas - The base metadata object
      key - The key to which to add the value. May not be null
      value - The value to add. May not be null
    • removeMetadata

      public static boolean removeMetadata(Map<String,List<String>> metadatas, String key, String value)
      Removes the given value from the given key
      Parameters:
      metadatas - The base metadata object
      key - The key of the value to remove. May not be null
      value - The value to remove. May not be null
      Returns:
      true if the value was removed
    • removeAllMetadata

      public static boolean removeAllMetadata(Map<String,List<String>> metadatas, String key)
      Removes all the metadata values associated with key
      Parameters:
      metadatas - The base metadata object
      key - The key of the metadata values to remove
      Returns:
      true if any value was removed
    • deepCopyMetadata

      public static Map<String,List<String>> deepCopyMetadata(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)
      Parameters:
      copyMe - The guy to copy (if null, null will be returned)
      Returns:
      A deep copy of the metadata
    • setField

      public static void setField(Field field, Object instance, Object value) throws Throwable
      Sets the given field to the given value
      Parameters:
      field - The non-null field to set
      instance - The non-null instance to set into
      value - The value to which the field should be set
      Throws:
      Throwable - If there was some exception while setting the field
    • invoke

      public static Object invoke(Object o, Method m, Object[] args, boolean neutralCCL) throws Throwable
      This version of invoke is CCL neutral (it will return with the same CCL as what it went in with)
      Parameters:
      o - the object on which to invoke it
      m - the method to invoke
      args - 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

      public static boolean isStatic(Member member)
      Returns true if the underlying member is static
      Parameters:
      member - The non-null member to test
      Returns:
      true if the member is static
    • setContextClassLoader

      private static void setContextClassLoader(Thread t, ClassLoader l)
      Sets the context classloader under the privileged of this class
      Parameters:
      t - The thread on which to set the classloader
      l - The classloader to set
    • setAccessible

      private static void setAccessible(AccessibleObject ao)
      Sets this accessible object to be accessible using the permissions of the hk2-locator bundle (which will need the required grant)
      Parameters:
      ao - The object to change
    • makeMe

      public static Object makeMe(Constructor<?> c, Object[] args, boolean neutralCCL) throws Throwable
      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 call
      args - 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 field
      metadata - 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 invalid input: '&'46;Named qualifier in this set of qualifiers
      Parameters:
      qualifiers - The set of qualifiers that may or may not have Named in it
      parent - 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
    • getCurrentContextClassLoader

      private static ClassLoader getCurrentContextClassLoader()
      Gets the current context class loader with privs
      Returns:
      The current context class loader
    • 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 annotations
      requiredAnnotations - The required annotations
      Returns:
      true if the candidate set contains the entire required set
    • translatePrimitiveType

      public static Class<?> translatePrimitiveType(Class<?> type)
      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

      public static boolean isPrivate(Member member)
      Returns true if the underlying member is private
      Parameters:
      member - The non-null member to test
      Returns:
      true if the member is private
    • getAllTypes

      public static Set<Type> getAllTypes(Type t)
    • fillInPT

      private static ParameterizedType fillInPT(ParameterizedType pt, HashMap<Class<?>,ParameterizedType> class2TypeMap)
    • isFilledIn

      private static boolean isFilledIn(ParameterizedType pt, HashSet<ParameterizedType> recursionKiller)
    • isFilledIn

      private static boolean isFilledIn(ParameterizedType pt)
    • addAllInterfaceContracts

      private static void addAllInterfaceContracts(Type interfaceType, LinkedHashSet<Type> addToMe)
    • createMethodWrapper

      public static MethodWrapper createMethodWrapper(Method wrapMe)
      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

      public static <T> T cast(Object o)
      Casts this thing to the given type
      Parameters:
      o - The thing to cast
      Returns:
      A casted version of o