org.jvnet.hk2.internal
Class Utilities

java.lang.Object
  extended by org.jvnet.hk2.internal.Utilities

public class Utilities
extends java.lang.Object

This class contains a set of static utilities useful for implementing HK2

Author:
jwells

Constructor Summary
Utilities()
           
 
Method Summary
static
<T> T
cast(java.lang.Object o)
          Casts this thing to the given type
static void checkFactoryType(java.lang.Class<?> factoryClass, Collector collector)
          Checks to be sure the Factory class is ok
static void checkLookupType(java.lang.Class<?> checkMe)
          Checks that the incoming lookup type is not improper in some way
static
<T> ActiveDescriptor<T>
createAutoDescriptor(java.lang.Class<T> clazz, ServiceLocatorImpl locator)
          Creates a reified automatically generated descriptor
static
<T> T
createService(ActiveDescriptor<T> root, Injectee injectee, ServiceLocatorImpl locator, ServiceHandle<T> handle, java.lang.Class<?> requestedClass)
           
static java.util.Set<java.lang.reflect.Field> findInitializerFields(java.lang.Class<?> annotatedType, ServiceLocatorImpl locator, Collector errorCollector)
          Will find all the initialize fields in the class
static java.util.Set<java.lang.reflect.Method> findInitializerMethods(java.lang.Class<?> annotatedType, ServiceLocatorImpl locator, Collector errorCollector)
          Get all the initializer methods of the annotatedType.
static java.lang.reflect.Method findPostConstruct(java.lang.Class<?> clazz, Collector collector)
          Finds the post construct method on this class
static java.lang.reflect.Method findPreDestroy(java.lang.Class<?> clazz, Collector collector)
          Finds the pre destroy method on this class
static java.lang.reflect.Constructor<?> findProducerConstructor(java.lang.Class<?> annotatedType, ServiceLocatorImpl locator, Collector collector)
          Validates the constructors of the annotated type and returns the producer for the annotatedType (if there is no valid producer constructor then this method returns null)
static java.util.Set<java.lang.annotation.Annotation> fixAndCheckQualifiers(java.lang.annotation.Annotation[] qualifiers, java.lang.String name)
          This method returns a set of qualifiers from an array of qualifiers.
static java.util.Set<java.lang.annotation.Annotation> getAllQualifiers(java.lang.reflect.AnnotatedElement annotatedGuy, java.lang.String name, Collector collector)
          Returns the full set of qualifier annotations on this class
static java.util.List<Injectee> getConstructorInjectees(java.lang.reflect.Constructor<?> c)
          Returns all the injectees for a constructor
static java.lang.String getDefaultNameFromMethod(java.lang.reflect.Method parent, Collector collector)
          Returns the default name if one can be found.
static java.lang.Class<?> getFactoryAwareImplementationClass(ActiveDescriptor<?> descriptor)
          This utility will return the proper implementation class, taking into account that the descriptor may be a factory
static java.lang.reflect.Method getFactoryProvideMethod(java.lang.Class<?> clazz)
          This method will retrieve the provide method from a Factory
static java.util.List<Injectee> getFieldInjectees(java.lang.reflect.Field f)
          Returns the injectees for a field
static
<T> T
getFirstThingInList(java.util.List<T> set)
          Returns the first thing found in the set
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 InjectionResolver<?> getInjectionResolver(ServiceLocatorImpl locator, java.lang.reflect.AnnotatedElement annotatedGuy)
          Returns an injection resolver for this AnnotatedElement.
static InjectionResolver<?> getInjectionResolver(ServiceLocatorImpl locator, Injectee injectee)
          Returns an injection resolver for the injectee
static java.lang.Class<? extends java.lang.annotation.Annotation> getInjectionResolverType(ActiveDescriptor<?> desc)
          Will return the class of the injection resolver annotation type, or null if no injection resolver annotation can be found
static java.lang.Class<?>[] getInterfacesForProxy(java.util.Set<java.lang.reflect.Type> contracts)
          Returns all the interfaces the proxy must implement
static ActiveDescriptor<ServiceLocator> getLocatorDescriptor(ServiceLocator locator)
          Returns a constant ActiveDescriptor for the basic ServiceLocator
static java.util.List<Injectee> getMethodInjectees(java.lang.reflect.Method c)
          Returns all the injectees for a constructor
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
static java.lang.Class<? extends java.lang.annotation.Annotation> getScopeAnnotationType(java.lang.reflect.AnnotatedElement annotatedGuy, Descriptor defaultScope, Collector collector)
          Returns the scope of this thing
static java.lang.Class<? extends java.lang.annotation.Annotation> getScopeAnnotationType(java.lang.Class<?> fromThis, Descriptor defaultScope)
          Returns the scope of this thing
static ActiveDescriptor<InjectionResolver<Inject>> getThreeThirtyDescriptor(ServiceLocatorImpl locator)
          Creates a Three Thirty constant active descriptor
static void handleErrors(NarrowResults results, java.util.LinkedList<ErrorService> callThese)
          Calls the list of error services for the list of errors
static boolean isAbstract(java.lang.reflect.Member member)
          Returns true if the underlying member is abstract
static boolean isFinal(java.lang.reflect.Member member)
          Returns true if the underlying member is abstract
static boolean isPrivate(java.lang.reflect.Member member)
          Returns true if the underlying member is private
static boolean isProxiable(ActiveDescriptor<?> desc)
          This method determines whether or not the descriptor should be proxied.
static boolean isProxiableScope(java.lang.Class<? extends java.lang.annotation.Annotation> scope)
          Returns true if this scope is proxiable
static boolean isUnproxiableScope(java.lang.Class<? extends java.lang.annotation.Annotation> scope)
          Returns true if this scope is unproxiable
static
<T> T
justCreate(java.lang.Class<T> createMe, ServiceLocatorImpl locator)
          Just creates the thing, doesn't try to do anything else
static void justInject(java.lang.Object injectMe, ServiceLocatorImpl locator)
          Just creates the thing, doesn't try to do anything else
static void justPostConstruct(java.lang.Object postMe)
          Post constructs the given object
static void justPreDestroy(java.lang.Object preMe)
          Pre Destroys the given object
static java.lang.Class<?> loadClass(java.lang.String loadMe, Descriptor fromMe, Collector collector)
          Loads the class using the loader from the given descriptor or the classloader of the utilities class otherwise
static java.lang.Class<?> loadClass(java.lang.String implementation, Injectee injectee)
          Load the given class for the given injectee.
static java.lang.Object makeMe(java.lang.reflect.Constructor<?> c, java.lang.Object[] args)
          This version of invoke is CCL neutral (it will return with the same CCL as what it went in with)
static java.lang.Class<?> translatePrimitiveType(java.lang.Class<?> type)
          Converts the type to its java form, or returns the original
static void validateSelfInjectees(ActiveDescriptor<?> givenDescriptor, java.util.List<Injectee> injectees, Collector collector)
          This method validates a list of injectees to ensure that any self injectees have the proper set of requirements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utilities

public Utilities()
Method Detail

getFactoryAwareImplementationClass

public static java.lang.Class<?> getFactoryAwareImplementationClass(ActiveDescriptor<?> descriptor)
This utility will return the proper implementation class, taking into account that the descriptor may be a factory

Parameters:
descriptor - The descriptor (reified and not null) that will be used to find the implementation
Returns:
The real implementation class

checkLookupType

public static void checkLookupType(java.lang.Class<?> checkMe)
Checks that the incoming lookup type is not improper in some way

Parameters:
checkMe - class to check

translatePrimitiveType

public static java.lang.Class<?> translatePrimitiveType(java.lang.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

handleErrors

public static void handleErrors(NarrowResults results,
                                java.util.LinkedList<ErrorService> callThese)
Calls the list of error services for the list of errors

Parameters:
results - the results
callThese - the services to call

loadClass

public static java.lang.Class<?> loadClass(java.lang.String loadMe,
                                           Descriptor fromMe,
                                           Collector collector)
Loads the class using the loader from the given descriptor or the classloader of the utilities class otherwise

Parameters:
loadMe - The fully qualified class name
fromMe - The descriptor to use for the loader
collector - The error collector to fill in if this returns null
Returns:
null on failure to load (the failure will be added to the collector)

loadClass

public static java.lang.Class<?> loadClass(java.lang.String implementation,
                                           Injectee injectee)
Load the given class for the given injectee.

Parameters:
implementation - the impl class name string
injectee - the injectee
Returns:
The class represented by this implementation and injectee

getInjectionResolverType

public static java.lang.Class<? extends java.lang.annotation.Annotation> getInjectionResolverType(ActiveDescriptor<?> desc)
Will return the class of the injection resolver annotation type, or null if no injection resolver annotation can be found

Parameters:
desc - The reified descriptor to find the injection resolution on
Returns:
The annotation type for this injection resolver

checkFactoryType

public static void checkFactoryType(java.lang.Class<?> factoryClass,
                                    Collector collector)
Checks to be sure the Factory class is ok

Parameters:
factoryClass - the class to check
collector - the exception collector

createAutoDescriptor

public static <T> ActiveDescriptor<T> createAutoDescriptor(java.lang.Class<T> clazz,
                                                           ServiceLocatorImpl locator)
                                                throws MultiException,
                                                       java.lang.IllegalArgumentException
Creates a reified automatically generated descriptor

Parameters:
clazz - The class to create the desciptor for
locator - The service locator for whom we are creating this
Returns:
A reified active descriptor
Throws:
MultiException - if there was an error in the class
java.lang.IllegalArgumentException - If the class is null

justPreDestroy

public static void justPreDestroy(java.lang.Object preMe)
Pre Destroys the given object

Parameters:
preMe - pre destroys the thing

justPostConstruct

public static void justPostConstruct(java.lang.Object postMe)
Post constructs the given object

Parameters:
postMe - post constructs the thing

justInject

public static void justInject(java.lang.Object injectMe,
                              ServiceLocatorImpl locator)
Just creates the thing, doesn't try to do anything else

Parameters:
injectMe - The object to inject into
locator - The locator to find the injection points with

justCreate

public static <T> T justCreate(java.lang.Class<T> createMe,
                               ServiceLocatorImpl locator)
Just creates the thing, doesn't try to do anything else

Parameters:
createMe - The thing to create
locator - The locator to find the injection points with
Returns:
The constructed thing, no further injection is performed

getInterfacesForProxy

public static java.lang.Class<?>[] getInterfacesForProxy(java.util.Set<java.lang.reflect.Type> contracts)
Returns all the interfaces the proxy must implement

Parameters:
contracts - All of the advertised contracts
Returns:
The array of contracts to add to the proxy

isProxiableScope

public static boolean isProxiableScope(java.lang.Class<? extends java.lang.annotation.Annotation> scope)
Returns true if this scope is proxiable

Parameters:
scope - The scope annotation to test
Returns:
true if this must be proxied

isUnproxiableScope

public static boolean isUnproxiableScope(java.lang.Class<? extends java.lang.annotation.Annotation> scope)
Returns true if this scope is unproxiable

Parameters:
scope - The scope annotation to test
Returns:
true if this must be proxied

isProxiable

public static boolean isProxiable(ActiveDescriptor<?> desc)
This method determines whether or not the descriptor should be proxied. The given descriptor must be reified and valid.

Parameters:
desc - A non-null, reified ActiveDescriptor
Returns:
true if this descriptor must be proxied, false otherwise

getFirstThingInList

public static <T> T getFirstThingInList(java.util.List<T> set)
Returns the first thing found in the set

Parameters:
set - The set from which to get the first element
Returns:
the first thing found in the set

getLocatorDescriptor

public static ActiveDescriptor<ServiceLocator> getLocatorDescriptor(ServiceLocator locator)
Returns a constant ActiveDescriptor for the basic ServiceLocator

Parameters:
locator - The service locator to get the ActiveDescriptor for
Returns:
An active descriptor specifically for the ServiceLocator

getThreeThirtyDescriptor

public static ActiveDescriptor<InjectionResolver<Inject>> getThreeThirtyDescriptor(ServiceLocatorImpl locator)
Creates a Three Thirty constant active descriptor

Parameters:
locator - The service locator to get the ActiveDescriptor for
Returns:
An active descriptor specifically for the ServiceLocator

findProducerConstructor

public static java.lang.reflect.Constructor<?> findProducerConstructor(java.lang.Class<?> annotatedType,
                                                                       ServiceLocatorImpl locator,
                                                                       Collector collector)
Validates the constructors of the annotated type and returns the producer for the annotatedType (if there is no valid producer constructor then this method returns null)

Parameters:
annotatedType - The type to find the producer constructor
locator - The service locator to use when analyzing constructors
collector - The error collector
Returns:
The producer constructor or null if the type has no valid producer constructor

getFirstTypeArgument

public 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

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

findInitializerMethods

public static java.util.Set<java.lang.reflect.Method> findInitializerMethods(java.lang.Class<?> annotatedType,
                                                                             ServiceLocatorImpl locator,
                                                                             Collector errorCollector)
Get all the initializer methods of the annotatedType. If there are definitional errors they will be put into the errorCollector (so as to get all the errors at one shot)

Parameters:
annotatedType - The type to find the errors in
locator - The locator to use when analyzing methods
errorCollector - The collector to add errors to
Returns:
A possibly empty but never null set of initializer methods

findInitializerFields

public static java.util.Set<java.lang.reflect.Field> findInitializerFields(java.lang.Class<?> annotatedType,
                                                                           ServiceLocatorImpl locator,
                                                                           Collector errorCollector)
Will find all the initialize fields in the class

Parameters:
annotatedType - The class to search for fields
locator - The locator to use when analyzing the class
errorCollector - The error collector
Returns:
A non-null but possibly empty set of initializer fields

isPrivate

public static boolean isPrivate(java.lang.reflect.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

isAbstract

public static boolean isAbstract(java.lang.reflect.Member member)
Returns true if the underlying member is abstract

Parameters:
member - The non-null member to test
Returns:
true if the member is abstract

isFinal

public static boolean isFinal(java.lang.reflect.Member member)
Returns true if the underlying member is abstract

Parameters:
member - The non-null member to test
Returns:
true if the member is abstract

getScopeAnnotationType

public static java.lang.Class<? extends java.lang.annotation.Annotation> getScopeAnnotationType(java.lang.Class<?> fromThis,
                                                                                                Descriptor defaultScope)
Returns the scope of this thing

Parameters:
fromThis - The annotated class or producer method
Returns:
The scope of this class or producer method. If no scope is found will return the dependent scope

getScopeAnnotationType

public static java.lang.Class<? extends java.lang.annotation.Annotation> getScopeAnnotationType(java.lang.reflect.AnnotatedElement annotatedGuy,
                                                                                                Descriptor defaultScope,
                                                                                                Collector collector)
Returns the scope of this thing

Parameters:
annotatedGuy - The annotated class or producer method
collector - The error collector
Returns:
The scope of this class or producer method. If no scope is found will return the dependent scope

getInjectionResolver

public static InjectionResolver<?> getInjectionResolver(ServiceLocatorImpl locator,
                                                        Injectee injectee)
                                                 throws java.lang.IllegalStateException
Returns an injection resolver for the injectee

Parameters:
locator - The locator to use when finding the resolver
injectee - Injectee from which the annotation should be extracted
Returns:
Injection resolver used to resolve the injection for the injectee
Throws:
java.lang.IllegalStateException - If we could not find a valid resolver

getInjectionResolver

public static InjectionResolver<?> getInjectionResolver(ServiceLocatorImpl locator,
                                                        java.lang.reflect.AnnotatedElement annotatedGuy)
                                                 throws java.lang.IllegalStateException
Returns an injection resolver for this AnnotatedElement. The method cannot be used for constructors or methods.

Parameters:
locator - The locator to use when finding the resolver
annotatedGuy - The annotated class or producer method
Returns:
The scope of this class or producer method. If no scope is found will return the dependent scope
Throws:
java.lang.IllegalStateException - If we could not find a valid resolver

getFactoryProvideMethod

public static java.lang.reflect.Method getFactoryProvideMethod(java.lang.Class<?> clazz)
This method will retrieve the provide method from a Factory

Parameters:
clazz - This class must implement factory
Returns:
The provide method from this class

getNameFromAllQualifiers

public 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

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

getDefaultNameFromMethod

public static java.lang.String getDefaultNameFromMethod(java.lang.reflect.Method parent,
                                                        Collector collector)
Returns the default name if one can be found. Will only work on classes and methods

Parameters:
parent - The parent annotated element
collector - For errors
Returns:
null if there is no default name (no Named)

getAllQualifiers

public static java.util.Set<java.lang.annotation.Annotation> getAllQualifiers(java.lang.reflect.AnnotatedElement annotatedGuy,
                                                                              java.lang.String name,
                                                                              Collector collector)
Returns the full set of qualifier annotations on this class

Parameters:
annotatedGuy - The element we are searching for qualifiers
name - The name this element must have
collector - The error collector
Returns:
A non-null but possibly empty set of qualifiers

getConstructorInjectees

public static java.util.List<Injectee> getConstructorInjectees(java.lang.reflect.Constructor<?> c)
Returns all the injectees for a constructor

Parameters:
c - The constructor to analyze
Returns:
the list (in order) of parameters to the constructor

getMethodInjectees

public static java.util.List<Injectee> getMethodInjectees(java.lang.reflect.Method c)
Returns all the injectees for a constructor

Parameters:
c - The constructor to analyze
Returns:
the list (in order) of parameters to the constructor

getFieldInjectees

public static java.util.List<Injectee> getFieldInjectees(java.lang.reflect.Field f)
Returns the injectees for a field

Parameters:
f - The field to analyze
Returns:
the list (in order) of parameters to the constructor

validateSelfInjectees

public static void validateSelfInjectees(ActiveDescriptor<?> givenDescriptor,
                                         java.util.List<Injectee> injectees,
                                         Collector collector)
This method validates a list of injectees to ensure that any self injectees have the proper set of requirements. It adds IllegalArgumentExceptions to the collector if it finds errors

Parameters:
givenDescriptor - The descriptor associated with this injectee, or null if there are none
injectees - The list of injectees to check. Only self injectees are validates
collector - The collector to add any errors to

findPostConstruct

public static java.lang.reflect.Method findPostConstruct(java.lang.Class<?> clazz,
                                                         Collector collector)
Finds the post construct method on this class

Parameters:
clazz - The class to search for the post construct
collector - An error collector
Returns:
The post construct method or null

findPreDestroy

public static java.lang.reflect.Method findPreDestroy(java.lang.Class<?> clazz,
                                                      Collector collector)
Finds the pre destroy method on this class

Parameters:
clazz - The class to search for the pre destroy method
collector - An error collector
Returns:
The pre destroy method or null

makeMe

public static java.lang.Object makeMe(java.lang.reflect.Constructor<?> c,
                                      java.lang.Object[] args)
                               throws java.lang.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)
Returns:
The return from the invocation
Throws:
java.lang.Throwable - The unwrapped throwable thrown by the method

fixAndCheckQualifiers

public static java.util.Set<java.lang.annotation.Annotation> fixAndCheckQualifiers(java.lang.annotation.Annotation[] qualifiers,
                                                                                   java.lang.String name)
This method returns a set of qualifiers from an array of qualifiers. TODO It can also do some sanity checking here (i.e., multiple qualifiers of the same type, that sort of thing)

Parameters:
qualifiers - The qualifiers to convert. May not be null, but may be zero length
name - The name this set of qualifiers must have
Returns:
The set containing all the qualifiers

cast

public static <T> T cast(java.lang.Object o)
Casts this thing to the given type

Parameters:
o - The thing to cast
Returns:
A casted version of o

createService

public static <T> T createService(ActiveDescriptor<T> root,
                                  Injectee injectee,
                                  ServiceLocatorImpl locator,
                                  ServiceHandle<T> handle,
                                  java.lang.Class<?> requestedClass)


Copyright © 2012 Oracle Corporation. All Rights Reserved.