001: /* SerializableMethod.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Jun 21 17:57:05 2006, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: }}IS_RIGHT
016: */
017: package org.zkoss.lang.reflect;
018:
019: import java.lang.reflect.Method;
020: import java.io.Serializable;
021: import java.io.ObjectOutputStream;
022: import java.io.ObjectInputStream;
023: import java.io.IOException;
024:
025: import org.zkoss.lang.Objects;
026: import org.zkoss.lang.SystemException;
027:
028: /**
029: * A wrapper of java.lang.reflect.Method to make it serializable.
030: *
031: * @author tomyeh
032: */
033: public class SerializableMethod implements Serializable, Cloneable {
034: private static final long serialVersionUID = 20060622L;
035: private transient Method _m;
036:
037: public SerializableMethod(Method method) {
038: _m = method;
039: }
040:
041: /** Returns the method being encapsulated.
042: */
043: public final Method getMethod() {
044: return _m;
045: }
046:
047: //Serializable//
048: //NOTE: they must be declared as private
049: private synchronized void writeObject(ObjectOutputStream s)
050: throws IOException {
051: s.defaultWriteObject();
052:
053: if (_m == null) {
054: s.writeObject(null);
055: } else {
056: s.writeObject(_m.getDeclaringClass());
057: s.writeObject(_m.getName());
058:
059: final Class[] argTypes = _m.getParameterTypes();
060: s.writeInt(argTypes.length);
061: for (int j = 0; j < argTypes.length; ++j)
062: s.writeObject(argTypes[j]);
063: }
064: }
065:
066: private synchronized void readObject(ObjectInputStream s)
067: throws IOException, ClassNotFoundException {
068: s.defaultReadObject();
069:
070: final Class cls = (Class) s.readObject();
071: if (cls != null) {
072: final String nm = (String) s.readObject();
073: final int sz = s.readInt();
074: final Class[] argTypes = new Class[sz];
075: for (int j = 0; j < sz; ++j)
076: argTypes[j] = (Class) s.readObject();
077: try {
078: _m = cls.getMethod(nm, argTypes);
079: } catch (NoSuchMethodException ex) {
080: throw new SystemException("Method not found: " + nm
081: + " with " + Objects.toString(argTypes));
082: }
083: }
084: }
085:
086: //-- cloneable --//
087: public Object clone() {
088: try {
089: return super .clone();
090: } catch (CloneNotSupportedException e) {
091: throw new InternalError();
092: }
093: }
094:
095: //Object//
096: public int hashCode() {
097: return Objects.hashCode(_m);
098: }
099:
100: public boolean equals(Object o) {
101: return (o instanceof SerializableMethod)
102: && Objects.equals(_m, ((SerializableMethod) o)._m);
103: }
104:
105: public String toString() {
106: return Objects.toString(_m);
107: }
108: }
|