jtfactory.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 213 行
JAVA
213 行
package Jt;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* Jt Factory
*/
public class JtFactory extends JtFactoryMethod {
private static final long serialVersionUID = 1L;
public static final String JtCLASS_NAME = JtFactory.class.getName();
public static final String JtGET_ATTRIBUTES = "JtGET_ATTRIBUTES";
public Class stopClass = null;
public Class getStopClass() {
return stopClass;
}
public void setStopClass(Class stopClass) {
this.stopClass = stopClass;
}
public JtFactory () {
}
private boolean checkModifiers (Class cl, String prop) {
Field field;
int mod;
if (cl == null || prop == null)
return (false);
field = null;
try {
field = cl.getDeclaredField (prop); // property dup property names
} catch (Exception e) {
//handleException (e);
if (cl.getSuperclass () == null) {
handleWarning ("JtFactory.checkModifiers:" + e.getMessage());
return (false);
}
}
if (field == null) {
cl = cl.getSuperclass ();
return (checkModifiers (cl, prop));
}
mod = field.getModifiers ();
if (Modifier.isTransient (mod)) {
return (false);
}
if (Modifier.isStatic (mod)) {
return (false);
}
return (true);
}
private JtHashTable getAttributes (Object obj) {
PropertyDescriptor[] prop;
int i;
BeanInfo info = null;
//Hashtable attr;
JtMessage msg = new JtMessage (JtHashTable.JtPUT);
JtHashTable table = new JtHashTable ();
//attr = new Hashtable ();
try {
//info = Introspector.getBeanInfo(
// obj.getClass (), java.lang.Object.class);
if (stopClass != null && stopClass.isAssignableFrom(obj.getClass ()))
info = Introspector.getBeanInfo(
obj.getClass (), stopClass);
else
info = Introspector.getBeanInfo(
obj.getClass (), java.lang.Object.class);
} catch(Exception e) {
handleException (e);
return (null);
}
prop = info.getPropertyDescriptors();
for(i = 0; i < prop.length; i++) {
// Skip static and transient attributes
if (!checkModifiers (obj.getClass (),prop[i].getName())) {
continue;
}
msg.setMsgContent(prop[i]);
msg.setMsgData (prop[i].getName());
table.processMessage(msg);
//attr.put (prop[i].getName(), null);
}
return (table);
}
// Create an object of a specific class
/*
private Object create (String className) {
Class jtclass;
if (className == null)
return (null);
try {
jtclass = Class.forName ((String) className);
return (jtclass.newInstance ());
} catch (Exception e) {
handleException (e);
return (null);
}
}
*/
/**
* Process object messages.
* <ul>
* <li>JtCREATE - creates a new instance of a class.
* </ul>
*/
public Object processMessage (Object event) {
String msgid = null;
JtMessage e = (JtMessage) event;
Object content;
if (e == null)
return null;
msgid = (String) e.getMsgId ();
if (msgid == null)
return null;
if (msgid.equals (JtObject.JtCREATE_OBJECT)) {
content = (String) e.getMsgContent ();
if (content == null) {
handleError ("JtCREATE_OBJECT: invalid message. Class name is null.");
return (null);
}
return (createObject ((String) e.getMsgContent (),
e.getMsgData()));
}
if (msgid.equals (JtFactory.JtGET_ATTRIBUTES)) {
content = e.getMsgContent ();
if (content == null) {
handleError ("JtGET_ATTRIBUTES: invalid message (msgContent). Object is null.");
return (null);
}
return (getAttributes (e.getMsgContent ()));
}
return (super.processMessage (event));
}
/**
* Demonstrates the messages processed by JtFactory
*/
public static void main(String[] args) {
JtFactory factory = new JtFactory ();
JtObject obj;
JtHashTable attributes;
JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
// Create JtFactory
obj = (JtObject) factory.createObject (JtObject.JtCLASS_NAME, "object");
System.out.println (obj);
msg.setMsgContent (obj);
attributes = (JtHashTable) factory.processMessage(msg);
factory.sendMessage (attributes, new JtMessage (JtObject.JtPRINT));
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?