author | malenkov |
Mon, 17 Dec 2012 16:58:56 +0400 | |
changeset 14887 | 226dd1cda199 |
parent 11120 | f8576c769572 |
child 20110 | 85b98c3054f6 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14887
226dd1cda199
8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents:
11120
diff
changeset
|
2 |
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.beans; |
|
27 |
||
28 |
import java.lang.ref.Reference; |
|
29 |
import java.lang.ref.WeakReference; |
|
30 |
import java.lang.reflect.Method; |
|
31 |
import java.util.List; |
|
32 |
import java.util.ArrayList; |
|
33 |
||
34 |
/** |
|
35 |
* A MethodDescriptor describes a particular method that a Java Bean |
|
36 |
* supports for external access from other components. |
|
37 |
*/ |
|
38 |
||
39 |
public class MethodDescriptor extends FeatureDescriptor { |
|
40 |
||
41 |
private Reference<Method> methodRef; |
|
42 |
||
43 |
private String[] paramNames; |
|
44 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
45 |
private List<WeakReference<Class<?>>> params; |
2 | 46 |
|
47 |
private ParameterDescriptor parameterDescriptors[]; |
|
48 |
||
49 |
/** |
|
50 |
* Constructs a <code>MethodDescriptor</code> from a |
|
51 |
* <code>Method</code>. |
|
52 |
* |
|
53 |
* @param method The low-level method information. |
|
54 |
*/ |
|
55 |
public MethodDescriptor(Method method) { |
|
56 |
this(method, null); |
|
57 |
} |
|
58 |
||
59 |
||
60 |
/** |
|
61 |
* Constructs a <code>MethodDescriptor</code> from a |
|
62 |
* <code>Method</code> providing descriptive information for each |
|
63 |
* of the method's parameters. |
|
64 |
* |
|
65 |
* @param method The low-level method information. |
|
66 |
* @param parameterDescriptors Descriptive information for each of the |
|
67 |
* method's parameters. |
|
68 |
*/ |
|
69 |
public MethodDescriptor(Method method, |
|
70 |
ParameterDescriptor parameterDescriptors[]) { |
|
71 |
setName(method.getName()); |
|
72 |
setMethod(method); |
|
14887
226dd1cda199
8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents:
11120
diff
changeset
|
73 |
this.parameterDescriptors = (parameterDescriptors != null) |
226dd1cda199
8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents:
11120
diff
changeset
|
74 |
? parameterDescriptors.clone() |
226dd1cda199
8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents:
11120
diff
changeset
|
75 |
: null; |
2 | 76 |
} |
77 |
||
78 |
/** |
|
79 |
* Gets the method that this MethodDescriptor encapsualtes. |
|
80 |
* |
|
81 |
* @return The low-level description of the method |
|
82 |
*/ |
|
83 |
public synchronized Method getMethod() { |
|
84 |
Method method = getMethod0(); |
|
85 |
if (method == null) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
86 |
Class<?> cls = getClass0(); |
5947
0e6f2837eeca
6707234: Method returned by Introspector.internalFindMethod not necessarily most specific
malenkov
parents:
5506
diff
changeset
|
87 |
String name = getName(); |
0e6f2837eeca
6707234: Method returned by Introspector.internalFindMethod not necessarily most specific
malenkov
parents:
5506
diff
changeset
|
88 |
if ((cls != null) && (name != null)) { |
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
89 |
Class<?>[] params = getParams(); |
2 | 90 |
if (params == null) { |
91 |
for (int i = 0; i < 3; i++) { |
|
92 |
// Find methods for up to 2 params. We are guessing here. |
|
93 |
// This block should never execute unless the classloader |
|
94 |
// that loaded the argument classes disappears. |
|
6657
15dbb366c6a3
6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents:
5947
diff
changeset
|
95 |
method = Introspector.findMethod(cls, name, i, null); |
2 | 96 |
if (method != null) { |
97 |
break; |
|
98 |
} |
|
99 |
} |
|
100 |
} else { |
|
6657
15dbb366c6a3
6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents:
5947
diff
changeset
|
101 |
method = Introspector.findMethod(cls, name, params.length, params); |
2 | 102 |
} |
103 |
setMethod(method); |
|
104 |
} |
|
105 |
} |
|
106 |
return method; |
|
107 |
} |
|
108 |
||
109 |
private synchronized void setMethod(Method method) { |
|
110 |
if (method == null) { |
|
111 |
return; |
|
112 |
} |
|
113 |
if (getClass0() == null) { |
|
114 |
setClass0(method.getDeclaringClass()); |
|
115 |
} |
|
116 |
setParams(getParameterTypes(getClass0(), method)); |
|
117 |
this.methodRef = getSoftReference(method); |
|
118 |
} |
|
119 |
||
120 |
private Method getMethod0() { |
|
121 |
return (this.methodRef != null) |
|
122 |
? this.methodRef.get() |
|
123 |
: null; |
|
124 |
} |
|
125 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
126 |
private synchronized void setParams(Class<?>[] param) { |
2 | 127 |
if (param == null) { |
128 |
return; |
|
129 |
} |
|
130 |
paramNames = new String[param.length]; |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
131 |
params = new ArrayList<>(param.length); |
2 | 132 |
for (int i = 0; i < param.length; i++) { |
133 |
paramNames[i] = param[i].getName(); |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
134 |
params.add(new WeakReference<Class<?>>(param[i])); |
2 | 135 |
} |
136 |
} |
|
137 |
||
138 |
// pp getParamNames used as an optimization to avoid method.getParameterTypes. |
|
139 |
String[] getParamNames() { |
|
140 |
return paramNames; |
|
141 |
} |
|
142 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
143 |
private synchronized Class<?>[] getParams() { |
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
144 |
Class<?>[] clss = new Class<?>[params.size()]; |
2 | 145 |
|
146 |
for (int i = 0; i < params.size(); i++) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
147 |
Reference<? extends Class<?>> ref = (Reference<? extends Class<?>>)params.get(i); |
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
148 |
Class<?> cls = ref.get(); |
2 | 149 |
if (cls == null) { |
150 |
return null; |
|
151 |
} else { |
|
152 |
clss[i] = cls; |
|
153 |
} |
|
154 |
} |
|
155 |
return clss; |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Gets the ParameterDescriptor for each of this MethodDescriptor's |
|
160 |
* method's parameters. |
|
161 |
* |
|
162 |
* @return The locale-independent names of the parameters. May return |
|
163 |
* a null array if the parameter names aren't known. |
|
164 |
*/ |
|
165 |
public ParameterDescriptor[] getParameterDescriptors() { |
|
14887
226dd1cda199
8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents:
11120
diff
changeset
|
166 |
return (this.parameterDescriptors != null) |
226dd1cda199
8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents:
11120
diff
changeset
|
167 |
? this.parameterDescriptors.clone() |
226dd1cda199
8005065: [findbugs] reference to mutable array in JavaBeans
malenkov
parents:
11120
diff
changeset
|
168 |
: null; |
2 | 169 |
} |
170 |
||
171 |
/* |
|
172 |
* Package-private constructor |
|
173 |
* Merge two method descriptors. Where they conflict, give the |
|
174 |
* second argument (y) priority over the first argument (x). |
|
175 |
* @param x The first (lower priority) MethodDescriptor |
|
176 |
* @param y The second (higher priority) MethodDescriptor |
|
177 |
*/ |
|
178 |
||
179 |
MethodDescriptor(MethodDescriptor x, MethodDescriptor y) { |
|
180 |
super(x,y); |
|
181 |
||
182 |
methodRef = x.methodRef; |
|
183 |
if (y.methodRef != null) { |
|
184 |
methodRef = y.methodRef; |
|
185 |
} |
|
186 |
params = x.params; |
|
187 |
if (y.params != null) { |
|
188 |
params = y.params; |
|
189 |
} |
|
190 |
paramNames = x.paramNames; |
|
191 |
if (y.paramNames != null) { |
|
192 |
paramNames = y.paramNames; |
|
193 |
} |
|
194 |
||
195 |
parameterDescriptors = x.parameterDescriptors; |
|
196 |
if (y.parameterDescriptors != null) { |
|
197 |
parameterDescriptors = y.parameterDescriptors; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
/* |
|
202 |
* Package-private dup constructor |
|
203 |
* This must isolate the new object from any changes to the old object. |
|
204 |
*/ |
|
205 |
MethodDescriptor(MethodDescriptor old) { |
|
206 |
super(old); |
|
207 |
||
208 |
methodRef = old.methodRef; |
|
209 |
params = old.params; |
|
210 |
paramNames = old.paramNames; |
|
211 |
||
212 |
if (old.parameterDescriptors != null) { |
|
213 |
int len = old.parameterDescriptors.length; |
|
214 |
parameterDescriptors = new ParameterDescriptor[len]; |
|
215 |
for (int i = 0; i < len ; i++) { |
|
216 |
parameterDescriptors[i] = new ParameterDescriptor(old.parameterDescriptors[i]); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
||
4960
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
221 |
void appendTo(StringBuilder sb) { |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
222 |
appendTo(sb, "method", this.methodRef); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
223 |
if (this.parameterDescriptors != null) { |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
224 |
sb.append("; parameterDescriptors={"); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
225 |
for (ParameterDescriptor pd : this.parameterDescriptors) { |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
226 |
sb.append(pd).append(", "); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
227 |
} |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
228 |
sb.setLength(sb.length() - 2); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
229 |
sb.append("}"); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
230 |
} |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
2
diff
changeset
|
231 |
} |
2 | 232 |
} |