org.spicefactory.cinnamon.reflect
Class ReflectionFactory

java.lang.Object
  extended by org.spicefactory.cinnamon.reflect.ReflectionFactory

public class ReflectionFactory
extends Object

The ReflectionFactory is the central factory for the Spicelib reflection package. It creates and caches instances of Bean, MethodInvoker and TypeContext in a thread-safe way. It is also a central repository for Converter instances which will be used within all types of objects created by this factory. It uses builtin Converters for Numbers, Arrays and Collections. You can register additional Converter types if required.

Author:
Jens Halm

Constructor Summary
ReflectionFactory()
          Creates a new ReflectionFactory.
 
Method Summary
 void addConverter(Converter<?> converter)
          Adds the given Converter instance to the repository.
 Bean getBean(Class<?> type)
          Returns the Bean instance representing the given Class.
 Converter<?> getConverter(Class<?> type)
          Returns the Converter instance that is applicable to the given target type.
 Set<Converter<?>> getConverters()
          Returns all Converter instances registered with this factory including the builtin ones.
 MethodInvoker getMethodInvoker(Method method, Class<?> contextClass)
          Returns a MethodInvoker instance for the given Method and context Class.
 TypeContext getTypeContext(Type contextType, Class<?> baseType)
          Returns a TypeContext for the given context and base types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionFactory

public ReflectionFactory()
Creates a new ReflectionFactory. Caches for Bean, MethodInvoker and TypeContext instances will be empty initially. Converters for Numbers, Enums, Arrays and Collections will be created and added to the factory.

Method Detail

addConverter

public void addConverter(Converter<?> converter)
Adds the given Converter instance to the repository. The implementation of the Converter interface must specify a class or a bounded TypeVariable for the type parameter of the interface. This means that MyConverter implements Converter<File> or MyConverter<T extends File> implements Converter<T> is permitted while MyConverter<T> implements Converter<T> is not since the target type this Converter can be applied to cannot be determined with an unbounded TypeVariable.

Parameters:
converter - the Converter instance to be added to the repository

getConverter

public Converter<?> getConverter(Class<?> type)
Returns the Converter instance that is applicable to the given target type. This method will also consider subclasses, that means a Converter that was registered for Number types will be returned if you invoke getConverter(Integer.class)

Parameters:
type - the class that the returned Converter instance should be applicable to
Returns:
the Converter instance applicable for the given taget type or null if no suitable Converter exists

getConverters

public Set<Converter<?>> getConverters()
Returns all Converter instances registered with this factory including the builtin ones. Modifications of the returned Set instance have no effect on the factory.

Returns:
a Set containing all registered Converter instances.

getBean

public Bean getBean(Class<?> type)
Returns the Bean instance representing the given Class. The Bean instance might be returned from the internal cache of this factory if the same Class has been requested before.

Parameters:
type - the class a Bean representation should be created for
Returns:
the Bean instance representing the given class

getMethodInvoker

public MethodInvoker getMethodInvoker(Method method,
                                      Class<?> contextClass)
Returns a MethodInvoker instance for the given Method and context Class. The context Class might be a subclass or subinterface of the declaring Class of the given Method and serves as a "perspective" from which TypeVariables of the declaring Class can be resolved.

Parameters:
method - the Method an invoker should be created for
contextClass - the context Class for the MethodInvoker
Returns:
the MethodInvoker instance for the given Method and context

getTypeContext

public TypeContext getTypeContext(Type contextType,
                                  Class<?> baseType)
Returns a TypeContext for the given context and base types. The TypeContext instance might be returned from the internal cache of this factory if the same combination of context and base type has been requested before. The raw type of the given context type must be a subclass or subinterface of the given base type and serves as a "perspective" from which TypeVariables in the base type might be resolved.

Parameters:
contextType - the context type
baseType - the base type
Returns:
a TypeContext instance for the given context and base types