author | alanb |
Fri, 02 Nov 2012 15:50:11 +0000 | |
changeset 14342 | 8435a30053c1 |
parent 11277 | e3a1c90dd439 |
child 24125 | b85eeaae56c7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
11277
diff
changeset
|
2 |
* Copyright (c) 1998, 2011, 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 com.sun.tools.jdi; |
|
27 |
||
28 |
import com.sun.jdi.*; |
|
29 |
||
30 |
import java.util.List; |
|
31 |
import java.util.Iterator; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Comparator; |
|
34 |
||
35 |
public abstract class MethodImpl extends TypeComponentImpl |
|
36 |
implements Method { |
|
37 |
private JNITypeParser signatureParser; |
|
38 |
abstract int argSlotCount() throws AbsentInformationException; |
|
39 |
||
40 |
abstract List<Location> allLineLocations(SDE.Stratum stratum, |
|
41 |
String sourceName) |
|
42 |
throws AbsentInformationException; |
|
43 |
||
44 |
abstract List<Location> locationsOfLine(SDE.Stratum stratum, |
|
45 |
String sourceName, |
|
46 |
int lineNumber) |
|
47 |
throws AbsentInformationException; |
|
48 |
||
49 |
MethodImpl(VirtualMachine vm, ReferenceTypeImpl declaringType, |
|
50 |
long ref, |
|
51 |
String name, String signature, |
|
52 |
String genericSignature, int modifiers) { |
|
53 |
super(vm, declaringType, ref, name, signature, |
|
54 |
genericSignature, modifiers); |
|
55 |
signatureParser = new JNITypeParser(signature); |
|
56 |
} |
|
57 |
||
58 |
static MethodImpl createMethodImpl(VirtualMachine vm, |
|
59 |
ReferenceTypeImpl declaringType, |
|
60 |
long ref, |
|
61 |
String name, |
|
62 |
String signature, |
|
63 |
String genericSignature, |
|
64 |
int modifiers) { |
|
65 |
if ((modifiers & |
|
66 |
(VMModifiers.NATIVE | VMModifiers.ABSTRACT)) != 0) { |
|
67 |
return new NonConcreteMethodImpl(vm, declaringType, ref, |
|
68 |
name, signature, |
|
69 |
genericSignature, |
|
70 |
modifiers); |
|
71 |
} else { |
|
72 |
return new ConcreteMethodImpl(vm, declaringType, ref, |
|
73 |
name, signature, |
|
74 |
genericSignature, |
|
75 |
modifiers); |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
public boolean equals(Object obj) { |
|
80 |
if ((obj != null) && (obj instanceof MethodImpl)) { |
|
81 |
MethodImpl other = (MethodImpl)obj; |
|
82 |
return (declaringType().equals(other.declaringType())) && |
|
83 |
(ref() == other.ref()) && |
|
84 |
super.equals(obj); |
|
85 |
} else { |
|
86 |
return false; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
public int hashCode() { |
|
91 |
return (int)ref(); |
|
92 |
} |
|
93 |
||
94 |
public final List<Location> allLineLocations() |
|
95 |
throws AbsentInformationException { |
|
96 |
return allLineLocations(vm.getDefaultStratum(), null); |
|
97 |
} |
|
98 |
||
99 |
public List<Location> allLineLocations(String stratumID, |
|
100 |
String sourceName) |
|
101 |
throws AbsentInformationException { |
|
102 |
return allLineLocations(declaringType.stratum(stratumID), |
|
103 |
sourceName); |
|
104 |
} |
|
105 |
||
106 |
public final List<Location> locationsOfLine(int lineNumber) |
|
107 |
throws AbsentInformationException { |
|
108 |
return locationsOfLine(vm.getDefaultStratum(), |
|
109 |
null, lineNumber); |
|
110 |
} |
|
111 |
||
112 |
public List<Location> locationsOfLine(String stratumID, |
|
113 |
String sourceName, |
|
114 |
int lineNumber) |
|
115 |
throws AbsentInformationException { |
|
116 |
return locationsOfLine(declaringType.stratum(stratumID), |
|
117 |
sourceName, lineNumber); |
|
118 |
} |
|
119 |
||
120 |
LineInfo codeIndexToLineInfo(SDE.Stratum stratum, |
|
121 |
long codeIndex) { |
|
122 |
if (stratum.isJava()) { |
|
123 |
return new BaseLineInfo(-1, declaringType); |
|
124 |
} else { |
|
125 |
return new StratumLineInfo(stratum.id(), -1, |
|
126 |
null, null); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* @return a text representation of the declared return type |
|
132 |
* of this method. |
|
133 |
*/ |
|
134 |
public String returnTypeName() { |
|
135 |
return signatureParser.typeName(); |
|
136 |
} |
|
137 |
||
138 |
private String returnSignature() { |
|
139 |
return signatureParser.signature(); |
|
140 |
} |
|
141 |
||
142 |
public Type returnType() throws ClassNotLoadedException { |
|
143 |
return findType(returnSignature()); |
|
144 |
} |
|
145 |
||
146 |
public Type findType(String signature) throws ClassNotLoadedException { |
|
147 |
ReferenceTypeImpl enclosing = (ReferenceTypeImpl)declaringType(); |
|
148 |
return enclosing.findType(signature); |
|
149 |
} |
|
150 |
||
151 |
public List<String> argumentTypeNames() { |
|
152 |
return signatureParser.argumentTypeNames(); |
|
153 |
} |
|
154 |
||
155 |
public List<String> argumentSignatures() { |
|
156 |
return signatureParser.argumentSignatures(); |
|
157 |
} |
|
158 |
||
159 |
Type argumentType(int index) throws ClassNotLoadedException { |
|
160 |
ReferenceTypeImpl enclosing = (ReferenceTypeImpl)declaringType(); |
|
51 | 161 |
String signature = argumentSignatures().get(index); |
2 | 162 |
return enclosing.findType(signature); |
163 |
} |
|
164 |
||
165 |
public List<Type> argumentTypes() throws ClassNotLoadedException { |
|
166 |
int size = argumentSignatures().size(); |
|
167 |
ArrayList<Type> types = new ArrayList<Type>(size); |
|
168 |
for (int i = 0; i < size; i++) { |
|
169 |
Type type = argumentType(i); |
|
170 |
types.add(type); |
|
171 |
} |
|
172 |
||
173 |
return types; |
|
174 |
} |
|
175 |
||
176 |
public int compareTo(Method method) { |
|
177 |
ReferenceTypeImpl declaringType = (ReferenceTypeImpl)declaringType(); |
|
178 |
int rc = declaringType.compareTo(method.declaringType()); |
|
179 |
if (rc == 0) { |
|
180 |
rc = declaringType.indexOf(this) - |
|
181 |
declaringType.indexOf(method); |
|
182 |
} |
|
183 |
return rc; |
|
184 |
} |
|
185 |
||
186 |
public boolean isAbstract() { |
|
187 |
return isModifierSet(VMModifiers.ABSTRACT); |
|
188 |
} |
|
189 |
||
190 |
public boolean isSynchronized() { |
|
191 |
return isModifierSet(VMModifiers.SYNCHRONIZED); |
|
192 |
} |
|
193 |
||
194 |
public boolean isNative() { |
|
195 |
return isModifierSet(VMModifiers.NATIVE); |
|
196 |
} |
|
197 |
||
198 |
public boolean isVarArgs() { |
|
199 |
return isModifierSet(VMModifiers.VARARGS); |
|
200 |
} |
|
201 |
||
202 |
public boolean isBridge() { |
|
203 |
return isModifierSet(VMModifiers.BRIDGE); |
|
204 |
} |
|
205 |
||
206 |
public boolean isConstructor() { |
|
207 |
return name().equals("<init>"); |
|
208 |
} |
|
209 |
||
210 |
public boolean isStaticInitializer() { |
|
211 |
return name().equals("<clinit>"); |
|
212 |
} |
|
213 |
||
214 |
public boolean isObsolete() { |
|
215 |
try { |
|
216 |
return JDWP.Method.IsObsolete.process(vm, |
|
217 |
declaringType, ref).isObsolete; |
|
218 |
} catch (JDWPException exc) { |
|
219 |
throw exc.toJDIException(); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
||
224 |
/* |
|
225 |
* A container class for the return value to allow |
|
226 |
* proper type-checking. |
|
227 |
*/ |
|
228 |
class ReturnContainer implements ValueContainer { |
|
229 |
ReturnContainer() { |
|
230 |
} |
|
231 |
public Type type() throws ClassNotLoadedException { |
|
232 |
return returnType(); |
|
233 |
} |
|
234 |
public String typeName(){ |
|
235 |
return returnTypeName(); |
|
236 |
} |
|
237 |
public String signature() { |
|
238 |
return returnSignature(); //type().signature(); |
|
239 |
} |
|
240 |
public Type findType(String signature) throws ClassNotLoadedException { |
|
241 |
return MethodImpl.this.findType(signature); |
|
242 |
} |
|
243 |
} |
|
244 |
ReturnContainer retValContainer = null; |
|
245 |
ReturnContainer getReturnValueContainer() { |
|
246 |
if (retValContainer == null) { |
|
247 |
retValContainer = new ReturnContainer(); |
|
248 |
} |
|
249 |
return retValContainer; |
|
250 |
} |
|
251 |
||
252 |
/* |
|
253 |
* A container class for the argument to allow |
|
254 |
* proper type-checking. |
|
255 |
*/ |
|
256 |
class ArgumentContainer implements ValueContainer { |
|
257 |
int index; |
|
258 |
||
259 |
ArgumentContainer(int index) { |
|
260 |
this.index = index; |
|
261 |
} |
|
262 |
public Type type() throws ClassNotLoadedException { |
|
263 |
return argumentType(index); |
|
264 |
} |
|
265 |
public String typeName(){ |
|
51 | 266 |
return argumentTypeNames().get(index); |
2 | 267 |
} |
268 |
public String signature() { |
|
51 | 269 |
return argumentSignatures().get(index); |
2 | 270 |
} |
271 |
public Type findType(String signature) throws ClassNotLoadedException { |
|
272 |
return MethodImpl.this.findType(signature); |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
/* |
|
277 |
* This is a var args method. Thus, its last param is an |
|
278 |
* array. If the method has n params, then: |
|
279 |
* 1. If there are n args and the last is the same type as the type of |
|
280 |
* the last param, do nothing. IE, a String[] |
|
281 |
* can be passed to a String... |
|
282 |
* 2. If there are >= n arguments and for each arg whose number is >= n, |
|
283 |
* the arg type is 'compatible' with the component type of |
|
284 |
* the last param, then do |
|
285 |
* - create an array of the type of the last param |
|
286 |
* - put the n, ... args into this array. |
|
287 |
* We might have to do conversions here. |
|
288 |
* - put this array into arguments(n) |
|
289 |
* - delete arguments(n+1), ... |
|
290 |
* NOTE that this might modify the input list. |
|
291 |
*/ |
|
292 |
void handleVarArgs(List<Value> arguments) |
|
293 |
throws ClassNotLoadedException, InvalidTypeException { |
|
294 |
List<Type> paramTypes = this.argumentTypes(); |
|
295 |
ArrayType lastParamType = (ArrayType)paramTypes.get(paramTypes.size() - 1); |
|
296 |
Type componentType = lastParamType.componentType(); |
|
297 |
int argCount = arguments.size(); |
|
298 |
int paramCount = paramTypes.size(); |
|
299 |
if (argCount < paramCount - 1) { |
|
300 |
// Error; will be caught later. |
|
301 |
return; |
|
302 |
} |
|
303 |
if (argCount == paramCount - 1) { |
|
304 |
// It is ok to pass 0 args to the var arg. |
|
305 |
// We have to gen a 0 length array. |
|
306 |
ArrayReference argArray = lastParamType.newInstance(0); |
|
307 |
arguments.add(argArray); |
|
308 |
return; |
|
309 |
} |
|
51 | 310 |
Value nthArgValue = arguments.get(paramCount - 1); |
2 | 311 |
if (nthArgValue == null) { |
312 |
return; |
|
313 |
} |
|
314 |
Type nthArgType = nthArgValue.type(); |
|
315 |
if (nthArgType instanceof ArrayTypeImpl) { |
|
316 |
if (argCount == paramCount && |
|
317 |
((ArrayTypeImpl)nthArgType).isAssignableTo(lastParamType)) { |
|
318 |
/* |
|
319 |
* This is case 1. A compatible array is being passed to the |
|
320 |
* var args array param. We don't have to do anything. |
|
321 |
*/ |
|
322 |
return; |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
/* |
|
327 |
* Case 2. We have to verify that the n, n+1, ... args are compatible |
|
328 |
* with componentType, and do conversions if necessary and create |
|
329 |
* an array of componentType to hold these possibly converted values. |
|
330 |
*/ |
|
331 |
int count = argCount - paramCount + 1; |
|
332 |
ArrayReference argArray = lastParamType.newInstance(count); |
|
333 |
||
334 |
/* |
|
335 |
* This will copy arguments(paramCount - 1) ... to argArray(0) ... |
|
336 |
* doing whatever conversions are needed! It will throw an |
|
337 |
* exception if an incompatible arg is encountered |
|
338 |
*/ |
|
339 |
argArray.setValues(0, arguments, paramCount - 1, count); |
|
340 |
arguments.set(paramCount - 1, argArray); |
|
341 |
||
342 |
/* |
|
343 |
* Remove the excess args |
|
344 |
*/ |
|
345 |
for (int ii = paramCount; ii < argCount; ii++) { |
|
346 |
arguments.remove(paramCount); |
|
347 |
} |
|
348 |
return; |
|
349 |
} |
|
350 |
||
351 |
/* |
|
352 |
* The output list will be different than the input list. |
|
353 |
*/ |
|
354 |
List<Value> validateAndPrepareArgumentsForInvoke(List<? extends Value> origArguments) |
|
355 |
throws ClassNotLoadedException, InvalidTypeException { |
|
356 |
||
357 |
List<Value> arguments = new ArrayList<Value>(origArguments); |
|
358 |
if (isVarArgs()) { |
|
359 |
handleVarArgs(arguments); |
|
360 |
} |
|
361 |
||
362 |
int argSize = arguments.size(); |
|
363 |
||
364 |
JNITypeParser parser = new JNITypeParser(signature()); |
|
11277 | 365 |
List<String> signatures = parser.argumentSignatures(); |
2 | 366 |
|
367 |
if (signatures.size() != argSize) { |
|
368 |
throw new IllegalArgumentException("Invalid argument count: expected " + |
|
369 |
signatures.size() + ", received " + |
|
370 |
arguments.size()); |
|
371 |
} |
|
372 |
||
373 |
for (int i = 0; i < argSize; i++) { |
|
51 | 374 |
Value value = arguments.get(i); |
2 | 375 |
value = ValueImpl.prepareForAssignment(value, |
376 |
new ArgumentContainer(i)); |
|
377 |
arguments.set(i, value); |
|
378 |
} |
|
379 |
return arguments; |
|
380 |
} |
|
381 |
||
382 |
public String toString() { |
|
383 |
StringBuffer sb = new StringBuffer(); |
|
384 |
sb.append(declaringType().name()); |
|
385 |
sb.append("."); |
|
386 |
sb.append(name()); |
|
387 |
sb.append("("); |
|
388 |
boolean first = true; |
|
51 | 389 |
for (String name : argumentTypeNames()) { |
2 | 390 |
if (!first) { |
391 |
sb.append(", "); |
|
392 |
} |
|
51 | 393 |
sb.append(name); |
2 | 394 |
first = false; |
395 |
} |
|
396 |
sb.append(")"); |
|
397 |
return sb.toString(); |
|
398 |
} |
|
399 |
} |