⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractdomainobject.java

📁 利用java反射机制
💻 JAVA
字号:
package org.julp;import java.beans.*;import java.util.*;import java.lang.reflect.*;    /**     * Normally to make your object able to persist you need to extend this object     * If you can't or don't want to extend, just make your class     * implement org.julp.DomainObject interface and copy and paste members     * and methods from org.julp.AbstractDomainObject into your class.     */public abstract class AbstractDomainObject implements DomainObject, java.io.Serializable, Cloneable {        protected DataHolder originalValues = null;    protected int objectId = 0;    protected char sqlStatus = '\u0000';    protected boolean loaded = false;    protected boolean loading = false;    protected boolean modified = false;    public String TEMP = null;  //NetBeans bug workaround: debugger does not display value of char datatype          public String toString() {        StringBuffer sb = new StringBuffer();        Object value = null;        Method[] methods = getClass().getMethods();        for (int i = 0;i < methods.length;i++){            String methodName = methods[i].getName();            if (methodName.equals("getValuesAsHtml") || methodName.equals("getClass")){                continue;            }            if (methodName.startsWith("get") && methods[i].getParameterTypes().length == 0){                try{                    value = methods[i].invoke(this, new Object[0]);                }catch (IllegalAccessException eae){                    continue;                                    }catch (InvocationTargetException ite){                                        continue;                }                String s = methodName.substring(3, 4).toLowerCase();                StringBuffer fieldName = new StringBuffer(s);                fieldName.append(methodName.substring(4));                sb.append(fieldName);                sb.append("=");                if (value == null){                    sb.append("");                }else{                    sb.append(value);                }                sb.append("&");            }        }        sb.deleteCharAt(sb.length() - 1);        return sb.toString();    }        public void setOriginalValues(DataHolder originalValues) {        this.originalValues = originalValues;    }        public DataHolder getOriginalValues() {        return this.originalValues;    }        public void setOriginalValue(String fieldName, Object value) {        if (originalValues == null) {            throw new IllegalArgumentException("setOriginalValue: DataHolder not set");        }        originalValues.setObject(fieldName, value);    }        public Object getOriginalValue(String fieldName) {        if (originalValues == null) {            return null;        }        return this.originalValues.getObject(fieldName);    }        public char getSqlStatus() {        return this.sqlStatus;    }        /**     * This method set sqlStatus     *     */        public void setSqlStatus(char sqlStatus) {        this.sqlStatus = sqlStatus;        TEMP = new String(String.valueOf(sqlStatus));    }        /** Getter for property objectId.     * @return Value of property objectId.     *     */    public int getObjectId() {        return objectId;    }        /**          *  Every time  DomainObjectFactory is loaded this object it assigned objectId     */    public void setObjectId(int objectId) {        this.objectId = objectId;    }        public boolean store() {        if (originalValues != null && originalValues.getFieldCount() > 0){            setSqlStatus(UPDATED);            return true;        }        return false;    }        public boolean remove() {        if (originalValues != null && originalValues.getFieldCount() > 0){            setSqlStatus(DELETED);            return true;        }        return false;    }        public boolean create() {        if (originalValues == null || originalValues.getFieldCount() < 1){            setSqlStatus(INSERTED);            return true;        }        return false;    }            public boolean load() {        setLoading(true);        if (originalValues != null && originalValues.getFieldCount() > 0){                      try{                List param = new ArrayList(1);                for(int fieldIndex = 1; fieldIndex <= originalValues.getFieldCount(); fieldIndex++){                    param.clear();                    String fieldName = originalValues.getFieldName(fieldIndex);                    Object value = originalValues.getObject(fieldIndex);                    PropertyDescriptor pd = new PropertyDescriptor(fieldName, this.getClass());                    Method writeMethod = pd.getWriteMethod();                                        param.add(value);                    writeMethod.invoke(this, param.toArray());                }            }catch(IntrospectionException ie){                throw new RuntimeException(ie);            }catch(IllegalAccessException iae){                throw new RuntimeException(iae);            }catch(InvocationTargetException ite){                throw new RuntimeException(ite);            }            setLoading(false);            setLoaded(true);            return true;        }        setLoading(false);        return false;    }        /** Getter for property loaded.     * @return Value of property loaded.     *     */    public boolean isLoaded() {        return loaded;    }        /** Setter for property loaded.     * @param loaded New value of property loaded.     *     */    public void setLoaded(boolean loaded) {        this.setSqlStatus(ORIGINAL);        this.loaded = loaded;    }        /**     *  This is convinient method to use with JSP.     *  Make sure not to call this method from another method     *  of this object (or subclass) started with "get",     *  or you will get endless loop     */        public java.lang.String getValuesAsHtml() {        StringBuffer sb = new StringBuffer();        Object value = null;        Object[] arg = new Object[0];        Method[] methods = getClass().getMethods();        for (int i = 0;i < methods.length;i++){            String methodName = methods[i].getName();            if (methodName.equals("getValuesAsHtml") || methodName.equals("getClass")){                continue;            }            if (methodName.startsWith("get") && methods[i].getParameterTypes().length == 0){                try{                    value = methods[i].invoke(this, arg);                }catch (IllegalAccessException eae){                    continue;                }catch (InvocationTargetException ite){                    continue;                }                String s = methodName.substring(3, 4).toLowerCase();                StringBuffer fieldName = new StringBuffer(s);                fieldName.append(methodName.substring(4));                sb.append(fieldName);                sb.append("=");                if (value == null){                    sb.append("");                }else{                    try{                        sb.append(java.net.URLEncoder.encode(value.toString(), "UTF-8"));                    }catch (java.io.UnsupportedEncodingException uee){                        throw new RuntimeException(uee);                    }                }                sb.append("&");            }        }        sb.deleteCharAt(sb.length() - 1);        return sb.toString();    }        /**     * Indicator that this object in proccess of loading with values from database          */        public boolean isLoading() {        return this.loading;    }        public void setLoading(boolean loading) {        this.loading = loading;    }    /**     * Indicator that this object is modified. You have to call it yourself     *     */        public boolean isModified() {        return this.modified;    }        public void setModified(boolean modified) {        this.modified = modified;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -