| Version 5 (modified by dsowen, 18 years ago) (diff) |
|---|
accesslib
In Java, variables refer to instances of objects. There's no way to refer to the reference of a variable, in order to indirectly change to what object it refers. accesslib provides an abstract mechanism for manipulating references (as opposed to manipulating objects).
Download
| Version | Formats |
| 2005-10-04T22:55 | zip |
Example
A quick example:
class Example {
int i;
void manipulate(Accessor a)
{
if(a.getType() == String.class) {
a.setValue("Hello, world!");
} else if(a.getType() == Integer.class) {
a.setValue(new Integer(13));
}
}
void foobar()
{
HashMap map = new HashMap();
Accessor a = new MapAccessor(map, "my key", String.class);
manipulate(a);
// Prints "Hello, world!".
System.out.println(map.get("my key"));
i = 0;
a = new IAccessor();
manipulate(a);
// Prints "13".
System.out.println(i);
}
class IAccessor implements Accessor {
void Class getType() { return Integer.class; }
void clearValue() { i = 0; }
Object getValue() { return Integer.valueOf(i); }
void setValue(Object value) { i = ((Integer)value).intValue(); }
}
}
