author | lana |
Tue, 01 Jun 2010 14:17:38 -0700 | |
changeset 5597 | ab490f66d2cf |
parent 5506 | 202f599c92aa |
child 11120 | f8576c769572 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2010, 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 |
package java.beans; |
|
26 |
||
27 |
import java.lang.reflect.AccessibleObject; |
|
28 |
import java.lang.reflect.Array; |
|
29 |
import java.lang.reflect.Constructor; |
|
30 |
import java.lang.reflect.InvocationTargetException; |
|
31 |
import java.lang.reflect.Method; |
|
5185
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
32 |
import java.security.AccessControlContext; |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
33 |
import java.security.AccessController; |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
34 |
import java.security.PrivilegedActionException; |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
35 |
import java.security.PrivilegedExceptionAction; |
2 | 36 |
|
37 |
import com.sun.beans.finder.ClassFinder; |
|
1851
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
38 |
import com.sun.beans.finder.ConstructorFinder; |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
39 |
import com.sun.beans.finder.MethodFinder; |
2 | 40 |
import sun.reflect.misc.MethodUtil; |
41 |
||
42 |
/** |
|
43 |
* A <code>Statement</code> object represents a primitive statement |
|
44 |
* in which a single method is applied to a target and |
|
45 |
* a set of arguments - as in <code>"a.setFoo(b)"</code>. |
|
46 |
* Note that where this example uses names |
|
47 |
* to denote the target and its argument, a statement |
|
48 |
* object does not require a name space and is constructed with |
|
49 |
* the values themselves. |
|
50 |
* The statement object associates the named method |
|
51 |
* with its environment as a simple set of values: |
|
52 |
* the target and an array of argument values. |
|
53 |
* |
|
54 |
* @since 1.4 |
|
55 |
* |
|
56 |
* @author Philip Milne |
|
57 |
*/ |
|
58 |
public class Statement { |
|
59 |
||
60 |
private static Object[] emptyArray = new Object[]{}; |
|
61 |
||
62 |
static ExceptionListener defaultExceptionListener = new ExceptionListener() { |
|
63 |
public void exceptionThrown(Exception e) { |
|
64 |
System.err.println(e); |
|
65 |
// e.printStackTrace(); |
|
66 |
System.err.println("Continuing ..."); |
|
67 |
} |
|
68 |
}; |
|
69 |
||
5185
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
70 |
private final AccessControlContext acc = AccessController.getContext(); |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
71 |
private final Object target; |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
72 |
private final String methodName; |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
73 |
private final Object[] arguments; |
3240
2f79c1748c93
6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents:
1851
diff
changeset
|
74 |
ClassLoader loader; |
2 | 75 |
|
76 |
/** |
|
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
77 |
* Creates a new {@link Statement} object |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
78 |
* for the specified target object to invoke the method |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
79 |
* specified by the name and by the array of arguments. |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
80 |
* <p> |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
81 |
* The {@code target} and the {@code methodName} values should not be {@code null}. |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
82 |
* Otherwise an attempt to execute this {@code Expression} |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
83 |
* will result in a {@code NullPointerException}. |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
84 |
* If the {@code arguments} value is {@code null}, |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
85 |
* an empty array is used as the value of the {@code arguments} property. |
2 | 86 |
* |
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
87 |
* @param target the target object of this statement |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
88 |
* @param methodName the name of the method to invoke on the specified target |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
89 |
* @param arguments the array of arguments to invoke the specified method |
2 | 90 |
*/ |
91 |
@ConstructorProperties({"target", "methodName", "arguments"}) |
|
92 |
public Statement(Object target, String methodName, Object[] arguments) { |
|
93 |
this.target = target; |
|
94 |
this.methodName = methodName; |
|
95 |
this.arguments = (arguments == null) ? emptyArray : arguments; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
99 |
* Returns the target object of this statement. |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
100 |
* If this method returns {@code null}, |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
101 |
* the {@link #execute} method |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
102 |
* throws a {@code NullPointerException}. |
2 | 103 |
* |
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
104 |
* @return the target object of this statement |
2 | 105 |
*/ |
106 |
public Object getTarget() { |
|
107 |
return target; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
111 |
* Returns the name of the method to invoke. |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
112 |
* If this method returns {@code null}, |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
113 |
* the {@link #execute} method |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
114 |
* throws a {@code NullPointerException}. |
2 | 115 |
* |
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
116 |
* @return the name of the method |
2 | 117 |
*/ |
118 |
public String getMethodName() { |
|
119 |
return methodName; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
123 |
* Returns the arguments for the method to invoke. |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
124 |
* The number of arguments and their types |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
125 |
* must match the method being called. |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
126 |
* {@code null} can be used as a synonym of an empty array. |
2 | 127 |
* |
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
128 |
* @return the array of arguments |
2 | 129 |
*/ |
130 |
public Object[] getArguments() { |
|
131 |
return arguments; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
5131
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
135 |
* The {@code execute} method finds a method whose name is the same |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
136 |
* as the {@code methodName} property, and invokes the method on |
2 | 137 |
* the target. |
138 |
* |
|
139 |
* When the target's class defines many methods with the given name |
|
140 |
* the implementation should choose the most specific method using |
|
141 |
* the algorithm specified in the Java Language Specification |
|
142 |
* (15.11). The dynamic class of the target and arguments are used |
|
143 |
* in place of the compile-time type information and, like the |
|
5131
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
144 |
* {@link java.lang.reflect.Method} class itself, conversion between |
2 | 145 |
* primitive values and their associated wrapper classes is handled |
146 |
* internally. |
|
147 |
* <p> |
|
148 |
* The following method types are handled as special cases: |
|
149 |
* <ul> |
|
150 |
* <li> |
|
151 |
* Static methods may be called by using a class object as the target. |
|
152 |
* <li> |
|
153 |
* The reserved method name "new" may be used to call a class's constructor |
|
154 |
* as if all classes defined static "new" methods. Constructor invocations |
|
5131
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
155 |
* are typically considered {@code Expression}s rather than {@code Statement}s |
2 | 156 |
* as they return a value. |
157 |
* <li> |
|
5131
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
158 |
* The method names "get" and "set" defined in the {@link java.util.List} |
2 | 159 |
* interface may also be applied to array instances, mapping to |
5131
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
160 |
* the static methods of the same name in the {@code Array} class. |
2 | 161 |
* </ul> |
5131
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
162 |
* |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
163 |
* @throws NullPointerException if the value of the {@code target} or |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
164 |
* {@code methodName} property is {@code null} |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
165 |
* @throws NoSuchMethodException if a matching method is not found |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
166 |
* @throws SecurityException if a security manager exists and |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
167 |
* it denies the method invocation |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
168 |
* @throws Exception that is thrown by the invoked method |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
169 |
* |
03037c4967f4
6707226: java.beans.Statement & java.beans.Expression miss one important usecase
malenkov
parents:
4845
diff
changeset
|
170 |
* @see java.lang.reflect.Method |
2 | 171 |
*/ |
172 |
public void execute() throws Exception { |
|
173 |
invoke(); |
|
174 |
} |
|
175 |
||
176 |
Object invoke() throws Exception { |
|
5185
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
177 |
AccessControlContext acc = this.acc; |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
178 |
if ((acc == null) && (System.getSecurityManager() != null)) { |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
179 |
throw new SecurityException("AccessControlContext is not set"); |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
180 |
} |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
181 |
try { |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
182 |
return AccessController.doPrivileged( |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
183 |
new PrivilegedExceptionAction<Object>() { |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
184 |
public Object run() throws Exception { |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
185 |
return invokeInternal(); |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
186 |
} |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
187 |
}, |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
188 |
acc |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
189 |
); |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
190 |
} |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
191 |
catch (PrivilegedActionException exception) { |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
192 |
throw exception.getException(); |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
193 |
} |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
194 |
} |
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
195 |
|
e29dad1b6945
6904691: Java Applet Trusted Methods Chaining Privilege Escalation Vulnerability
malenkov
parents:
3240
diff
changeset
|
196 |
private Object invokeInternal() throws Exception { |
2 | 197 |
Object target = getTarget(); |
198 |
String methodName = getMethodName(); |
|
199 |
||
200 |
if (target == null || methodName == null) { |
|
201 |
throw new NullPointerException((target == null ? "target" : |
|
202 |
"methodName") + " should not be null"); |
|
203 |
} |
|
204 |
||
205 |
Object[] arguments = getArguments(); |
|
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
206 |
if (arguments == null) { |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
207 |
arguments = emptyArray; |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
208 |
} |
2 | 209 |
// Class.forName() won't load classes outside |
210 |
// of core from a class inside core. Special |
|
211 |
// case this method. |
|
212 |
if (target == Class.class && methodName.equals("forName")) { |
|
3240
2f79c1748c93
6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents:
1851
diff
changeset
|
213 |
return ClassFinder.resolveClass((String)arguments[0], this.loader); |
2 | 214 |
} |
215 |
Class[] argClasses = new Class[arguments.length]; |
|
216 |
for(int i = 0; i < arguments.length; i++) { |
|
217 |
argClasses[i] = (arguments[i] == null) ? null : arguments[i].getClass(); |
|
218 |
} |
|
219 |
||
220 |
AccessibleObject m = null; |
|
221 |
if (target instanceof Class) { |
|
222 |
/* |
|
223 |
For class methods, simluate the effect of a meta class |
|
224 |
by taking the union of the static methods of the |
|
225 |
actual class, with the instance methods of "Class.class" |
|
226 |
and the overloaded "newInstance" methods defined by the |
|
227 |
constructors. |
|
228 |
This way "System.class", for example, will perform both |
|
229 |
the static method getProperties() and the instance method |
|
230 |
getSuperclass() defined in "Class.class". |
|
231 |
*/ |
|
232 |
if (methodName.equals("new")) { |
|
233 |
methodName = "newInstance"; |
|
234 |
} |
|
235 |
// Provide a short form for array instantiation by faking an nary-constructor. |
|
236 |
if (methodName.equals("newInstance") && ((Class)target).isArray()) { |
|
237 |
Object result = Array.newInstance(((Class)target).getComponentType(), arguments.length); |
|
238 |
for(int i = 0; i < arguments.length; i++) { |
|
239 |
Array.set(result, i, arguments[i]); |
|
240 |
} |
|
241 |
return result; |
|
242 |
} |
|
243 |
if (methodName.equals("newInstance") && arguments.length != 0) { |
|
244 |
// The Character class, as of 1.4, does not have a constructor |
|
245 |
// which takes a String. All of the other "wrapper" classes |
|
246 |
// for Java's primitive types have a String constructor so we |
|
247 |
// fake such a constructor here so that this special case can be |
|
248 |
// ignored elsewhere. |
|
249 |
if (target == Character.class && arguments.length == 1 && |
|
250 |
argClasses[0] == String.class) { |
|
251 |
return new Character(((String)arguments[0]).charAt(0)); |
|
252 |
} |
|
1851
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
253 |
try { |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
254 |
m = ConstructorFinder.findConstructor((Class)target, argClasses); |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
255 |
} |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
256 |
catch (NoSuchMethodException exception) { |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
257 |
m = null; |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
258 |
} |
2 | 259 |
} |
260 |
if (m == null && target != Class.class) { |
|
1851
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
261 |
m = getMethod((Class)target, methodName, argClasses); |
2 | 262 |
} |
263 |
if (m == null) { |
|
1851
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
264 |
m = getMethod(Class.class, methodName, argClasses); |
2 | 265 |
} |
266 |
} |
|
267 |
else { |
|
268 |
/* |
|
269 |
This special casing of arrays is not necessary, but makes files |
|
270 |
involving arrays much shorter and simplifies the archiving infrastrcure. |
|
271 |
The Array.set() method introduces an unusual idea - that of a static method |
|
272 |
changing the state of an instance. Normally statements with side |
|
273 |
effects on objects are instance methods of the objects themselves |
|
274 |
and we reinstate this rule (perhaps temporarily) by special-casing arrays. |
|
275 |
*/ |
|
276 |
if (target.getClass().isArray() && |
|
277 |
(methodName.equals("set") || methodName.equals("get"))) { |
|
278 |
int index = ((Integer)arguments[0]).intValue(); |
|
279 |
if (methodName.equals("get")) { |
|
280 |
return Array.get(target, index); |
|
281 |
} |
|
282 |
else { |
|
283 |
Array.set(target, index, arguments[1]); |
|
284 |
return null; |
|
285 |
} |
|
286 |
} |
|
1851
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
287 |
m = getMethod(target.getClass(), methodName, argClasses); |
2 | 288 |
} |
289 |
if (m != null) { |
|
290 |
try { |
|
291 |
if (m instanceof Method) { |
|
292 |
return MethodUtil.invoke((Method)m, target, arguments); |
|
293 |
} |
|
294 |
else { |
|
295 |
return ((Constructor)m).newInstance(arguments); |
|
296 |
} |
|
297 |
} |
|
298 |
catch (IllegalAccessException iae) { |
|
299 |
throw new Exception("Statement cannot invoke: " + |
|
300 |
methodName + " on " + target.getClass(), |
|
301 |
iae); |
|
302 |
} |
|
303 |
catch (InvocationTargetException ite) { |
|
304 |
Throwable te = ite.getTargetException(); |
|
305 |
if (te instanceof Exception) { |
|
306 |
throw (Exception)te; |
|
307 |
} |
|
308 |
else { |
|
309 |
throw ite; |
|
310 |
} |
|
311 |
} |
|
312 |
} |
|
313 |
throw new NoSuchMethodException(toString()); |
|
314 |
} |
|
315 |
||
316 |
String instanceName(Object instance) { |
|
317 |
if (instance == null) { |
|
318 |
return "null"; |
|
319 |
} else if (instance.getClass() == String.class) { |
|
320 |
return "\""+(String)instance + "\""; |
|
321 |
} else { |
|
322 |
// Note: there is a minor problem with using the non-caching |
|
323 |
// NameGenerator method. The return value will not have |
|
324 |
// specific information about the inner class name. For example, |
|
325 |
// In 1.4.2 an inner class would be represented as JList$1 now |
|
326 |
// would be named Class. |
|
327 |
||
328 |
return NameGenerator.unqualifiedClassName(instance.getClass()); |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* Prints the value of this statement using a Java-style syntax. |
|
334 |
*/ |
|
335 |
public String toString() { |
|
336 |
// Respect a subclass's implementation here. |
|
337 |
Object target = getTarget(); |
|
338 |
String methodName = getMethodName(); |
|
339 |
Object[] arguments = getArguments(); |
|
4845
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
340 |
if (arguments == null) { |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
341 |
arguments = emptyArray; |
64433d7632f2
4922835: DOC: Statement javadoc should indicate that target and methodName cannot be null
malenkov
parents:
3240
diff
changeset
|
342 |
} |
2 | 343 |
StringBuffer result = new StringBuffer(instanceName(target) + "." + methodName + "("); |
344 |
int n = arguments.length; |
|
345 |
for(int i = 0; i < n; i++) { |
|
346 |
result.append(instanceName(arguments[i])); |
|
347 |
if (i != n -1) { |
|
348 |
result.append(", "); |
|
349 |
} |
|
350 |
} |
|
351 |
result.append(");"); |
|
352 |
return result.toString(); |
|
353 |
} |
|
1851
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
354 |
|
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
355 |
static Method getMethod(Class<?> type, String name, Class<?>... args) { |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
356 |
try { |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
357 |
return MethodFinder.findMethod(type, name, args); |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
358 |
} |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
359 |
catch (NoSuchMethodException exception) { |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
360 |
return null; |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
361 |
} |
8203c7eb8caf
6788531: java.beans.Statement imposes excessive access control
malenkov
parents:
2
diff
changeset
|
362 |
} |
2 | 363 |
} |