KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > beans > MethodDescriptor


1 /*
2  * @(#)MethodDescriptor.java 1.31 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.beans;
9
10 import java.lang.ref.Reference JavaDoc;
11 import java.lang.ref.WeakReference JavaDoc;
12
13 import java.lang.reflect.Method JavaDoc;
14
15 import java.util.List JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 import com.sun.beans.ObjectHandler;
19
20 /**
21  * A MethodDescriptor describes a particular method that a Java Bean
22  * supports for external access from other components.
23  */

24
25 public class MethodDescriptor extends FeatureDescriptor JavaDoc {
26
27     private Reference JavaDoc methodRef;
28
29     private String JavaDoc[] paramNames;
30
31     private List JavaDoc params;
32
33     private ParameterDescriptor JavaDoc parameterDescriptors[];
34
35     /**
36      * Constructs a <code>MethodDescriptor</code> from a
37      * <code>Method</code>.
38      *
39      * @param method The low-level method information.
40      */

41     public MethodDescriptor(Method JavaDoc method) {
42     this(method, null);
43     }
44
45
46     /**
47      * Constructs a <code>MethodDescriptor</code> from a
48      * <code>Method</code> providing descriptive information for each
49      * of the method's parameters.
50      *
51      * @param method The low-level method information.
52      * @param parameterDescriptors Descriptive information for each of the
53      * method's parameters.
54      */

55     public MethodDescriptor(Method JavaDoc method,
56         ParameterDescriptor JavaDoc parameterDescriptors[]) {
57     setName(method.getName());
58     setMethod(method);
59     this.parameterDescriptors = parameterDescriptors;
60     }
61
62     /**
63      * Gets the method that this MethodDescriptor encapsualtes.
64      *
65      * @return The low-level description of the method
66      */

67     public synchronized Method JavaDoc getMethod() {
68     Method JavaDoc method = getMethod0();
69     if (method == null) {
70         Class JavaDoc cls = getClass0();
71         if (cls != null) {
72         Class JavaDoc[] params = getParams();
73         if (params == null) {
74             for (int i = 0; i < 3; i++) {
75             // Find methods for up to 2 params. We are guessing here.
76
// This block should never execute unless the classloader
77
// that loaded the argument classes disappears.
78
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 JavaDoc 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 JavaDoc getMethod0() {
105     return (Method JavaDoc)getObject(methodRef);
106     }
107
108     private synchronized void setParams(Class JavaDoc[] param) {
109     if (param == null) {
110         return;
111     }
112     paramNames = new String JavaDoc[param.length];
113     params = new ArrayList JavaDoc(param.length);
114     for (int i = 0; i < param.length; i++) {
115         paramNames[i] = param[i].getName();
116         params.add(new WeakReference JavaDoc(param[i]));
117     }
118     }
119
120     // pp getParamNames used as an optimization to avoid method.getParameterTypes.
121
String JavaDoc[] getParamNames() {
122     return paramNames;
123     }
124
125     private synchronized Class JavaDoc[] getParams() {
126     Class JavaDoc[] clss = new Class JavaDoc[params.size()];
127
128     for (int i = 0; i < params.size(); i++) {
129         Reference JavaDoc ref = (Reference JavaDoc)params.get(i);
130         Class JavaDoc cls = (Class JavaDoc)ref.get();
131         if (cls == null) {
132         return null;
133         } else {
134         clss[i] = cls;
135         }
136     }
137     return clss;
138     }
139
140     /**
141      * Gets the ParameterDescriptor for each of this MethodDescriptor's
142      * method's parameters.
143      *
144      * @return The locale-independent names of the parameters. May return
145      * a null array if the parameter names aren't known.
146      */

147     public ParameterDescriptor JavaDoc[] getParameterDescriptors() {
148     return parameterDescriptors;
149     }
150
151     /*
152     public String toString() {
153     String message = "name=" + getName();
154     Class cls = getClass0();
155     if (cls != null) {
156         message += ", class=";
157         message += cls.getName();
158     }
159     String[] names = getParamNames();
160     if (names != null) {
161         for (int i = 0; i < names.length; i++) {
162         message += ", param=" + names[i];
163         }
164     }
165     return message;
166     } */

167
168     /*
169      * Package-private constructor
170      * Merge two method descriptors. Where they conflict, give the
171      * second argument (y) priority over the first argument (x).
172      * @param x The first (lower priority) MethodDescriptor
173      * @param y The second (higher priority) MethodDescriptor
174      */

175
176     MethodDescriptor(MethodDescriptor JavaDoc x, MethodDescriptor JavaDoc 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     /*
199      * Package-private dup constructor
200      * This must isolate the new object from any changes to the old object.
201      */

202     MethodDescriptor(MethodDescriptor JavaDoc 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 JavaDoc[len];
212         for (int i = 0; i < len ; i++) {
213             parameterDescriptors[i] = new ParameterDescriptor JavaDoc(old.parameterDescriptors[i]);
214         }
215     }
216     }
217
218 }
219
Popular Tags