Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
research:software:reflex:documentation:create_reflextive_objects [2007/08/20 17:23] – admin | research:software:reflex:documentation:create_reflextive_objects [2007/08/24 22:09] (current) – rtoledo | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Create Reflective Objects ====== | ||
+ | Up to this point of the tutorial, we have seen how to apply aspects to classes, altering their structure and/or behavior. But sometimes it is useful to alter the structure and/or behaviour of just one instance. If we only were limited to what we have seen we would have to create a empty subclass and apply the links to it. To avoid this kind of problems, Reflex provides an utility method Reflex.createObject, | ||
+ | |||
+ | |||
+ | |||
+ | ===== API ===== | ||
+ | |||
+ | The signature of the method is: | ||
+ | <code java> | ||
+ | public static <T> T createObject( | ||
+ | String aClassName, | ||
+ | Object[] aArgs, | ||
+ | ConfigurationObject aConfObject | ||
+ | ) | ||
+ | </ | ||
+ | The first parameter is the class name we want to get an instance from. Then, the arguments to create the object must be specified. Finally, a '' | ||
+ | |||
+ | Let us review the (more interesting) part of the API of the '' | ||
+ | <code java> | ||
+ | public SLink addSMetaobject(SMetaobject aSMetaobject) | ||
+ | </ | ||
+ | This method allow us to add a structural metaobject to the configuration. Notice that we do not have to specify a '' | ||
+ | |||
+ | It returns a SLink that is associated with the specified structural metaobject. | ||
+ | <code java> | ||
+ | public BLink addBMetaobjectDefinition( | ||
+ | MODefinition aMODef, | ||
+ | Class aOp, | ||
+ | OperationSelector aOpSelector | ||
+ | ) | ||
+ | </ | ||
+ | This method allows to add a metaobject definition for the operation and operation selector specified. As in the '' | ||
+ | |||
+ | It returns the '' | ||
+ | |||
+ | ===== Example of Usage ===== | ||
+ | |||
+ | In the Reflex distribution there is a complete example that uses all the methods listed above to create a reflective instance. The code can be found [[http:// | ||
+ | |||
+ | Nevertheless, | ||
+ | <code java> | ||
+ | MODefinition theMO = new MODefinition.SharedMO(System.out); | ||
+ | |||
+ | ConfigurationObject theConfigObject = new ConfigurationObject(); | ||
+ | theConfigObject.addBMetaobjectDefinition( | ||
+ | theMO, | ||
+ | MsgReceive.class, | ||
+ | new DeclaredInOS(" | ||
+ | ); | ||
+ | |||
+ | BLink theLink = theConfigObject.getBLinkFor(theMO); | ||
+ | theLink.setControl(Control.BEFORE_AFTER); | ||
+ | theLink.setCall( | ||
+ | Control.BEFORE, | ||
+ | System.out.getClass().getName(), | ||
+ | ); | ||
+ | theLink.setCall( | ||
+ | Control.AFTER, | ||
+ | System.out.getClass().getName(), | ||
+ | ); | ||
+ | </ | ||
+ | |||
+ | Here we use the same metaobject definition and link configuration as in the original example. The difference is that we do not have to: | ||
+ | * Specify a '' | ||
+ | * Define the '' | ||
+ | |||
+ | Then if we use this object: | ||
+ | <code java> | ||
+ | System.out.println(" | ||
+ | Fibonacci theInstance = Reflex.createObject( | ||
+ | Fibonacci.class, | ||
+ | ); | ||
+ | System.out.println(" | ||
+ | </ | ||
+ | We should see output like the following: | ||
+ | < | ||
+ | get([5]) | ||
+ | Before message receive: get([4]) | ||
+ | Before message receive: get([3]) | ||
+ | Before message receive: get([2]) | ||
+ | Before message receive: get([1]) | ||
+ | After message receive: get is returning: [1] | ||
+ | Before message receive: get([0]) | ||
+ | ... | ||
+ | After message receive: get is returning: [5] | ||
+ | fib[5] = 5 | ||
+ | </ | ||
+ | However, if we invoke the method over a normal instace: | ||
+ | <code java> | ||
+ | System.out.println(" | ||
+ | theInstance = new Fibonacci(); | ||
+ | System.out.println(" | ||
+ | </ | ||
+ | We do not get any tracing: | ||
+ | < | ||
+ | --------------- Normal Object ------------------------------- | ||
+ | fib[5] = 5 | ||
+ | </ | ||
+ | The complete code of this class (we **do not** need link providers here because the links are defined at runtime) is [[http:// | ||
+ | < | ||
+ | Windows | ||
+ | %java -classpath | ||
+ | " | ||
+ | reflex.examples.tutorial.CreateObjectTracingAspect | ||
+ | |||
+ | Linux | ||
+ | %java -classpath | ||
+ | " | ||
+ | reflex.examples.tutorial.CreateObjectTracingAspect | ||
+ | </ | ||
+ | |||
+ | ===== Limitations ===== | ||
+ | |||
+ | There are some limitations for this part of Reflex that are not present if we use link providers. All of them are related to the way the '' | ||
+ | * The specified class must not be declared final. | ||
+ | * The operations must be callee-side. At this moment, there are two operations of this kind: '' |