org.iqual.chaplin
Class SimpleContextHandler

java.lang.Object
  extended by org.iqual.chaplin.SimpleContextHandler
All Implemented Interfaces:
ContextHandler, Injected, FilterableMessageReceiver, MessageReceiver

public class SimpleContextHandler
extends Object
implements ContextHandler, Injected, FilterableMessageReceiver

This ContextHandler implementation combines both the map and reflection approaches. It means that the context fields can be accessed by calling the set/get methods or they can be declared in the compile time as fields in a subclass.

Since:
Mar 7, 2009 3:28:52 PM
Author:
Zbynek Slajchrt

Constructor Summary
SimpleContextHandler()
           
SimpleContextHandler(Object instance)
           
SimpleContextHandler(Object instance, boolean recursive)
          Collect all declared fields which hold references to components.
 
Method Summary
 Object $$$$$$()
           
 void $$$$$$(Object context)
           
 boolean canInvoke(Object component, String methodName, Class[] argTypes)
           
 Field findFieldByType(Class clazz, Class type)
           
 Method findInSuperClass(Class clazz, String methodName, Class[] argTypes)
           
 Method findMethod(Class clazz, String methodName, Class[] argTypes)
           
 Method findMethod(String methodName, Class[] argTypes)
           
 Object get(String fieldName)
           
 Object get(String fieldName, Class fieldType, Object instance)
           
 Field getField(String fieldName)
           
 Field getFieldByType(Class clazz, Class type)
           
 Collection<Field> getFields()
           
 Object getFieldValue(Field field)
           
 Object getInstance()
           
 Method getMethod(String methodKey)
           
 Collection<Method> getMethods()
           
 boolean hasBehavior(String name, Class... argTypes)
           
 boolean hasByName(String fieldName)
           
 boolean hasByType(Class fieldType)
           
 boolean hasValue(String fieldName, Class fieldType)
           
protected  boolean isIgnoredField(Field field)
          This method is called only once for each field.
protected  boolean isIgnoredMethod(Method method)
          This method is called only once for each method.
protected  boolean isIgnoredSuperClass(Class superClass)
           
 boolean isMessageAcceptable(Message message)
           
 void onMessageReceived(Message message, MessageReplies replies, MessageReceiversChain chain)
          Called when a request is made.
 void removeBehavior(String name, Class... argTypes)
          Remove the behavior determined by the name and the argument types.
 void set(String fieldName, Class fieldType, Object value, Object instance)
           
 void set(String fieldName, Object value)
           
 void setBehavior(Object behavioralObject, String name, Class... argTypes)
          Add behavior represented by the behavior object.
 void setByName(String fieldName, Object value)
           
 void setByType(Class fieldType, Object value)
           
 void setFieldValue(Field field, Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleContextHandler

public SimpleContextHandler()

SimpleContextHandler

public SimpleContextHandler(Object instance)

SimpleContextHandler

public SimpleContextHandler(Object instance,
                            boolean recursive)
Collect all declared fields which hold references to components. The field is considered holding a reference to a component as long as its type is not primitive and the field is final. Moreover, the field must not be a context field.

Parameters:
instance -
Method Detail

getInstance

public Object getInstance()

get

public Object get(String fieldName,
                  Class fieldType,
                  Object instance)
           throws Exception
Throws:
Exception

getFieldValue

public Object getFieldValue(Field field)
                     throws IllegalAccessException
Throws:
IllegalAccessException

setFieldValue

public void setFieldValue(Field field,
                          Object value)
                   throws IllegalAccessException
Throws:
IllegalAccessException

set

public void set(String fieldName,
                Class fieldType,
                Object value,
                Object instance)
         throws IllegalAccessException
Throws:
IllegalAccessException

setByName

public void setByName(String fieldName,
                      Object value)
               throws IllegalAccessException
Throws:
IllegalAccessException

setByType

public void setByType(Class fieldType,
                      Object value)
               throws IllegalAccessException
Throws:
IllegalAccessException

get

public Object get(String fieldName)
           throws Exception
Throws:
Exception

set

public void set(String fieldName,
                Object value)
         throws IllegalAccessException
Throws:
IllegalAccessException

hasValue

public boolean hasValue(String fieldName,
                        Class fieldType)

hasByName

public boolean hasByName(String fieldName)

hasByType

public boolean hasByType(Class fieldType)

setBehavior

public void setBehavior(Object behavioralObject,
                        String name,
                        Class... argTypes)
Add behavior represented by the behavior object. The behavioral object must be either an instance of ContextHandler or an object which owns the adequate method.

Parameters:
behavioralObject - the behavioral object
name - the behavior name. If the behavioral object is an object it must have a method with that name. If the behavioral object is a context handler it must support that behavior in its invoke method.
argTypes - the argument types

removeBehavior

public void removeBehavior(String name,
                           Class... argTypes)
Remove the behavior determined by the name and the argument types.

Parameters:
name - the behavior name
argTypes - the argument types

hasBehavior

public boolean hasBehavior(String name,
                           Class... argTypes)
Parameters:
name - the behavior name
argTypes - the argument types
Returns:
true if this context has the behavior determined by the name and the argument types.

isMessageAcceptable

public boolean isMessageAcceptable(Message message)
Specified by:
isMessageAcceptable in interface FilterableMessageReceiver

onMessageReceived

public void onMessageReceived(Message message,
                              MessageReplies replies,
                              MessageReceiversChain chain)
                       throws Throwable
Description copied from interface: MessageReceiver
Called when a request is made. The receiver must call the chain's process method so that the subsequent receivers could be called. The receiver may manipulate with the arguments passed to that method. For example it may change the message that will be processed by the subsequent receivers in the chain or change the results of the subsequent message processing.

Specified by:
onMessageReceived in interface MessageReceiver
Throws:
Throwable

findMethod

public Method findMethod(String methodName,
                         Class[] argTypes)

getMethod

public Method getMethod(String methodKey)

getField

public Field getField(String fieldName)

getMethods

public Collection<Method> getMethods()

getFields

public Collection<Field> getFields()

findMethod

public Method findMethod(Class clazz,
                         String methodName,
                         Class[] argTypes)

findInSuperClass

public Method findInSuperClass(Class clazz,
                               String methodName,
                               Class[] argTypes)

canInvoke

public boolean canInvoke(Object component,
                         String methodName,
                         Class[] argTypes)

getFieldByType

public Field getFieldByType(Class clazz,
                            Class type)

isIgnoredSuperClass

protected boolean isIgnoredSuperClass(Class superClass)

isIgnoredField

protected boolean isIgnoredField(Field field)
This method is called only once for each field.

Parameters:
field -
Returns:
true if the field is not collected

isIgnoredMethod

protected boolean isIgnoredMethod(Method method)
This method is called only once for each method.

Parameters:
method -
Returns:
true if the method is not collected

findFieldByType

public Field findFieldByType(Class clazz,
                             Class type)

$$$$$$

public Object $$$$$$()
Specified by:
$$$$$$ in interface Injected

$$$$$$

public void $$$$$$(Object context)
Specified by:
$$$$$$ in interface Injected


Copyright © 2009 iquality.org. All Rights Reserved.