|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.beans.ObjectFactoryAdapter
public class ObjectFactoryAdapter
Adapater which can be used to implement the ObjectFactory interface.
This adapter provides a full implementation of the ObjectFactory interface. It provides the following
three functions:
static Object foo(String cname) {
Class oclass = ObjectFactoryAdapter.stringToClass(cname);
return ObjectFactoryAdapter.createObjectInstance(oclass);
}
ObjectFactoryAdapter ofd = new ObjectFactoryAdapter(java.util.Date);
import com.ccg.beans.ObjectFactoryAdapter;
class MyInt extends ObjectFactoryAdapter {
static int _COUNT;
public MyInt() {
super(Integer.class);
}
public Object createObjectInstance() throws IllegalStateException {
int cnt;
synchronized (MyInt.class) {
cnt = _COUNT++;
}
return new Integer(cnt);
}
public String toString() {
return "MyInt@"+_COUNT;
}
static public void main(String[] args) {
MyInt mint = new MyInt();
for (int i = 0; i < 10; i++) {
System.out.println(mint+" createObjectInstance:"+
mint.createObjectInstance());
}
}
}
| Constructor Summary | |
|---|---|
ObjectFactoryAdapter()
Initializes the object and associates the Object.class with the object. |
|
ObjectFactoryAdapter(Class oclass)
Initializes the object and associates the specified class. |
|
ObjectFactoryAdapter(String cname)
Initializes the object and associates the specified class. |
|
| Method Summary | |
|---|---|
static ObjectFactory |
createNamedObjectFactory(String name,
Class oc)
Creates a new ObjectFactory for an
associated class with an optional string name. |
Object |
createObjectInstance()
Creates a new instance of the type of object associated with the menu item. |
static Object |
createObjectInstance(Class oclass)
Creates a new instance of the object associated with the Class passed. |
Class |
getObjectClass()
Get the Class associated with the object factory. |
void |
setObjectClass(Class val)
Set the Class associated with the object factory. |
static Class |
stringToClass(String cname)
Convert a string into a Class object. |
String |
toString()
Get a string representation of the object. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public ObjectFactoryAdapter()
public ObjectFactoryAdapter(Class oclass)
val - Class to associate with menu item (must not be null).
NullPointerException - If you pass null.setObjectClass(java.lang.Class)public ObjectFactoryAdapter(String cname)
cname - Fully qualified name of the class to associate (like:
"java.util.Date").
IllegalArgumentException - If we could not resolve the string passed to a available class
within the CLASSPATH.
NullPointerException - If you pass null.setObjectClass(java.lang.Class)| Method Detail |
|---|
public static Class stringToClass(String cname)
throws NullPointerException,
IllegalArgumentException
Class object.
This static helper method is used to convert a arbitrary
string into the Class object which it
represents. This method either succeeds and returns a non-null
value, or fails and throws a exception (you never get null
back).
This method is intended to aid those implementing the ObjectFactory, but can be used by anyone.
cname - Fully qualified name of the class to associate (like:
"java.util.Date").
Class which the string value referred to
- never returns null.
IllegalArgumentException - If we could not resolve the string passed to a available class
within the CLASSPATH.
NullPointerException - If you pass null.public final void setObjectClass(Class val)
Class associated with the object factory.
This method is used to set the associated class. The class
set, should either have a usable default constructor (in order
for the factory to create new instances), OR you should override
the createObjectInstance method to
access your specialized constructor.
val - Class to associate with menu item (must not be null).
NullPointerException - If you pass null.getObjectClass()public final Class getObjectClass()
Class associated with the object factory.
getObjectClass in interface ObjectFactorysetObjectClass(java.lang.Class)
public static Object createObjectInstance(Class oclass)
throws IllegalStateException
Class passed.
This static method attempts to create a new instance of the
passed class by using the default constructor
of the class. If the class does not have a accessible default
constructor, a exception is thrown (null is never returned).
This static method is intended for those implementing the
ObjectFactory interface (in particular the
createObjectInstance method), and
hence throws a IllegalStateException instead of a IllegalArgumentException.
IllegalStateException - If the class has not been set, or we could not create a new
instance of the object using the newInstance method.setObjectClass(java.lang.Class)
public Object createObjectInstance()
throws IllegalStateException
This method attempts to create a new instance of the
associated class by using the default
constructor of the associated class. If the associated class does
not have a accessible default constructor, a exception is thrown
(null is never returned).
createObjectInstance in interface ObjectFactoryIllegalStateException - If the class has not been set, or we could not create a new
instance of the object using the newInstance method.setObjectClass(java.lang.Class)public String toString()
toString in class Object
public static ObjectFactory createNamedObjectFactory(String name,
Class oc)
ObjectFactory for an
associated class with an optional string name.
This static method allows one to construct a Object factory instance but assign it a different
textual name (set what the toString() method returns).
name - The text name you want to associate with the ObjectFactory, or
null if you want to take it from the associated object class.oc - The class to be associated with the ObjectFactory (it should
have a working default constructor).
NullPointerException - If 'oc' was null.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||