ErrorLog
For an actual type parameter Person
this factory generates a class PersonWithErrorLog that extends
class Person. PersonWithErrorLog overwrites all
methods of Person with methods of the same signature that wrap the
original ones. Each wrapper method prints out a message with the actual
parameters of the call, calls the original method and prints out a
message with the return value, if any.
This factory can be useful for debugging.
Factory/Java Source
The source can be found in the Factory/Java .zip-archive in factory/classes/examples/ErrorLog.factory:
<param> <var> T </var> </param>
package test;
public class
<apply>
<apply>
<class> factory.Toolbox
</class>
<method> getRelativeName
</method>
<args>
<var> T
</var>
</args>
</apply>
<method> concat </method>
<args>
<const> "WithErrorLog"
</const>
</args>
</apply>
extends <var> T </var>
{
<for> <var> I </var>
<apply>
<var> T </var>
<method> getDeclaredMethods </method>
</apply>
<body>
<var> I </var>
{
System.out.print("Calling method "+
<literal>
<apply>
<var> I </var>
<method> getName
</method>
</apply>
</literal>
+" with
arguments:");
<for>
<var> J </var>
<apply>
<class> factory.Toolbox </class>
<method> getArguments </method>
<args>
<var> I </var>
</args>
</apply>
<body>
System.out.print(" "+
<apply>
<var> J </var>
<method> getName
</method>
</apply>
);
</body>
</for>
System.out.println();
<if>
<apply>
<apply>
<apply>
<var> I
</var>
<method> getReturnType </method>
</apply>
<method> getName
</method>
</apply>
<method> equals </method>
<args>
<const> "void" </const>
</args>
</apply>
<then>
super.
<apply>
<var> I </var>
<method> getName
</method>
</apply>
<apply>
<var> I </var>
<method> getParameterTypes
</method>
</apply>;
System.out.println("Method "+
<literal>
<apply>
<var> I
</var>
<method>
getName </method>
</apply>
</literal>
+" returned.");
</then>
<else>
<apply>
<var> I </var>
<method> getReturnType
</method>
</apply>
val = super.
<apply>
<var> I </var>
<method> getName
</method>
</apply>
<apply>
<var> I </var>
<method> getParameterTypes
</method>
</apply>;
System.out.println("Method "+
<literal>
<apply>
<var> I
</var>
<method>
getName </method>
</apply>
</literal>
+" returned "+val+".");
return val;
</else>
</if>
}
</body>
</for>
}
Test Program
The following unparameterized factory
can be found in factory/src/test/ErrorLogTest.factory in the
Factory/Java .zip-archive. It applies factory ErrorLog to
class Person, instantiates an object
of the resulting class PersonWithErrorLog and calls one of its
public member methods.
package test;
class ErrorLogTest {
public static void main(String argv[]) {
<let> <var> T
</var>
<apply>
<factory> examples/ErrorLog </factory>
<args>
<const> test.Person </const> </args>
</apply>
<body>
<var> T
</var> p = new <var> T </var>();
p.myMethod(1,
"xyz");
</body> </let>
}
}
This factory can be compiled in the factory directory
with
java -classpath classes factory.Factory -javad src -classd classes src/test/ErrorLogTest
and run with
java -classpath classes test.ErrorLogTest
Execution of the class yields the following textual output:
Calling method myMethod with arguments: 1 xyz
xyz
Method myMethod returned 2.