qflib 0.98.1

de.qfs.lib.util
Class ClassWrapper

java.lang.Object
  |
  +--de.qfs.lib.util.ClassWrapper

public class ClassWrapper
extends java.lang.Object

A ClassWrapper is one possible answer to the problem of parallel class hierarchies.

These parallel class hierarchies do occur often in OO programming but cause problems with purely statically typed languages, since there is no language construct that maps to the abstract model.

This class uses Java's reflection interface to dynamically create wrapper objects that are semantically connected to objects from a parallel class hierarchy. A cache is used internally for speedup of repetitive calls.

Wrapper classes are expected to have the same name as the wrapped object's class (without package), plus a suffix. It is not necessary for the wrapping classes to have a hierarchy of the same layout as the wrapped classes.

If a wrapper class for an object does not exist, the object's baseclass is tried recursively until class Object is reached.

An example:

 // Create a ClassWrapper for package a.b.c with suffix Wrapper
 ClassWrapper wrapper = new ClassWrapper ("a.b.c", "Wrapper");
 wrapper.makeWrapper (new JTextField);
 
will first try to create an a.b.c.JTextFieldWrapper, then an a.b.c.JTextComponentWrapper, and so on until a.b.c.ObjectWrapper.

Classes of the parallel hierarchy need not be aware that they are being wrapped, so third party software can be integrated without problems. However, a class that is aware of its wrappers can implement the ClassWrapperAware interface to speed up the ClassWrapper's lookup mechanism. If wrapper objects are spread over more than one package, additional packages to search for wrappers can be added via addPackage.

Caveat: Inner classes cannot be wrapped yet, due to the $ sign in their name.

Version:
$Revision: 1.9 $
Author:
Gregor Schmid

Constructor Summary
ClassWrapper(java.lang.String packagename, java.lang.String suffix)
          Create a ClassWrapper for objects from a class hierarchy.
ClassWrapper(java.lang.String packagename, java.lang.String suffix, boolean dynamic)
          Create a ClassWrapper for objects from a class hierarchy.
 
Method Summary
 void addPackage(java.lang.String packagename)
          Add a package to be searched for classes.
 boolean isDynamic()
          Get the dynamic flag of the ClassWrapper.
 java.lang.Object makeWrapper(java.lang.Object client)
          A factory method that dynamically creates a wrapper object for the most derived class or baseclass of the client, for which a wrapper is available.
 void setDynamic(boolean dynamic)
          Set the dynamic flag of the ClassWrapper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassWrapper

public ClassWrapper(java.lang.String packagename,
                    java.lang.String suffix)
Create a ClassWrapper for objects from a class hierarchy.
Parameters:
packagename - The package to search for wrapper classes.
suffix - The suffix to append to the wrapped object's class name.

ClassWrapper

public ClassWrapper(java.lang.String packagename,
                    java.lang.String suffix,
                    boolean dynamic)
Create a ClassWrapper for objects from a class hierarchy.
Parameters:
packagename - The package to search for wrapper classes.
suffix - The suffix to append to the wrapped object's class name.
dynamic - If true, use DynamicClassLoader to create the wrappers. This will disable the lookup cache.
Method Detail

isDynamic

public final boolean isDynamic()
Get the dynamic flag of the ClassWrapper.
Returns:
The dynamic flag of the ClassWrapper.
Since:
0.98.0

setDynamic

public final void setDynamic(boolean dynamic)
Set the dynamic flag of the ClassWrapper.
Parameters:
dynamic - The dynamic flag to set.
Since:
0.98.0

makeWrapper

public java.lang.Object makeWrapper(java.lang.Object client)
A factory method that dynamically creates a wrapper object for the most derived class or baseclass of the client, for which a wrapper is available.
Parameters:
client - The object to wrap.
Returns:
The new wrapper or null, if the client cannot be wrapped.

addPackage

public void addPackage(java.lang.String packagename)
Add a package to be searched for classes. Packages added later are considered more specific and are searched first.
Parameters:
packagename - The name of the package to add.

qflib 0.98.1