Tutorial
Access a Class
The AccessFactory creates new instances of outer classes. Outer classes are
this classes, which does not contain a $ sign in the class name. A correct class
name for the factory looks like
"org.foo.Test"
, an incorrect class name
looks like
"org.foo.Test$Help"
. The static
method newInstance required a class name. Following code create a instance:
String className = "org.foo.Test";
AccessObject fooInstA = AccessFactory.newInstance(className);
The class
org.foo.Test
must have an empty constructor. It can
be a non public. Otherwise the method throws a TextExtensionsException.
When testing a constructor with parameters, you have to call newInstance with
the an array of arguments:
String className = "org.foo.Test";
Object[] args = new Object[] { "arg1", 2, new Integer(3), "arg4" };
AccessObject fooInstB = AccessFactory.newInstance(className,args);
So the factory looks for a constructor which accepts the
given arguments. If the factory doesn't find one, then it
throws an exception.
Access a Inner Class
A inner class needs an instance of an outer class for instantiation. So the factory
cann't directly create a new instance. The program have to create a instance of the
outer class. Then the program can create the inner class.
String outerClassName = "org.foo.Test";
String innerClassName = "Help"; // instead of org.foo.Test$Help
AccessObject fooOuter = AccessFactory.newInstance(outerClassName);
AccessObject helpInner = fooOuter.newInnerInstance(innerClassName);
Access a Field
The test can very easy access a field.
AccessObject fooInstA = AccessFactory.newInstance(className);
AccessField user = fooInstA.getField("user");
user.setValue(aNewValue);
assertEquals(aNewValue,user.getValue());
If user isn't a field of the class, then the method getField throws a
TestExtensionsException. The method setValue throws an exception, if
aNewValue doesn't suite to the type of the field "user". The field "user"
can have any accessibility (public, package, protected or private).
Access a Method
Accessing a method is a little more complex. From an AccessObject the
test can simply get an AccessMethod instance:
AccessObject fooInstA = AccessFactory.newInstance(className);
String methodName = "dummyMethod";
AccessMethod dummyMethodA = fooInstA.getMethod(methodName);
The object from type AccessMethod provide access to all methods of the class
with the same methodName. Which method the programm invokes, this the method
call
of AccessMethod decides. It looks for the first method with
suitable arguments:
dummyMethodA.call(); // call the method without parameters.
dummyMethodA.call("dummyString"); // call the method with one parameter
Object[] args = new Object[] { obj1, obj2, ..., objn };
dummyMethodA.call(args); // call the method with suitable parameter types.
If AccessMethod doesn't find a method, which suites to the parameters, then
it throws a TextExtensionsException. The method can be from any kind of accessibility.
If the method call return a value, the programm can check the value so:
Object[] args = new Object[] { obj1, obj2, ..., objn };
Object response = dummyMethodA.call(args); // call the method with suitable parameter types.
assertNotNull(response);
If the method is void, then AccessMethod.call gives allways
null
back.