net.sf.morph.reflect.reflectors
Class BaseReflector

java.lang.Object
  extended by net.sf.morph.reflect.reflectors.BaseReflector
All Implemented Interfaces:
net.sf.composite.Component, DecoratedReflector, Reflector
Direct Known Subclasses:
BaseBeanReflector, BaseCompositeReflector, BaseContainerReflector, MapReflector, ReflectorDecorator, SimpleInstantiatingReflector

public abstract class BaseReflector
extends java.lang.Object
implements Reflector, DecoratedReflector

Convenient base class for Reflectors. Validates arguments and takes care of logging and exception handling. Also, automatically implements some methods for certain types of reflectors. Most notably, the BeanReflector interface is automatically implemented for reflectors that implement MutableIndexedContainerReflector. See method JavaDoc for more information.

Since:
Nov 14, 2004
Author:
Matt Sgarlata

Field Summary
protected  org.apache.commons.logging.Log log
          Protected Log instance
 
Constructor Summary
BaseReflector()
          Create a new BaseReflector.
 
Method Summary
 boolean add(java.lang.Object container, java.lang.Object value)
          GrowableContainerReflector.add(Object, Object)
protected  boolean addImpl(java.lang.Object container, java.lang.Object value)
          Implementation of GrowableContainer.add(Object).
protected  void checkIndex(java.lang.Object container, int index)
          Validate index into container.
protected  void checkIsReflectable(java.lang.Object object)
          Throws an exception if a the given object is not reflectable by this reflector.
protected  WrapperInvocationHandler createWrapperInvocationHandler(java.lang.Object object)
          Create a WrapperInvocationHandler for the specified Object.
 java.lang.Object get(java.lang.Object container, int index)
          IndexedContainerReflector.get(Object, int)
 java.lang.Object get(java.lang.Object bean, java.lang.String propertyName)
          BeanReflector.get(Object, String)
 java.lang.Class getContainedType(java.lang.Class clazz)
          ContainerReflector.getContainedType(Class)
protected  java.lang.Class getContainedTypeImpl(java.lang.Class clazz)
          Implementation of ContainerReflector.getContainedType(Class).
protected  java.lang.Object getImpl(java.lang.Object container, int index)
          Implementation of IndexedContainerReflector.get(Object, int).
protected  java.lang.Object getImpl(java.lang.Object bean, java.lang.String propertyName)
          Implementation of BeanReflector.get(Object, String).
 java.util.Iterator getIterator(java.lang.Object container)
          ContainerReflector.getIterator(Object)
protected  java.util.Iterator getIteratorImpl(java.lang.Object container)
          Implementation of ContainerReflector.getIterator(Object).
 java.lang.String[] getPropertyNames(java.lang.Object bean)
          BeanReflector.getPropertyNames(Object)
protected  java.lang.String[] getPropertyNamesImpl(java.lang.Object bean)
          Implementation of BeanReflector.getPropertyNames(Object).
protected  java.util.Map getReflectableCallCache()
          Get the isReflectable(Class) call cache.
 java.lang.Class[] getReflectableClasses()
          Defines which classes are reflectable by this reflector.
protected abstract  java.lang.Class[] getReflectableClassesImpl()
          Implementation of Reflector.getReflectableClasses().
 java.lang.String getReflectorName()
          Get the reflectorName.
 int getSize(java.lang.Object container)
          SizableReflector.getSize(Object)
protected  int getSizeImpl(java.lang.Object container)
          Implementation of SizableReflector.getSize(Object).
 java.lang.Class getType(java.lang.Object bean, java.lang.String propertyName)
          BeanReflector.getType(Object, String)
protected  java.lang.Class getTypeImpl(java.lang.Object bean, java.lang.String propertyName)
          Implementation of BeanReflector.getType(Object, String).
 Wrapper getWrapper(java.lang.Object object)
          Optional operation: returns a wrapper for the given object.
protected  Wrapper getWrapperImpl(java.lang.Object object)
          Implementation of Reflector.getWrapper(Object).
protected  void initialize()
          Initialize this Reflector.
protected  void initializeImpl()
          Implementation of initialize().
 boolean isCachingIsReflectableCalls()
          Learn whether this Reflector is caching isReflectable(Class) calls.
protected  boolean isInitialized()
          Learn whether this Reflector is initialized.
protected  boolean isPerformingLogging()
          Indicates whether this reflector is writing log messages
 boolean isReadable(java.lang.Object bean, java.lang.String propertyName)
          BeanReflector.isReadable(Object, String)
protected  boolean isReadableImpl(java.lang.Object bean, java.lang.String propertyName)
          Implementation of BeanReflector.isReadable(Object, String).
 boolean isReflectable(java.lang.Class reflectedType)
          Indicates whether the given type is reflectable by this reflector.
 boolean isReflectable(java.lang.Class reflectedType, java.lang.Class reflectorType)
          DecoratedReflector.isReflectable(Class)
protected  boolean isReflectableImpl(java.lang.Class reflectedType)
          Implementation of DecoratedReflector.isReflectable(Class).
protected  boolean isReflectableImpl(java.lang.Class reflectedType, java.lang.Class reflectorType)
          Implementation of isReflectable(Class, Class).
 boolean isStrictlyTyped()
          Indicates whether this reflector is strictly typed.
protected  boolean isValidIndex(java.lang.Object bean, java.lang.String propertyName)
          Learn whether propertyName denotes a valid numeric property index for bean.
protected  boolean isWrappingRuntimeExceptions()
          Indicates whether runtime exceptions should be wrapped as ReflectionExceptions.
 boolean isWriteable(java.lang.Object bean, java.lang.String propertyName)
          BeanReflector.isWriteable(Object, String)
protected  boolean isWriteableImpl(java.lang.Object bean, java.lang.String propertyName)
          Implementation of BeanReflector.isWriteable(Object, String).
 java.lang.Object newInstance(java.lang.Class clazz, java.lang.Object parameters)
          InstantiatingReflector.newInstance(Class, Object)
protected  java.lang.Object newInstanceImpl(java.lang.Class clazz)
          Deprecated. Use newInstanceImpl(Class, Object) instead. Calls to this method will fail with an UnsupportedOperationException
protected  java.lang.Object newInstanceImpl(java.lang.Class clazz, java.lang.Object parameters)
          Implementation of InstantiatingReflector.newInstance(Class, Object).
 java.lang.Object set(java.lang.Object container, int index, java.lang.Object propertyValue)
          MutableIndexedContainerReflector.set(Object, int, Object)
 void set(java.lang.Object bean, java.lang.String propertyName, java.lang.Object propertyValue)
          BeanReflector.set(Object, String, Object)
 void setCachingIsReflectableCalls(boolean cachingIsReflectableCalls)
          Set whether this Reflector is caching isReflectable(Class) calls.
protected  java.lang.Object setImpl(java.lang.Object container, int index, java.lang.Object propertyValue)
          Implementation of MutableIndexedContainerReflector.set(Object, int, Object).
protected  void setImpl(java.lang.Object bean, java.lang.String propertyName, java.lang.Object value)
          Implementation of BeanReflector.set(Object, String, Object).
protected  void setInitialized(boolean initialized)
          Set the initialization status of this Reflector.
protected  void setReflectableCallCache(java.util.Map reflectableCallCache)
          Set the isReflectable(Class) call cache.
 void setReflectorName(java.lang.String reflectorName)
          Set the reflectorName.
 java.lang.String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected transient org.apache.commons.logging.Log log
Protected Log instance

Constructor Detail

BaseReflector

public BaseReflector()
Create a new BaseReflector.

Method Detail

initializeImpl

protected void initializeImpl()
                       throws java.lang.Exception
Implementation of initialize().

Throws:
java.lang.Exception

initialize

protected final void initialize()
                         throws ReflectionException
Initialize this Reflector.

Throws:
ReflectionException

getReflectableClasses

public final java.lang.Class[] getReflectableClasses()
Defines which classes are reflectable by this reflector. Each returned class and all its subclasses are reflectable by this reflector.

Specified by:
getReflectableClasses in interface Reflector
Returns:
the classes that are reflectable by this reflector.
See Also:
Reflector.getReflectableClasses()

getReflectableClassesImpl

protected abstract java.lang.Class[] getReflectableClassesImpl()
                                                        throws java.lang.Exception
Implementation of Reflector.getReflectableClasses().

Throws:
java.lang.Exception

getWrapper

public final Wrapper getWrapper(java.lang.Object object)
Optional operation: returns a wrapper for the given object.

Specified by:
getWrapper in interface Reflector
Parameters:
object - the object for which a wrapper is desired
Returns:
the wrapper
See Also:
Reflector.getWrapper(java.lang.Object)

getWrapperImpl

protected Wrapper getWrapperImpl(java.lang.Object object)
                          throws java.lang.Exception
Implementation of Reflector.getWrapper(Object).

Throws:
java.lang.Exception

createWrapperInvocationHandler

protected WrapperInvocationHandler createWrapperInvocationHandler(java.lang.Object object)
Create a WrapperInvocationHandler for the specified Object.

Parameters:
object -
Returns:
WrapperInvocationHandler

isReflectable

public final boolean isReflectable(java.lang.Class reflectedType)
                            throws ReflectionException
Indicates whether the given type is reflectable by this reflector.

Specified by:
isReflectable in interface DecoratedReflector
Parameters:
reflectedType - the type to test
Returns:
true, if this reflector can reflect the given class or
false, if this reflector cannot reflect the given class
Throws:
ReflectionException - if an error occurrs
See Also:
DecoratedReflector.isReflectable(java.lang.Class)

isReflectableImpl

protected boolean isReflectableImpl(java.lang.Class reflectedType)
                             throws java.lang.Exception
Implementation of DecoratedReflector.isReflectable(Class).

Throws:
java.lang.Exception

isReflectable

public final boolean isReflectable(java.lang.Class reflectedType,
                                   java.lang.Class reflectorType)
                            throws ReflectionException
DecoratedReflector.isReflectable(Class)

Parameters:
reflectedType -
reflectorType -
Returns:
Throws:
ReflectionException

isReflectableImpl

protected boolean isReflectableImpl(java.lang.Class reflectedType,
                                    java.lang.Class reflectorType)
                             throws java.lang.Exception
Implementation of isReflectable(Class, Class).

Throws:
java.lang.Exception

checkIsReflectable

protected void checkIsReflectable(java.lang.Object object)
                           throws ReflectionException
Throws an exception if a the given object is not reflectable by this reflector. Called before executing each method in this class to ensure the reflector is being used properly.

Parameters:
object - the object to test
Throws:
ReflectionException - if the given object is not reflectable by this reflector

newInstance

public final java.lang.Object newInstance(java.lang.Class clazz,
                                          java.lang.Object parameters)
InstantiatingReflector.newInstance(Class, Object)

Parameters:
clazz -
parameters -

newInstanceImpl

protected final java.lang.Object newInstanceImpl(java.lang.Class clazz)
                                          throws java.lang.Exception
Deprecated. Use newInstanceImpl(Class, Object) instead. Calls to this method will fail with an UnsupportedOperationException

This method will be removed in a subsequent release of Morph. Left in-place to flag subclasses that require modification.

Throws:
java.lang.Exception

newInstanceImpl

protected java.lang.Object newInstanceImpl(java.lang.Class clazz,
                                           java.lang.Object parameters)
                                    throws java.lang.Exception
Implementation of InstantiatingReflector.newInstance(Class, Object). Default implementation returns a new instance of the given class by calling Class#newInstance()).

Throws:
java.lang.Exception

getPropertyNames

public final java.lang.String[] getPropertyNames(java.lang.Object bean)
                                          throws ReflectionException
BeanReflector.getPropertyNames(Object)

Parameters:
bean -
Returns:
Throws:
ReflectionException

getPropertyNamesImpl

protected java.lang.String[] getPropertyNamesImpl(java.lang.Object bean)
                                           throws java.lang.Exception
Implementation of BeanReflector.getPropertyNames(Object). Implementation automatically provided for IndexedContainerReflectors. For other reflectors, throws an UnsupportedOperationException.

Throws:
java.lang.Exception

isReadable

public final boolean isReadable(java.lang.Object bean,
                                java.lang.String propertyName)
                         throws ReflectionException
BeanReflector.isReadable(Object, String)

Parameters:
bean -
propertyName -
Returns:
boolean
Throws:
ReflectionException

isReadableImpl

protected boolean isReadableImpl(java.lang.Object bean,
                                 java.lang.String propertyName)
                          throws java.lang.Exception
Implementation of BeanReflector.isReadable(Object, String). Default implementation assumes that all properties of the bean specified by BeanReflector.getPropertyNames(Object) are readable.

Throws:
java.lang.Exception

isWriteable

public final boolean isWriteable(java.lang.Object bean,
                                 java.lang.String propertyName)
BeanReflector.isWriteable(Object, String)

Parameters:
bean -
propertyName -
Returns:
boolean

isWriteableImpl

protected boolean isWriteableImpl(java.lang.Object bean,
                                  java.lang.String propertyName)
                           throws java.lang.Exception
Implementation of BeanReflector.isWriteable(Object, String). Default implementation assumes that all readable properties are also writeable. One exception to this is when this reflector is an IndexedContainerReflector but not a MutableIndexedContainerReflector, in which case no properties are considered writeable.

Throws:
java.lang.Exception

isValidIndex

protected boolean isValidIndex(java.lang.Object bean,
                               java.lang.String propertyName)
                        throws ReflectionException
Learn whether propertyName denotes a valid numeric property index for bean.

Parameters:
bean -
propertyName -
Returns:
boolean
Throws:
ReflectionException

set

public final void set(java.lang.Object bean,
                      java.lang.String propertyName,
                      java.lang.Object propertyValue)
               throws ReflectionException
BeanReflector.set(Object, String, Object)

Parameters:
bean -
propertyName -
propertyValue -
Throws:
ReflectionException

setImpl

protected void setImpl(java.lang.Object bean,
                       java.lang.String propertyName,
                       java.lang.Object value)
                throws java.lang.Exception
Implementation of BeanReflector.set(Object, String, Object). Implementation automatically provided for MutableIndexedContainerReflectors. For other reflectors, throws an UnsupportedOperationException.

Throws:
java.lang.Exception

get

public final java.lang.Object get(java.lang.Object bean,
                                  java.lang.String propertyName)
                           throws ReflectionException
BeanReflector.get(Object, String)

Parameters:
bean -
propertyName -
Returns:
Throws:
ReflectionException

getImpl

protected java.lang.Object getImpl(java.lang.Object bean,
                                   java.lang.String propertyName)
                            throws java.lang.Exception
Implementation of BeanReflector.get(Object, String). Implementation automatically provided for IndexedContainerReflectors. For other reflectors, throws an UnsupportedOperationException.

Throws:
java.lang.Exception

getType

public final java.lang.Class getType(java.lang.Object bean,
                                     java.lang.String propertyName)
                              throws ReflectionException
BeanReflector.getType(Object, String)

Parameters:
bean -
propertyName -
Returns:
Throws:
ReflectionException

getTypeImpl

protected java.lang.Class getTypeImpl(java.lang.Object bean,
                                      java.lang.String propertyName)
                               throws java.lang.Exception
Implementation of BeanReflector.getType(Object, String). Default implementation provided. For IndexedContainerReflectors, returns the type by calling ContainerReflector.getContainedType(Class). For other reflectors, checks the type of the property by calling get(Object, String).

Throws:
java.lang.Exception

getContainedType

public final java.lang.Class getContainedType(java.lang.Class clazz)
                                       throws ReflectionException
ContainerReflector.getContainedType(Class)

Parameters:
clazz -
Returns:
Throws:
ReflectionException

getContainedTypeImpl

protected java.lang.Class getContainedTypeImpl(java.lang.Class clazz)
                                        throws java.lang.Exception
Implementation of ContainerReflector.getContainedType(Class).

Throws:
java.lang.Exception

getIterator

public final java.util.Iterator getIterator(java.lang.Object container)
                                     throws ReflectionException
ContainerReflector.getIterator(Object)

Parameters:
container -
Returns:
Iterator
Throws:
ReflectionException

getIteratorImpl

protected java.util.Iterator getIteratorImpl(java.lang.Object container)
                                      throws java.lang.Exception
Implementation of ContainerReflector.getIterator(Object).

Throws:
java.lang.Exception

checkIndex

protected void checkIndex(java.lang.Object container,
                          int index)
                   throws ReflectionException
Validate index into container.

Parameters:
container -
index -
Throws:
ReflectionException

getSize

public final int getSize(java.lang.Object container)
                  throws ReflectionException
SizableReflector.getSize(Object)

Parameters:
container -
Returns:
Throws:
ReflectionException

getSizeImpl

protected int getSizeImpl(java.lang.Object container)
                   throws java.lang.Exception
Implementation of SizableReflector.getSize(Object).

Throws:
java.lang.Exception

get

public final java.lang.Object get(java.lang.Object container,
                                  int index)
                           throws ReflectionException
IndexedContainerReflector.get(Object, int)

Parameters:
container -
index -
Returns:
Throws:
ReflectionException

getImpl

protected java.lang.Object getImpl(java.lang.Object container,
                                   int index)
                            throws java.lang.Exception
Implementation of IndexedContainerReflector.get(Object, int).

Throws:
java.lang.Exception

set

public final java.lang.Object set(java.lang.Object container,
                                  int index,
                                  java.lang.Object propertyValue)
                           throws ReflectionException
MutableIndexedContainerReflector.set(Object, int, Object)

Parameters:
container -
index -
propertyValue -
Returns:
Throws:
ReflectionException

setImpl

protected java.lang.Object setImpl(java.lang.Object container,
                                   int index,
                                   java.lang.Object propertyValue)
                            throws java.lang.Exception
Implementation of MutableIndexedContainerReflector.set(Object, int, Object).

Throws:
java.lang.Exception

add

public final boolean add(java.lang.Object container,
                         java.lang.Object value)
                  throws ReflectionException
GrowableContainerReflector.add(Object, Object)

Parameters:
container -
value -
Returns:
Throws:
ReflectionException

addImpl

protected boolean addImpl(java.lang.Object container,
                          java.lang.Object value)
                   throws java.lang.Exception
Implementation of GrowableContainer.add(Object).

Throws:
java.lang.Exception

isPerformingLogging

protected boolean isPerformingLogging()
Indicates whether this reflector is writing log messages


isStrictlyTyped

public boolean isStrictlyTyped()
Indicates whether this reflector is strictly typed. If a reflector is strictly typed, the getType(Object, String) method will throw an exception if the requested property name is not a valid property of the object. Default implementation returns false.

Returns:
false.

isInitialized

protected boolean isInitialized()
Learn whether this Reflector is initialized.

Returns:
boolean

setInitialized

protected void setInitialized(boolean initialized)
Set the initialization status of this Reflector.

Parameters:
initialized -

isCachingIsReflectableCalls

public boolean isCachingIsReflectableCalls()
Learn whether this Reflector is caching isReflectable(Class) calls. Default true.

Returns:
boolean

setCachingIsReflectableCalls

public void setCachingIsReflectableCalls(boolean cachingIsReflectableCalls)
Set whether this Reflector is caching isReflectable(Class) calls.

Parameters:
cachingIsReflectableCalls -

getReflectableCallCache

protected java.util.Map getReflectableCallCache()
Get the isReflectable(Class) call cache.

Returns:
Map

setReflectableCallCache

protected void setReflectableCallCache(java.util.Map reflectableCallCache)
Set the isReflectable(Class) call cache.


isWrappingRuntimeExceptions

protected boolean isWrappingRuntimeExceptions()
Indicates whether runtime exceptions should be wrapped as ReflectionExceptions. By default, this method returns true.

Simple Reflectors in Morph will usually set this value to true so that they throw ReflectionExceptions if problems occur. User-written Reflectors are encouraged to return false so that runtime exceptions are not wrapped. This way, problems accessing data will be expressed by the native API of a user's domain objects and avoid the need to catch Morph-specific exceptions (assuming the use of runtime exceptions in said domain objects).

Returns:
true
Since:
Morph 1.1

getReflectorName

public java.lang.String getReflectorName()
Get the reflectorName.

Returns:
String
Since:
Morph 1.1

setReflectorName

public void setReflectorName(java.lang.String reflectorName)
Set the reflectorName.

Parameters:
reflectorName - the String to set
Since:
Morph 1.1

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object
Since:
Morph 1.1


Copyright © 2004-2008.