author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45714 | jdk/src/jdk.jdi/share/classes/com/sun/tools/jdi/ClassTypeImpl.java@1820d351198d |
child 54907 | 6bcda7ed24ef |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45714 | 2 |
* Copyright (c) 1998, 2017, 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 |
||
45714 | 28 |
import java.util.ArrayList; |
29 |
import java.util.List; |
|
2 | 30 |
|
45714 | 31 |
import com.sun.jdi.ClassNotLoadedException; |
32 |
import com.sun.jdi.ClassType; |
|
33 |
import com.sun.jdi.Field; |
|
34 |
import com.sun.jdi.IncompatibleThreadStateException; |
|
35 |
import com.sun.jdi.InterfaceType; |
|
36 |
import com.sun.jdi.InvalidTypeException; |
|
37 |
import com.sun.jdi.InvocationException; |
|
38 |
import com.sun.jdi.Method; |
|
39 |
import com.sun.jdi.ObjectReference; |
|
40 |
import com.sun.jdi.ReferenceType; |
|
41 |
import com.sun.jdi.ThreadReference; |
|
42 |
import com.sun.jdi.Value; |
|
43 |
import com.sun.jdi.VirtualMachine; |
|
2 | 44 |
|
24195
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
45 |
final public class ClassTypeImpl extends InvokableTypeImpl |
45714 | 46 |
implements ClassType |
2 | 47 |
{ |
24195
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
48 |
private static class IResult implements InvocationResult { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
49 |
final private JDWP.ClassType.InvokeMethod rslt; |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
50 |
|
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
51 |
public IResult(JDWP.ClassType.InvokeMethod rslt) { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
52 |
this.rslt = rslt; |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
53 |
} |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
54 |
|
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
55 |
@Override |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
56 |
public ObjectReferenceImpl getException() { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
57 |
return rslt.exception; |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
58 |
} |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
59 |
|
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
60 |
@Override |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
61 |
public ValueImpl getResult() { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
62 |
return rslt.returnValue; |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
63 |
} |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
64 |
} |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
65 |
|
2 | 66 |
private boolean cachedSuperclass = false; |
67 |
private ClassType superclass = null; |
|
68 |
private List<InterfaceType> interfaces = null; |
|
69 |
||
45714 | 70 |
protected ClassTypeImpl(VirtualMachine aVm, long aRef) { |
2 | 71 |
super(aVm, aRef); |
72 |
} |
|
73 |
||
74 |
public ClassType superclass() { |
|
45714 | 75 |
if (!cachedSuperclass) { |
2 | 76 |
ClassTypeImpl sup = null; |
77 |
try { |
|
78 |
sup = JDWP.ClassType.Superclass. |
|
79 |
process(vm, this).superclass; |
|
80 |
} catch (JDWPException exc) { |
|
81 |
throw exc.toJDIException(); |
|
82 |
} |
|
83 |
||
84 |
/* |
|
85 |
* If there is a superclass, cache its |
|
86 |
* ClassType here. Otherwise, |
|
87 |
* leave the cache reference null. |
|
88 |
*/ |
|
89 |
if (sup != null) { |
|
90 |
superclass = sup; |
|
91 |
} |
|
92 |
cachedSuperclass = true; |
|
93 |
} |
|
94 |
||
95 |
return superclass; |
|
96 |
} |
|
97 |
||
24195
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
98 |
@Override |
2 | 99 |
public List<InterfaceType> interfaces() { |
100 |
if (interfaces == null) { |
|
101 |
interfaces = getInterfaces(); |
|
102 |
} |
|
103 |
return interfaces; |
|
104 |
} |
|
105 |
||
24195
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
106 |
@Override |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
107 |
public List<InterfaceType> allInterfaces() { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
108 |
return getAllInterfaces(); |
2 | 109 |
} |
110 |
||
111 |
public List<ClassType> subclasses() { |
|
45714 | 112 |
List<ClassType> subs = new ArrayList<>(); |
51 | 113 |
for (ReferenceType refType : vm.allClasses()) { |
2 | 114 |
if (refType instanceof ClassType) { |
115 |
ClassType clazz = (ClassType)refType; |
|
116 |
ClassType superclass = clazz.superclass(); |
|
117 |
if ((superclass != null) && superclass.equals(this)) { |
|
118 |
subs.add((ClassType)refType); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
return subs; |
|
124 |
} |
|
125 |
||
126 |
public boolean isEnum() { |
|
127 |
ClassType superclass = superclass(); |
|
128 |
if (superclass != null && |
|
129 |
superclass.name().equals("java.lang.Enum")) { |
|
130 |
return true; |
|
131 |
} |
|
132 |
return false; |
|
133 |
} |
|
134 |
||
135 |
public void setValue(Field field, Value value) |
|
136 |
throws InvalidTypeException, ClassNotLoadedException { |
|
137 |
||
138 |
validateMirror(field); |
|
139 |
validateMirrorOrNull(value); |
|
140 |
validateFieldSet(field); |
|
141 |
||
142 |
// More validation specific to setting from a ClassType |
|
143 |
if(!field.isStatic()) { |
|
144 |
throw new IllegalArgumentException( |
|
145 |
"Must set non-static field through an instance"); |
|
146 |
} |
|
147 |
||
148 |
try { |
|
149 |
JDWP.ClassType.SetValues.FieldValue[] values = |
|
150 |
new JDWP.ClassType.SetValues.FieldValue[1]; |
|
151 |
values[0] = new JDWP.ClassType.SetValues.FieldValue( |
|
152 |
((FieldImpl)field).ref(), |
|
153 |
// validate and convert if necessary |
|
154 |
ValueImpl.prepareForAssignment(value, (FieldImpl)field)); |
|
155 |
||
156 |
try { |
|
157 |
JDWP.ClassType.SetValues.process(vm, this, values); |
|
158 |
} catch (JDWPException exc) { |
|
159 |
throw exc.toJDIException(); |
|
160 |
} |
|
161 |
} catch (ClassNotLoadedException e) { |
|
162 |
/* |
|
163 |
* Since we got this exception, |
|
164 |
* the field type must be a reference type. The value |
|
165 |
* we're trying to set is null, but if the field's |
|
166 |
* class has not yet been loaded through the enclosing |
|
167 |
* class loader, then setting to null is essentially a |
|
168 |
* no-op, and we should allow it without an exception. |
|
169 |
*/ |
|
170 |
if (value != null) { |
|
171 |
throw e; |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
PacketStream sendNewInstanceCommand(final ThreadReferenceImpl thread, |
|
45714 | 177 |
final MethodImpl method, |
178 |
final ValueImpl[] args, |
|
179 |
final int options) { |
|
2 | 180 |
CommandSender sender = |
181 |
new CommandSender() { |
|
182 |
public PacketStream send() { |
|
183 |
return JDWP.ClassType.NewInstance.enqueueCommand( |
|
184 |
vm, ClassTypeImpl.this, thread, |
|
185 |
method.ref(), args, options); |
|
186 |
} |
|
187 |
}; |
|
188 |
||
189 |
PacketStream stream; |
|
190 |
if ((options & INVOKE_SINGLE_THREADED) != 0) { |
|
191 |
stream = thread.sendResumingCommand(sender); |
|
192 |
} else { |
|
193 |
stream = vm.sendResumingCommand(sender); |
|
194 |
} |
|
195 |
return stream; |
|
196 |
} |
|
197 |
||
198 |
public ObjectReference newInstance(ThreadReference threadIntf, |
|
199 |
Method methodIntf, |
|
200 |
List<? extends Value> origArguments, |
|
201 |
int options) |
|
202 |
throws InvalidTypeException, |
|
203 |
ClassNotLoadedException, |
|
204 |
IncompatibleThreadStateException, |
|
205 |
InvocationException { |
|
206 |
validateMirror(threadIntf); |
|
207 |
validateMirror(methodIntf); |
|
208 |
validateMirrorsOrNulls(origArguments); |
|
209 |
||
210 |
MethodImpl method = (MethodImpl)methodIntf; |
|
211 |
ThreadReferenceImpl thread = (ThreadReferenceImpl)threadIntf; |
|
212 |
||
213 |
validateConstructorInvocation(method); |
|
214 |
||
215 |
List<Value> arguments = method.validateAndPrepareArgumentsForInvoke( |
|
216 |
origArguments); |
|
51 | 217 |
ValueImpl[] args = arguments.toArray(new ValueImpl[0]); |
2 | 218 |
JDWP.ClassType.NewInstance ret = null; |
219 |
try { |
|
220 |
PacketStream stream = |
|
221 |
sendNewInstanceCommand(thread, method, args, options); |
|
222 |
ret = JDWP.ClassType.NewInstance.waitForReply(vm, stream); |
|
223 |
} catch (JDWPException exc) { |
|
224 |
if (exc.errorCode() == JDWP.Error.INVALID_THREAD) { |
|
225 |
throw new IncompatibleThreadStateException(); |
|
226 |
} else { |
|
227 |
throw exc.toJDIException(); |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
/* |
|
232 |
* There is an implict VM-wide suspend at the conclusion |
|
233 |
* of a normal (non-single-threaded) method invoke |
|
234 |
*/ |
|
235 |
if ((options & INVOKE_SINGLE_THREADED) == 0) { |
|
236 |
vm.notifySuspend(); |
|
237 |
} |
|
238 |
||
239 |
if (ret.exception != null) { |
|
240 |
throw new InvocationException(ret.exception); |
|
241 |
} else { |
|
242 |
return ret.newObject; |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
public Method concreteMethodByName(String name, String signature) { |
|
45714 | 247 |
Method method = null; |
248 |
for (Method candidate : visibleMethods()) { |
|
249 |
if (candidate.name().equals(name) && |
|
250 |
candidate.signature().equals(signature) && |
|
251 |
!candidate.isAbstract()) { |
|
2 | 252 |
|
45714 | 253 |
method = candidate; |
254 |
break; |
|
255 |
} |
|
256 |
} |
|
257 |
return method; |
|
258 |
} |
|
2 | 259 |
|
260 |
void validateConstructorInvocation(Method method) |
|
261 |
throws InvalidTypeException, |
|
262 |
InvocationException { |
|
263 |
/* |
|
264 |
* Method must be in this class. |
|
265 |
*/ |
|
266 |
ReferenceTypeImpl declType = (ReferenceTypeImpl)method.declaringType(); |
|
267 |
if (!declType.equals(this)) { |
|
268 |
throw new IllegalArgumentException("Invalid constructor"); |
|
269 |
} |
|
270 |
||
271 |
/* |
|
272 |
* Method must be a constructor |
|
273 |
*/ |
|
274 |
if (!method.isConstructor()) { |
|
275 |
throw new IllegalArgumentException("Cannot create instance with non-constructor"); |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
public String toString() { |
|
280 |
return "class " + name() + " (" + loaderString() + ")"; |
|
281 |
} |
|
24195
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
282 |
|
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
283 |
@Override |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
284 |
CommandSender getInvokeMethodSender(ThreadReferenceImpl thread, |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
285 |
MethodImpl method, |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
286 |
ValueImpl[] args, |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
287 |
int options) { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
288 |
return () -> |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
289 |
JDWP.ClassType.InvokeMethod.enqueueCommand(vm, |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
290 |
ClassTypeImpl.this, |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
291 |
thread, |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
292 |
method.ref(), |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
293 |
args, |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
294 |
options); |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
295 |
} |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
296 |
|
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
297 |
@Override |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
298 |
InvocationResult waitForReply(PacketStream stream) throws JDWPException { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
299 |
return new IResult(JDWP.ClassType.InvokeMethod.waitForReply(vm, stream)); |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
300 |
} |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
301 |
|
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
302 |
@Override |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
303 |
boolean canInvoke(Method method) { |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
304 |
// Method must be in this class or a superclass. |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
305 |
return ((ReferenceTypeImpl)method.declaringType()).isAssignableFrom(this); |
705325a63a58
8042123: Support default and static interface methods in JDI, JDWP and JDB
jbachorik
parents:
24127
diff
changeset
|
306 |
} |
2 | 307 |
} |