1 7 8 package java.beans; 9 10 import java.lang.ref.Reference ; 11 import java.lang.ref.WeakReference ; 12 13 import java.lang.reflect.Method ; 14 15 import java.util.List ; 16 import java.util.ArrayList ; 17 18 import com.sun.beans.ObjectHandler; 19 20 24 25 public class MethodDescriptor extends FeatureDescriptor { 26 27 private Reference methodRef; 28 29 private String [] paramNames; 30 31 private List params; 32 33 private ParameterDescriptor parameterDescriptors[]; 34 35 41 public MethodDescriptor(Method method) { 42 this(method, null); 43 } 44 45 46 55 public MethodDescriptor(Method method, 56 ParameterDescriptor parameterDescriptors[]) { 57 setName(method.getName()); 58 setMethod(method); 59 this.parameterDescriptors = parameterDescriptors; 60 } 61 62 67 public synchronized Method getMethod() { 68 Method method = getMethod0(); 69 if (method == null) { 70 Class cls = getClass0(); 71 if (cls != null) { 72 Class [] params = getParams(); 73 if (params == null) { 74 for (int i = 0; i < 3; i++) { 75 method = Introspector.findMethod(cls, getName(), i, null); 79 if (method != null) { 80 break; 81 } 82 } 83 } else { 84 method = Introspector.findMethod(cls, getName(), 85 params.length, params); 86 } 87 setMethod(method); 88 } 89 } 90 return method; 91 } 92 93 private synchronized void setMethod(Method method) { 94 if (method == null) { 95 return; 96 } 97 if (getClass0() == null) { 98 setClass0(method.getDeclaringClass()); 99 } 100 setParams(method.getParameterTypes()); 101 methodRef = createReference(method, true); 102 } 103 104 private Method getMethod0() { 105 return (Method )getObject(methodRef); 106 } 107 108 private synchronized void setParams(Class [] param) { 109 if (param == null) { 110 return; 111 } 112 paramNames = new String [param.length]; 113 params = new ArrayList (param.length); 114 for (int i = 0; i < param.length; i++) { 115 paramNames[i] = param[i].getName(); 116 params.add(new WeakReference (param[i])); 117 } 118 } 119 120 String [] getParamNames() { 122 return paramNames; 123 } 124 125 private synchronized Class [] getParams() { 126 Class [] clss = new Class [params.size()]; 127 128 for (int i = 0; i < params.size(); i++) { 129 Reference ref = (Reference )params.get(i); 130 Class cls = (Class )ref.get(); 131 if (cls == null) { 132 return null; 133 } else { 134 clss[i] = cls; 135 } 136 } 137 return clss; 138 } 139 140 147 public ParameterDescriptor [] getParameterDescriptors() { 148 return parameterDescriptors; 149 } 150 151 167 168 175 176 MethodDescriptor(MethodDescriptor x, MethodDescriptor y) { 177 super(x,y); 178 179 methodRef = x.methodRef; 180 if (y.methodRef != null) { 181 methodRef = y.methodRef; 182 } 183 params = x.params; 184 if (y.params != null) { 185 params = y.params; 186 } 187 paramNames = x.paramNames; 188 if (y.paramNames != null) { 189 paramNames = y.paramNames; 190 } 191 192 parameterDescriptors = x.parameterDescriptors; 193 if (y.parameterDescriptors != null) { 194 parameterDescriptors = y.parameterDescriptors; 195 } 196 } 197 198 202 MethodDescriptor(MethodDescriptor old) { 203 super(old); 204 205 methodRef = old.methodRef; 206 params = old.params; 207 paramNames = old.paramNames; 208 209 if (old.parameterDescriptors != null) { 210 int len = old.parameterDescriptors.length; 211 parameterDescriptors = new ParameterDescriptor [len]; 212 for (int i = 0; i < len ; i++) { 213 parameterDescriptors[i] = new ParameterDescriptor (old.parameterDescriptors[i]); 214 } 215 } 216 } 217 218 } 219 | Popular Tags |