This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
ReferenceTypeImpl.java
1174 lines (1042 loc) · 39.2 KB
/
ReferenceTypeImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.jdi;
import com.sun.jdi.*;
import java.util.*;
import java.lang.ref.SoftReference;
public abstract class ReferenceTypeImpl extends TypeImpl
implements ReferenceType {
protected long ref;
private String signature = null;
private String genericSignature = null;
private boolean genericSignatureGotten = false;
private String baseSourceName = null;
private String baseSourceDir = null;
private String baseSourcePath = null;
protected int modifiers = -1;
private SoftReference<List<Field>> fieldsRef = null;
private SoftReference<List<Method>> methodsRef = null;
private SoftReference<SDE> sdeRef = null;
private boolean isClassLoaderCached = false;
private ClassLoaderReference classLoader = null;
private ClassObjectReference classObject = null;
private int status = 0;
private boolean isPrepared = false;
private boolean versionNumberGotten = false;
private int majorVersion;
private int minorVersion;
private boolean constantPoolInfoGotten = false;
private int constanPoolCount;
private byte[] constantPoolBytes;
private SoftReference<byte[]> constantPoolBytesRef = null;
/* to mark a SourceFile request that returned a genuine JDWP.Error.ABSENT_INFORMATION */
private static final String ABSENT_BASE_SOURCE_NAME = "**ABSENT_BASE_SOURCE_NAME**";
/* to mark when no info available */
static final SDE NO_SDE_INFO_MARK = new SDE();
// bits set when initialization was attempted (succeeded or failed)
private static final int INITIALIZED_OR_FAILED =
JDWP.ClassStatus.INITIALIZED | JDWP.ClassStatus.ERROR;
protected ReferenceTypeImpl(VirtualMachine aVm, long aRef) {
super(aVm);
ref = aRef;
genericSignatureGotten = false;
}
void noticeRedefineClass() {
//Invalidate information previously fetched and cached.
//These will be refreshed later on demand.
baseSourceName = null;
baseSourcePath = null;
modifiers = -1;
fieldsRef = null;
methodsRef = null;
sdeRef = null;
versionNumberGotten = false;
constantPoolInfoGotten = false;
}
Method getMethodMirror(long ref) {
if (ref == 0) {
// obsolete method
return new ObsoleteMethodImpl(vm, this);
}
// Fetch all methods for the class, check performance impact
// Needs no synchronization now, since methods() returns
// unmodifiable local data
Iterator<Method> it = methods().iterator();
while (it.hasNext()) {
MethodImpl method = (MethodImpl)it.next();
if (method.ref() == ref) {
return method;
}
}
throw new IllegalArgumentException("Invalid method id: " + ref);
}
Field getFieldMirror(long ref) {
// Fetch all fields for the class, check performance impact
// Needs no synchronization now, since fields() returns
// unmodifiable local data
Iterator<Field>it = fields().iterator();
while (it.hasNext()) {
FieldImpl field = (FieldImpl)it.next();
if (field.ref() == ref) {
return field;
}
}
throw new IllegalArgumentException("Invalid field id: " + ref);
}
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof ReferenceTypeImpl)) {
ReferenceTypeImpl other = (ReferenceTypeImpl)obj;
return (ref() == other.ref()) &&
(vm.equals(other.virtualMachine()));
} else {
return false;
}
}
public int hashCode() {
return(int)ref();
}
public int compareTo(ReferenceType object) {
/*
* Note that it is critical that compareTo() == 0
* implies that equals() == true. Otherwise, TreeSet
* will collapse classes.
*
* (Classes of the same name loaded by different class loaders
* or in different VMs must not return 0).
*/
ReferenceTypeImpl other = (ReferenceTypeImpl)object;
int comp = name().compareTo(other.name());
if (comp == 0) {
long rf1 = ref();
long rf2 = other.ref();
// optimize for typical case: refs equal and VMs equal
if (rf1 == rf2) {
// sequenceNumbers are always positive
comp = vm.sequenceNumber -
((VirtualMachineImpl)(other.virtualMachine())).sequenceNumber;
} else {
comp = (rf1 < rf2)? -1 : 1;
}
}
return comp;
}
public String signature() {
if (signature == null) {
// Does not need synchronization, since worst-case
// static info is fetched twice
if (vm.canGet1_5LanguageFeatures()) {
/*
* we might as well get both the signature and the
* generic signature.
*/
genericSignature();
} else {
try {
signature = JDWP.ReferenceType.Signature.
process(vm, this).signature;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
}
}
return signature;
}
public String genericSignature() {
// This gets both the signature and the generic signature
if (vm.canGet1_5LanguageFeatures() && !genericSignatureGotten) {
// Does not need synchronization, since worst-case
// static info is fetched twice
JDWP.ReferenceType.SignatureWithGeneric result;
try {
result = JDWP.ReferenceType.SignatureWithGeneric.
process(vm, this);
} catch (JDWPException exc) {
throw exc.toJDIException();
}
signature = result.signature;
setGenericSignature(result.genericSignature);
}
return genericSignature;
}
public ClassLoaderReference classLoader() {
if (!isClassLoaderCached) {
// Does not need synchronization, since worst-case
// static info is fetched twice
try {
classLoader = (ClassLoaderReference)
JDWP.ReferenceType.ClassLoader.
process(vm, this).classLoader;
isClassLoaderCached = true;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
}
return classLoader;
}
public boolean isPublic() {
if (modifiers == -1)
getModifiers();
return((modifiers & VMModifiers.PUBLIC) > 0);
}
public boolean isProtected() {
if (modifiers == -1)
getModifiers();
return((modifiers & VMModifiers.PROTECTED) > 0);
}
public boolean isPrivate() {
if (modifiers == -1)
getModifiers();
return((modifiers & VMModifiers.PRIVATE) > 0);
}
public boolean isPackagePrivate() {
return !isPublic() && !isPrivate() && !isProtected();
}
public boolean isAbstract() {
if (modifiers == -1)
getModifiers();
return((modifiers & VMModifiers.ABSTRACT) > 0);
}
public boolean isFinal() {
if (modifiers == -1)
getModifiers();
return((modifiers & VMModifiers.FINAL) > 0);
}
public boolean isStatic() {
if (modifiers == -1)
getModifiers();
return((modifiers & VMModifiers.STATIC) > 0);
}
public boolean isPrepared() {
// This ref type may have been prepared before we were getting
// events, so get it once. After that,
// this status flag is updated through the ClassPrepareEvent,
// there is no need for the expense of a JDWP query.
if (status == 0) {
updateStatus();
}
return isPrepared;
}
public boolean isVerified() {
// Once true, it never resets, so we don't need to update
if ((status & JDWP.ClassStatus.VERIFIED) == 0) {
updateStatus();
}
return (status & JDWP.ClassStatus.VERIFIED) != 0;
}
public boolean isInitialized() {
// Once initialization succeeds or fails, it never resets,
// so we don't need to update
if ((status & INITIALIZED_OR_FAILED) == 0) {
updateStatus();
}
return (status & JDWP.ClassStatus.INITIALIZED) != 0;
}
public boolean failedToInitialize() {
// Once initialization succeeds or fails, it never resets,
// so we don't need to update
if ((status & INITIALIZED_OR_FAILED) == 0) {
updateStatus();
}
return (status & JDWP.ClassStatus.ERROR) != 0;
}
public List<Field> fields() {
List<Field> fields = (fieldsRef == null) ? null : fieldsRef.get();
if (fields == null) {
if (vm.canGet1_5LanguageFeatures()) {
JDWP.ReferenceType.FieldsWithGeneric.FieldInfo[] jdwpFields;
try {
jdwpFields = JDWP.ReferenceType.FieldsWithGeneric.process(vm, this).declared;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
fields = new ArrayList<Field>(jdwpFields.length);
for (int i=0; i<jdwpFields.length; i++) {
JDWP.ReferenceType.FieldsWithGeneric.FieldInfo fi
= jdwpFields[i];
Field field = new FieldImpl(vm, this, fi.fieldID,
fi.name, fi.signature,
fi.genericSignature,
fi.modBits);
fields.add(field);
}
} else {
JDWP.ReferenceType.Fields.FieldInfo[] jdwpFields;
try {
jdwpFields = JDWP.ReferenceType.Fields.
process(vm, this).declared;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
fields = new ArrayList<Field>(jdwpFields.length);
for (int i=0; i<jdwpFields.length; i++) {
JDWP.ReferenceType.Fields.FieldInfo fi = jdwpFields[i];
Field field = new FieldImpl(vm, this, fi.fieldID,
fi.name, fi.signature,
null,
fi.modBits);
fields.add(field);
}
}
fields = Collections.unmodifiableList(fields);
fieldsRef = new SoftReference<List<Field>>(fields);
}
return fields;
}
abstract List<? extends ReferenceType> inheritedTypes();
void addVisibleFields(List<Field> visibleList, Map<String, Field> visibleTable, List<String> ambiguousNames) {
for (Field field : visibleFields()) {
String name = field.name();
if (!ambiguousNames.contains(name)) {
Field duplicate = visibleTable.get(name);
if (duplicate == null) {
visibleList.add(field);
visibleTable.put(name, field);
} else if (!field.equals(duplicate)) {
ambiguousNames.add(name);
visibleTable.remove(name);
visibleList.remove(duplicate);
} else {
// identical field from two branches; do nothing
}
}
}
}
public List<Field> visibleFields() {
/*
* Maintain two different collections of visible fields. The
* list maintains a reasonable order for return. The
* hash map provides an efficient way to lookup visible fields
* by name, important for finding hidden or ambiguous fields.
*/
List<Field> visibleList = new ArrayList<Field>();
Map<String, Field> visibleTable = new HashMap<String, Field>();
/* Track fields removed from above collection due to ambiguity */
List<String> ambiguousNames = new ArrayList<String>();
/* Add inherited, visible fields */
List<? extends ReferenceType> types = inheritedTypes();
Iterator<? extends ReferenceType> iter = types.iterator();
while (iter.hasNext()) {
/*
* TO DO: Be defensive and check for cyclic interface inheritance
*/
ReferenceTypeImpl type = (ReferenceTypeImpl)iter.next();
type.addVisibleFields(visibleList, visibleTable, ambiguousNames);
}
/*
* Insert fields from this type, removing any inherited fields they
* hide.
*/
List<Field> retList = new ArrayList<Field>(fields());
for (Field field : retList) {
Field hidden = visibleTable.get(field.name());
if (hidden != null) {
visibleList.remove(hidden);
}
}
retList.addAll(visibleList);
return retList;
}
void addAllFields(List<Field> fieldList, Set<ReferenceType> typeSet) {
/* Continue the recursion only if this type is new */
if (!typeSet.contains(this)) {
typeSet.add((ReferenceType)this);
/* Add local fields */
fieldList.addAll(fields());
/* Add inherited fields */
List<? extends ReferenceType> types = inheritedTypes();
Iterator<? extends ReferenceType> iter = types.iterator();
while (iter.hasNext()) {
ReferenceTypeImpl type = (ReferenceTypeImpl)iter.next();
type.addAllFields(fieldList, typeSet);
}
}
}
public List<Field> allFields() {
List<Field> fieldList = new ArrayList<Field>();
Set<ReferenceType> typeSet = new HashSet<ReferenceType>();
addAllFields(fieldList, typeSet);
return fieldList;
}
public Field fieldByName(String fieldName) {
List<Field> searchList = visibleFields();
for (int i=0; i<searchList.size(); i++) {
Field f = searchList.get(i);
if (f.name().equals(fieldName)) {
return f;
}
}
//throw new NoSuchFieldException("Field '" + fieldName + "' not found in " + name());
return null;
}
public List<Method> methods() {
List<Method> methods = (methodsRef == null) ? null : methodsRef.get();
if (methods == null) {
if (!vm.canGet1_5LanguageFeatures()) {
methods = methods1_4();
} else {
JDWP.ReferenceType.MethodsWithGeneric.MethodInfo[] declared;
try {
declared = JDWP.ReferenceType.MethodsWithGeneric.
process(vm, this).declared;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
methods = new ArrayList<Method>(declared.length);
for (int i=0; i<declared.length; i++) {
JDWP.ReferenceType.MethodsWithGeneric.MethodInfo
mi = declared[i];
Method method = MethodImpl.createMethodImpl(vm, this,
mi.methodID,
mi.name, mi.signature,
mi.genericSignature,
mi.modBits);
methods.add(method);
}
}
methods = Collections.unmodifiableList(methods);
methodsRef = new SoftReference<List<Method>>(methods);
}
return methods;
}
private List<Method> methods1_4() {
List<Method> methods;
JDWP.ReferenceType.Methods.MethodInfo[] declared;
try {
declared = JDWP.ReferenceType.Methods.
process(vm, this).declared;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
methods = new ArrayList<Method>(declared.length);
for (int i=0; i<declared.length; i++) {
JDWP.ReferenceType.Methods.MethodInfo mi = declared[i];
Method method = MethodImpl.createMethodImpl(vm, this,
mi.methodID,
mi.name, mi.signature,
null,
mi.modBits);
methods.add(method);
}
return methods;
}
/*
* Utility method used by subclasses to build lists of visible
* methods.
*/
void addToMethodMap(Map<String, Method> methodMap, List<Method> methodList) {
for (Method method : methodList)
methodMap.put(method.name().concat(method.signature()), method);
}
abstract void addVisibleMethods(Map<String, Method> methodMap, Set<InterfaceType> seenInterfaces);
public List<Method> visibleMethods() {
/*
* Build a collection of all visible methods. The hash
* map allows us to do this efficiently by keying on the
* concatenation of name and signature.
*/
Map<String, Method> map = new HashMap<String, Method>();
addVisibleMethods(map, new HashSet<InterfaceType>());
/*
* ... but the hash map destroys order. Methods should be
* returned in a sensible order, as they are in allMethods().
* So, start over with allMethods() and use the hash map
* to filter that ordered collection.
*/
List<Method> list = allMethods();
list.retainAll(map.values());
return list;
}
abstract public List<Method> allMethods();
public List<Method> methodsByName(String name) {
List<Method> methods = visibleMethods();
ArrayList<Method> retList = new ArrayList<Method>(methods.size());
for (Method candidate : methods) {
if (candidate.name().equals(name)) {
retList.add(candidate);
}
}
retList.trimToSize();
return retList;
}
public List<Method> methodsByName(String name, String signature) {
List<Method> methods = visibleMethods();
ArrayList<Method> retList = new ArrayList<Method>(methods.size());
for (Method candidate : methods) {
if (candidate.name().equals(name) &&
candidate.signature().equals(signature)) {
retList.add(candidate);
}
}
retList.trimToSize();
return retList;
}
List<InterfaceType> getInterfaces() {
InterfaceTypeImpl[] intfs;
try {
intfs = JDWP.ReferenceType.Interfaces.
process(vm, this).interfaces;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
return Arrays.asList((InterfaceType[])intfs);
}
public List<ReferenceType> nestedTypes() {
List<ReferenceType> all = vm.allClasses();
List<ReferenceType> nested = new ArrayList<ReferenceType>();
String outername = name();
int outerlen = outername.length();
Iterator<ReferenceType> iter = all.iterator();
while (iter.hasNext()) {
ReferenceType refType = iter.next();
String name = refType.name();
int len = name.length();
/* The separator is historically '$' but could also be '#' */
if ( len > outerlen && name.startsWith(outername) ) {
char c = name.charAt(outerlen);
if ( c =='$' || c== '#' ) {
nested.add(refType);
}
}
}
return nested;
}
public Value getValue(Field sig) {
List<Field> list = new ArrayList<Field>(1);
list.add(sig);
Map<Field, Value> map = getValues(list);
return map.get(sig);
}
void validateFieldAccess(Field field) {
/*
* Field must be in this object's class, a superclass, or
* implemented interface
*/
ReferenceTypeImpl declType = (ReferenceTypeImpl)field.declaringType();
if (!declType.isAssignableFrom(this)) {
throw new IllegalArgumentException("Invalid field");
}
}
void validateFieldSet(Field field) {
validateFieldAccess(field);
if (field.isFinal()) {
throw new IllegalArgumentException("Cannot set value of final field");
}
}
/**
* Returns a map of field values
*/
public Map<Field,Value> getValues(List<? extends Field> theFields) {
validateMirrors(theFields);
int size = theFields.size();
JDWP.ReferenceType.GetValues.Field[] queryFields =
new JDWP.ReferenceType.GetValues.Field[size];
for (int i=0; i<size; i++) {
FieldImpl field = (FieldImpl)theFields.get(i);
validateFieldAccess(field);
// Do more validation specific to ReferenceType field getting
if (!field.isStatic()) {
throw new IllegalArgumentException(
"Attempt to use non-static field with ReferenceType");
}
queryFields[i] = new JDWP.ReferenceType.GetValues.Field(
field.ref());
}
Map<Field, Value> map = new HashMap<Field, Value>(size);
ValueImpl[] values;
try {
values = JDWP.ReferenceType.GetValues.
process(vm, this, queryFields).values;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
if (size != values.length) {
throw new InternalException(
"Wrong number of values returned from target VM");
}
for (int i=0; i<size; i++) {
FieldImpl field = (FieldImpl)theFields.get(i);
map.put(field, values[i]);
}
return map;
}
public ClassObjectReference classObject() {
if (classObject == null) {
// Are classObjects unique for an Object, or
// created each time? Is this spec'ed?
synchronized(this) {
if (classObject == null) {
try {
classObject = JDWP.ReferenceType.ClassObject.
process(vm, this).classObject;
} catch (JDWPException exc) {
throw exc.toJDIException();
}
}
}
}
return classObject;
}
SDE.Stratum stratum(String stratumID) {
SDE sde = sourceDebugExtensionInfo();
if (!sde.isValid()) {
sde = NO_SDE_INFO_MARK;
}
return sde.stratum(stratumID);
}
public String sourceName() throws AbsentInformationException {
return sourceNames(vm.getDefaultStratum()).get(0);
}
public List<String> sourceNames(String stratumID)
throws AbsentInformationException {
SDE.Stratum stratum = stratum(stratumID);
if (stratum.isJava()) {
List<String> result = new ArrayList<String>(1);
result.add(baseSourceName());
return result;
}
return stratum.sourceNames(this);
}
public List<String> sourcePaths(String stratumID)
throws AbsentInformationException {
SDE.Stratum stratum = stratum(stratumID);
if (stratum.isJava()) {
List<String> result = new ArrayList<String>(1);
result.add(baseSourceDir() + baseSourceName());
return result;
}
return stratum.sourcePaths(this);
}
String baseSourceName() throws AbsentInformationException {
String bsn = baseSourceName;
if (bsn == null) {
// Does not need synchronization, since worst-case
// static info is fetched twice
try {
bsn = JDWP.ReferenceType.SourceFile.
process(vm, this).sourceFile;
} catch (JDWPException exc) {
if (exc.errorCode() == JDWP.Error.ABSENT_INFORMATION) {
bsn = ABSENT_BASE_SOURCE_NAME;
} else {
throw exc.toJDIException();
}
}
baseSourceName = bsn;
}
if (bsn == ABSENT_BASE_SOURCE_NAME) {
throw new AbsentInformationException();
}
return bsn;
}
String baseSourcePath() throws AbsentInformationException {
String bsp = baseSourcePath;
if (bsp == null) {
bsp = baseSourceDir() + baseSourceName();
baseSourcePath = bsp;
}
return bsp;
}
String baseSourceDir() {
if (baseSourceDir == null) {
String typeName = name();
StringBuffer sb = new StringBuffer(typeName.length() + 10);
int index = 0;
int nextIndex;
while ((nextIndex = typeName.indexOf('.', index)) > 0) {
sb.append(typeName.substring(index, nextIndex));
sb.append(java.io.File.separatorChar);
index = nextIndex + 1;
}
baseSourceDir = sb.toString();
}
return baseSourceDir;
}
public String sourceDebugExtension()
throws AbsentInformationException {
if (!vm.canGetSourceDebugExtension()) {
throw new UnsupportedOperationException();
}
SDE sde = sourceDebugExtensionInfo();
if (sde == NO_SDE_INFO_MARK) {
throw new AbsentInformationException();
}
return sde.sourceDebugExtension;
}
private SDE sourceDebugExtensionInfo() {
if (!vm.canGetSourceDebugExtension()) {
return NO_SDE_INFO_MARK;
}
SDE sde = (sdeRef == null) ? null : sdeRef.get();
if (sde == null) {
String extension = null;
try {
extension = JDWP.ReferenceType.SourceDebugExtension.
process(vm, this).extension;
} catch (JDWPException exc) {
if (exc.errorCode() != JDWP.Error.ABSENT_INFORMATION) {
sdeRef = new SoftReference<SDE>(NO_SDE_INFO_MARK);
throw exc.toJDIException();
}
}
if (extension == null) {
sde = NO_SDE_INFO_MARK;
} else {
sde = new SDE(extension);
}
sdeRef = new SoftReference<SDE>(sde);
}
return sde;
}
public List<String> availableStrata() {
SDE sde = sourceDebugExtensionInfo();
if (sde.isValid()) {
return sde.availableStrata();
} else {
List<String> strata = new ArrayList<String>();
strata.add(SDE.BASE_STRATUM_NAME);
return strata;
}
}
/**
* Always returns non-null stratumID
*/
public String defaultStratum() {
SDE sdei = sourceDebugExtensionInfo();
if (sdei.isValid()) {
return sdei.defaultStratumId;
} else {
return SDE.BASE_STRATUM_NAME;
}
}
public int modifiers() {
if (modifiers == -1)
getModifiers();
return modifiers;
}
public List<Location> allLineLocations()
throws AbsentInformationException {
return allLineLocations(vm.getDefaultStratum(), null);
}
public List<Location> allLineLocations(String stratumID, String sourceName)
throws AbsentInformationException {
boolean someAbsent = false; // A method that should have info, didn't
SDE.Stratum stratum = stratum(stratumID);
List<Location> list = new ArrayList<Location>(); // location list
for (Iterator<Method> iter = methods().iterator(); iter.hasNext(); ) {
MethodImpl method = (MethodImpl)iter.next();
try {
list.addAll(
method.allLineLocations(stratum, sourceName));
} catch(AbsentInformationException exc) {
someAbsent = true;
}
}
// If we retrieved no line info, and at least one of the methods
// should have had some (as determined by an
// AbsentInformationException being thrown) then we rethrow
// the AbsentInformationException.
if (someAbsent && list.size() == 0) {
throw new AbsentInformationException();
}
return list;
}
public List<Location> locationsOfLine(int lineNumber)
throws AbsentInformationException {
return locationsOfLine(vm.getDefaultStratum(),
null,
lineNumber);
}
public List<Location> locationsOfLine(String stratumID,
String sourceName,
int lineNumber)
throws AbsentInformationException {
// A method that should have info, didn't
boolean someAbsent = false;
// A method that should have info, did
boolean somePresent = false;
List<Method> methods = methods();
SDE.Stratum stratum = stratum(stratumID);
List<Location> list = new ArrayList<Location>();
Iterator<Method> iter = methods.iterator();
while(iter.hasNext()) {
MethodImpl method = (MethodImpl)iter.next();
// eliminate native and abstract to eliminate
// false positives
if (!method.isAbstract() &&
!method.isNative()) {
try {
list.addAll(
method.locationsOfLine(stratum,
sourceName,
lineNumber));
somePresent = true;
} catch(AbsentInformationException exc) {
someAbsent = true;
}
}
}
if (someAbsent && !somePresent) {
throw new AbsentInformationException();
}
return list;
}
public List<ObjectReference> instances(long maxInstances) {
if (!vm.canGetInstanceInfo()) {
throw new UnsupportedOperationException(
"target does not support getting instances");
}
if (maxInstances < 0) {
throw new IllegalArgumentException("maxInstances is less than zero: "
+ maxInstances);
}
int intMax = (maxInstances > Integer.MAX_VALUE)?
Integer.MAX_VALUE: (int)maxInstances;
// JDWP can't currently handle more than this (in mustang)
try {
return Arrays.asList(
(ObjectReference[])JDWP.ReferenceType.Instances.
process(vm, this, intMax).instances);
} catch (JDWPException exc) {
throw exc.toJDIException();
}
}
private void getClassFileVersion() {
if (!vm.canGetClassFileVersion()) {
throw new UnsupportedOperationException();
}
JDWP.ReferenceType.ClassFileVersion classFileVersion;
if (versionNumberGotten) {
return;
} else {
try {
classFileVersion = JDWP.ReferenceType.ClassFileVersion.process(vm, this);
} catch (JDWPException exc) {
if (exc.errorCode() == JDWP.Error.ABSENT_INFORMATION) {
majorVersion = 0;
minorVersion = 0;
versionNumberGotten = true;
return;
} else {
throw exc.toJDIException();
}
}
majorVersion = classFileVersion.majorVersion;
minorVersion = classFileVersion.minorVersion;
versionNumberGotten = true;
}
}
public int majorVersion() {
try {
getClassFileVersion();
} catch (RuntimeException exc) {
throw exc;
}
return majorVersion;
}
public int minorVersion() {
try {
getClassFileVersion();
} catch (RuntimeException exc) {
throw exc;
}
return minorVersion;
}
private byte[] getConstantPoolInfo() {
JDWP.ReferenceType.ConstantPool jdwpCPool;
if (!vm.canGetConstantPool()) {
throw new UnsupportedOperationException();
}
if (constantPoolInfoGotten) {
if (constantPoolBytesRef == null) {
return null;
}
byte[] cpbytes = constantPoolBytesRef.get();
if (cpbytes != null) {
return cpbytes;
}
}
try {
jdwpCPool = JDWP.ReferenceType.ConstantPool.process(vm, this);
} catch (JDWPException exc) {
if (exc.errorCode() == JDWP.Error.ABSENT_INFORMATION) {
constanPoolCount = 0;
constantPoolBytesRef = null;
constantPoolInfoGotten = true;
return null;