-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommonNameResolver.java
979 lines (924 loc) · 36.9 KB
/
CommonNameResolver.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
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG and IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
* IBM Corporation - additional resolvers and fixes
*******************************************************************************/
package org.eclipse.mat.inspections;
import java.lang.reflect.Modifier;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.extension.IClassSpecificNameResolver;
import org.eclipse.mat.snapshot.extension.Subject;
import org.eclipse.mat.snapshot.extension.Subjects;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.IObjectArray;
import org.eclipse.mat.snapshot.model.IPrimitiveArray;
import org.eclipse.mat.snapshot.model.ObjectReference;
import org.eclipse.mat.snapshot.model.PrettyPrinter;
import org.eclipse.mat.snapshot.registry.ClassSpecificNameResolverRegistry;
public class CommonNameResolver
{
@Subject("java.lang.String")
public static class StringResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
return PrettyPrinter.objectAsString(obj, 1024);
}
}
@Subjects( { "java.lang.StringBuffer", //
"java.lang.StringBuilder" })
public static class StringBufferResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
Integer count = (Integer) obj.resolveValue("count"); //$NON-NLS-1$
if (count == null)
return null;
if (count == 0)
return ""; //$NON-NLS-1$
IPrimitiveArray charArray = (IPrimitiveArray) obj.resolveValue("value"); //$NON-NLS-1$
if (charArray == null)
return null;
if (charArray.getType() == IObject.Type.BYTE)
{
// Java 9 compact strings
return PrettyPrinter.objectAsString(obj, 1024);
}
else
{
return PrettyPrinter.arrayAsString(charArray, 0, count, 1024);
}
}
}
@Subject("java.lang.Thread")
public static class ThreadResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
IObject name = (IObject) obj.resolveValue("name"); //$NON-NLS-1$
return name != null ? name.getClassSpecificName() : null;
}
}
@Subject("java.lang.ThreadGroup")
public static class ThreadGroupResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
IObject nameString = (IObject) object.resolveValue("name"); //$NON-NLS-1$
if (nameString == null)
return null;
return nameString.getClassSpecificName();
}
}
@Subjects( { "java.lang.Byte", //
"java.lang.Character", //
"java.lang.Short", //
"java.lang.Integer", //
"java.lang.Long", //
"java.lang.Float", //
"java.lang.Double", //
"java.lang.Boolean", //
"java.util.concurrent.atomic.AtomicInteger", //
"java.util.concurrent.atomic.AtomicLong", //
})
public static class ValueResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
Object value = heapObject.resolveValue("value"); //$NON-NLS-1$
return value != null ? String.valueOf(value) : null;
}
}
/**
* For Oracle VMs for int.class, byte.class, void.class etc.
* These are just simple IObjects, not IClass objects.
* All other classes resolve via IClass.
*/
@Subjects("java.lang.Class")
public static class ClassTypeResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
// Let normal IClass resolution happen if possible
if (object instanceof IClass)
return null;
IObject nameString = (IObject) object.resolveValue("name"); //$NON-NLS-1$
if (nameString == null)
return null;
return nameString.getClassSpecificName();
}
}
@Subjects("java.util.concurrent.atomic.AtomicBoolean")
public static class AtomicBooleanResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
Integer value = (Integer) heapObject.resolveValue("value"); //$NON-NLS-1$
return value != null ? Boolean.toString(value != 0) : null;
}
}
@Subjects("java.util.concurrent.atomic.AtomicReference")
public static class AtomicReferenceValueResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
IObject value = (IObject) heapObject.resolveValue("value"); //$NON-NLS-1$
return value != null ? ClassSpecificNameResolverRegistry.resolve(value) : null;
}
}
@Subjects("java.util.concurrent.atomic.AtomicStampedReference")
public static class AtomicStampedReferenceValueResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
IObject value = (IObject) heapObject.resolveValue("pair.reference"); //$NON-NLS-1$
return value != null ? ClassSpecificNameResolverRegistry.resolve(value) : null;
}
}
@Subject("char[]")
public static class CharArrayResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
IPrimitiveArray charArray = (IPrimitiveArray) heapObject;
// Check for empty array
int limit = 1024;
Object v = charArray.getValueArray(0, Math.min(charArray.getLength(), limit));
if (!(v instanceof char[]))
return null;
char[] value = (char[])v;
int j;
for (j = 0; j < value.length; j++)
{
if (value[j] != 0)
break;
}
if (j == value.length)
return null;
return PrettyPrinter.arrayAsString(charArray, 0, charArray.getLength(), limit);
}
}
@Subject("byte[]")
public static class ByteArrayResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
IPrimitiveArray arr = (IPrimitiveArray) heapObject;
// Check for empty array
Object v = arr.getValueArray(0, Math.min(arr.getLength(), 1024));
if (!(v instanceof byte[]))
return null;
byte[] value = (byte[])v;
int j;
for (j = 0; j < value.length; j++)
{
if (value[j] != 0)
break;
}
if (j == value.length)
return null;
// must not modify the original byte array
StringBuilder r = new StringBuilder(value.length);
for (int i = 0; i < value.length; i++)
{
// ASCII/Unicode 127 is not printable
if (value[i] < 32 || value[i] > 126)
r.append('.');
else
r.append((char) value[i]);
}
return r.toString();
}
}
/*
* Contributed in bug 273915
*/
@Subject("java.net.URL")
public static class URLResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
StringBuilder builder = new StringBuilder();
IObject protocol = (IObject) obj.resolveValue("protocol"); //$NON-NLS-1$
if (protocol != null)
{
builder.append(protocol.getClassSpecificName());
builder.append(":"); //$NON-NLS-1$
}
IObject authority = (IObject) obj.resolveValue("authority"); //$NON-NLS-1$
if (authority != null)
{
builder.append("//"); //$NON-NLS-1$
builder.append(authority.getClassSpecificName());
}
IObject path = (IObject) obj.resolveValue("path"); //$NON-NLS-1$
if (path != null)
builder.append(path.getClassSpecificName());
IObject query = (IObject) obj.resolveValue("query"); //$NON-NLS-1$
if (query != null)
{
builder.append("?"); //$NON-NLS-1$
builder.append(query.getClassSpecificName());
}
IObject ref = (IObject) obj.resolveValue("ref"); //$NON-NLS-1$
if (ref != null)
{
builder.append("#"); //$NON-NLS-1$
builder.append(ref.getClassSpecificName());
}
return builder.length() > 0 ? builder.toString() : null;
}
}
@Subject("java.net.URI")
public static class URIResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
StringBuilder builder = new StringBuilder();
IObject string = (IObject) obj.resolveValue("string"); //$NON-NLS-1$
if (string != null)
{
String val = string.getClassSpecificName();
if (val != null)
return val;
}
IObject protocol = (IObject) obj.resolveValue("scheme"); //$NON-NLS-1$
if (protocol != null)
{
builder.append(protocol.getClassSpecificName());
builder.append(":"); //$NON-NLS-1$
}
IObject authority = (IObject) obj.resolveValue("authority"); //$NON-NLS-1$
if (authority != null)
{
builder.append("//"); //$NON-NLS-1$
builder.append(authority.getClassSpecificName());
}
IObject path = (IObject) obj.resolveValue("path"); //$NON-NLS-1$
if (path != null)
builder.append(path.getClassSpecificName());
IObject query = (IObject) obj.resolveValue("query"); //$NON-NLS-1$
if (query != null)
{
builder.append("?"); //$NON-NLS-1$
builder.append(query.getClassSpecificName());
}
IObject ref = (IObject) obj.resolveValue("ref"); //$NON-NLS-1$
if (ref != null)
{
builder.append("#"); //$NON-NLS-1$
builder.append(ref.getClassSpecificName());
}
return builder.length() > 0 ? builder.toString() : null;
}
}
@Subject("java.lang.reflect.AccessibleObject")
public static class AccessibleObjectResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
// Important fields
// modifiers - not actually present, but present in all superclasses
// clazz - not actually present, but present in all superclasses
StringBuilder r = new StringBuilder();
ISnapshot snapshot = obj.getSnapshot();
IObject ref;
Object val = obj.resolveValue("modifiers"); //$NON-NLS-1$
if (val instanceof Integer)
{
r.append(Modifier.toString((Integer)val));
if (r.length() > 0) r.append(' ');
}
ref = (IObject) obj.resolveValue("clazz"); //$NON-NLS-1$
if (ref != null)
{
addClassName(snapshot, ref.getObjectAddress(), r);
}
else
{
return null;
}
return r.toString();
}
protected void addClassName(ISnapshot snapshot, long addr, StringBuilder r) throws SnapshotException
{
ObjectReference or = new ObjectReference(snapshot, addr);
IObject ox = or.getObject();
if (ox instanceof IClass)
{
IClass cls = (IClass) ox;
r.append(cls.getName());
}
}
}
@Subject("java.lang.reflect.Field")
public static class FieldResolver extends AccessibleObjectResolver
{
public String resolve(IObject obj) throws SnapshotException
{
// Important fields
// modifiers
// clazz
// name
// type
StringBuilder r = new StringBuilder();
ISnapshot snapshot = obj.getSnapshot();
IObject ref;
Object val = obj.resolveValue("modifiers"); //$NON-NLS-1$
if (val instanceof Integer)
{
r.append(Modifier.toString((Integer)val));
if (r.length() > 0) r.append(' ');
}
ref = (IObject) obj.resolveValue("type"); //$NON-NLS-1$
if (ref != null)
{
addClassName(snapshot, ref.getObjectAddress(), r);
r.append(' ');
}
ref = (IObject) obj.resolveValue("clazz"); //$NON-NLS-1$
if (ref != null)
{
addClassName(snapshot, ref.getObjectAddress(), r);
r.append('.');
}
ref = (IObject) obj.resolveValue("name"); //$NON-NLS-1$
if (ref != null)
{
r.append(ref.getClassSpecificName());
}
else
{
// No method name so give up
return null;
}
return r.toString();
}
}
@Subject("java.lang.reflect.Method")
public static class MethodResolver extends AccessibleObjectResolver
{
public String resolve(IObject obj) throws SnapshotException
{
// Important fields
// modifiers
// clazz
// name
// parameterTypes[]
// exceptionTypes[]
// returnType
StringBuilder r = new StringBuilder();
ISnapshot snapshot = obj.getSnapshot();
IObject ref;
Object val = obj.resolveValue("modifiers"); //$NON-NLS-1$
if (val instanceof Integer)
{
r.append(Modifier.toString((Integer)val));
if (r.length() > 0) r.append(' ');
}
ref = (IObject) obj.resolveValue("returnType"); //$NON-NLS-1$
if (ref != null)
{
addClassName(snapshot, ref.getObjectAddress(), r);
r.append(' ');
}
ref = (IObject) obj.resolveValue("clazz"); //$NON-NLS-1$
if (ref != null)
{
addClassName(snapshot, ref.getObjectAddress(), r);
r.append('.');
}
ref = (IObject) obj.resolveValue("name"); //$NON-NLS-1$
if (ref != null)
{
r.append(ref.getClassSpecificName());
}
else
{
// No method name so give up
return null;
}
r.append('(');
ref = (IObject) obj.resolveValue("parameterTypes"); //$NON-NLS-1$
if (ref instanceof IObjectArray)
{
IObjectArray orefa = (IObjectArray) ref;
long refs[] = orefa.getReferenceArray();
for (int i = 0; i < orefa.getLength(); ++i)
{
if (i > 0)
r.append(',');
long addr = refs[i];
addClassName(snapshot, addr, r);
}
}
r.append(')');
return r.toString();
}
}
@Subject("java.lang.reflect.Constructor")
public static class ConstructorResolver extends AccessibleObjectResolver
{
public String resolve(IObject obj) throws SnapshotException
{
// Important fields
// modifiers
// clazz
// parameterTypes[]
// exceptionTypes[]
StringBuilder r = new StringBuilder();
ISnapshot snapshot = obj.getSnapshot();
IObject ref;
Object val = obj.resolveValue("modifiers"); //$NON-NLS-1$
if (val instanceof Integer)
{
r.append(Modifier.toString((Integer)val));
if (r.length() > 0) r.append(' ');
}
ref = (IObject) obj.resolveValue("clazz"); //$NON-NLS-1$
if (ref != null)
{
addClassName(snapshot, ref.getObjectAddress(), r);
}
else
{
// No class name so give up
return null;
}
r.append('(');
ref = (IObject) obj.resolveValue("parameterTypes"); //$NON-NLS-1$
if (ref instanceof IObjectArray)
{
IObjectArray orefa = (IObjectArray) ref;
long refs[] = orefa.getReferenceArray();
for (int i = 0; i < orefa.getLength(); ++i)
{
if (i > 0)
r.append(',');
long addr = refs[i];
addClassName(snapshot, addr, r);
}
}
r.append(')');
return r.toString();
}
}
@Subject("java.lang.StackTraceElement")
public static class StackTraceElementResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
IObject cls = (IObject)obj.resolveValue("declaringClass"); //$NON-NLS-1$
IObject methodName = (IObject)obj.resolveValue("methodName"); //$NON-NLS-1$
if (cls == null || methodName == null)
return null;
int line = (Integer)obj.resolveValue("lineNumber"); //$NON-NLS-1$
IObject fn = (IObject)obj.resolveValue("fileName"); //$NON-NLS-1$
String ln;
if (line == -2)
ln = "(Compiled Code)"; //$NON-NLS-1$
else if (line == -3)
ln = "(Native Method)"; //$NON-NLS-1$
else if (line == -1)
ln = ""; //$NON-NLS-1$
else if (line == 0)
ln = ""; //$NON-NLS-1$
else
ln = Integer.toString(line);
String name;
if (fn == null)
if (line > 0)
name = cls.getClassSpecificName() + "." + methodName.getClassSpecificName() + "() " + ln; //$NON-NLS-1$ //$NON-NLS-2$
else
name = cls.getClassSpecificName() + "." + methodName.getClassSpecificName() + "()"; //$NON-NLS-1$//$NON-NLS-2$
else
if (line > 0)
name = cls.getClassSpecificName() + "." + methodName.getClassSpecificName() + "() (" //$NON-NLS-1$ //$NON-NLS-2$
+ fn.getClassSpecificName() + ":" + ln + ")"; //$NON-NLS-1$//$NON-NLS-2$
else
name = cls.getClassSpecificName() + "." + methodName.getClassSpecificName() + "() (" //$NON-NLS-1$ //$NON-NLS-2$
+ fn.getClassSpecificName() + ")"; //$NON-NLS-1$
return name;
}
}
@Subject("java.lang.Enum")
public static class EnumResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
IObject value = (IObject) heapObject.resolveValue("name"); //$NON-NLS-1$
return value != null ? ClassSpecificNameResolverRegistry.resolve(value) : null;
}
}
@Subject("java.lang.ProcessEnvironment$ExternalData")
public static class ExternalDataResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
IObject value = (IObject) heapObject.resolveValue("str"); //$NON-NLS-1$
return value != null ? ClassSpecificNameResolverRegistry.resolve(value) : null;
}
}
@Subject("java.lang.invoke.MemberName")
public static class MemberNameResolver implements IClassSpecificNameResolver
{
public String resolve(IObject heapObject) throws SnapshotException
{
StringBuilder buf = new StringBuilder();
IObject value = (IObject) heapObject.resolveValue("name"); //$NON-NLS-1$
if (value != null)
{
IObject cvalue = (IObject) heapObject.resolveValue("clazz"); //$NON-NLS-1$
if (cvalue instanceof IClass)
{
IClass cls = (IClass)cvalue;
buf.append(cls.getName()).append('.');
}
buf.append(ClassSpecificNameResolverRegistry.resolve(value));
return buf.toString();
}
return null;
}
}
@Subject("java.net.InetAddress")
public static class InetAddressResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
Object holder = object.resolveValue("holder"); //$NON-NLS-1$
if (holder instanceof IObject)
return ((IObject)holder).getClassSpecificName();
else
return null;
}
}
@Subject("java.net.InetAddress$InetAddressHolder")
public static class InetAddressHolderResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
Object host = object.resolveValue("hostName"); //$NON-NLS-1$
String hostname;
if (host instanceof IObject)
{
hostname = ((IObject)host).getClassSpecificName();
}
else
{
hostname = null;
}
Object address = object.resolveValue("address"); //$NON-NLS-1$
if (address instanceof Integer)
{
int addr = (Integer)address;
byte b[] = new byte[] { (byte)(addr >>> 24),
(byte)(addr >>> 16),
(byte)(addr >>> 8),
(byte)(addr >>> 0)};
try
{
if (hostname != null)
{
InetAddress in = InetAddress.getByAddress(hostname, b);
return in.getHostName()+"/"+in.getHostAddress(); //$NON-NLS-1$
}
else
{
InetAddress in = InetAddress.getByAddress(b);
return in.getHostAddress();
}
}
catch (UnknownHostException e)
{
}
}
else if (hostname != null)
{
return hostname;
}
return null;
}
}
@Subject("java.net.Inet6Address")
public static class Inet6AddressResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
Object holder = object.resolveValue("holder6"); //$NON-NLS-1$
if (holder instanceof IObject)
return ((IObject)holder).getClassSpecificName();
else
return null;
}
}
@Subject("java.net.Inet6Address$Inet6AddressHolder")
public static class Inet6AddressHolderResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
Object host = object.resolveValue("hostName"); //$NON-NLS-1$
String hostname;
if (host instanceof IObject)
{
hostname = ((IObject)host).getClassSpecificName();
}
else
{
hostname = null;
}
Object address = object.resolveValue("ipaddress"); //$NON-NLS-1$
if (address instanceof IPrimitiveArray)
{
IPrimitiveArray p = (IPrimitiveArray)address;
Object vals = p.getValueArray();
if (vals instanceof byte[])
{
byte b[] = (byte[])vals;
try
{
if (hostname != null)
{
Object os = object.resolveValue("scope_id_set"); //$NON-NLS-1$
Object oid = object.resolveValue("scope_id"); //$NON-NLS-1$
if (os instanceof Boolean && (Boolean)os && oid instanceof Integer)
{
InetAddress in = Inet6Address.getByAddress(hostname, b, (Integer)oid);
return in.getHostName()+"/" +in.getHostAddress(); //$NON-NLS-1$
}
else
{
InetAddress in = InetAddress.getByAddress(hostname, b);
return in.getHostName()+"/"+in.getHostAddress(); //$NON-NLS-1$
}
}
else
{
InetAddress in = InetAddress.getByAddress(b);
return in.getHostAddress();
}
}
catch (UnknownHostException e)
{
}
}
}
else if (hostname != null)
{
return hostname;
}
return null;
}
}
@Subject("java.net.InetSocketAddress$InetSocketAddressHolder")
public static class InetSocketAddressHolderResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
StringBuilder res = new StringBuilder();
Object addr = object.resolveValue("addr"); //$NON-NLS-1$
String addrname = (addr instanceof IObject) ? ((IObject) addr).getClassSpecificName() : null;
Object host = object.resolveValue("host"); //$NON-NLS-1$
String hostname = (host instanceof IObject) ? ((IObject) host).getClassSpecificName() : null;
if (addrname != null && addrname.indexOf('/') >= 0)
{
int slash = addrname.indexOf('/');
if (addrname.substring(slash + 1).indexOf(':') >= 0)
{
// IPv6
res.append(addrname.substring(0, slash + 1));
res.append('[');
res.append(addrname.substring(slash + 1));
res.append(']');
}
else
{
res.append(addrname);
}
}
else if (hostname != null)
{
res.append(hostname);
if (addrname != null)
{
res.append('/');
if (addrname.indexOf(':') >= 0)
res.append('[').append(addrname).append(']');
else
res.append(addrname);
}
}
else if (addrname != null)
{
if (addrname.indexOf(':') >= 0)
res.append('[').append(addrname).append(']');
else
res.append(addrname);
}
res.append(':');
Object port = object.resolveValue("port"); //$NON-NLS-1$
if (port instanceof Integer)
{
res.append(port);
}
if (res.length() > 1)
return res.toString();
else
return null;
}
}
@Subject("java.net.InetSocketAddress")
public static class InetSocketAddressResolver implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
Object holder = object.resolveValue("holder"); //$NON-NLS-1$
if (holder instanceof IObject)
{
return ((IObject)holder).getClassSpecificName();
}
else
return null;
}
}
@Subject("sun.nio.ch.SocketChannelImpl")
public static class SocketChannelImpl implements IClassSpecificNameResolver
{
public String resolve(IObject object) throws SnapshotException
{
StringBuilder sb = new StringBuilder();
Object state = object.resolveValue("state"); //$NON-NLS-1$
if (state instanceof Integer)
{
IClass cls = object.getClazz();
Object s1 = cls.resolveValue("ST_UNINITIALIZED"); //$NON-NLS-1$
Object s2 = cls.resolveValue("ST_UNCONNECTED"); //$NON-NLS-1$
Object s3 = cls.resolveValue("ST_PENDING"); //$NON-NLS-1$
Object s4 = cls.resolveValue("ST_CONNECTED"); //$NON-NLS-1$
Object s5 = cls.resolveValue("ST_KILLPENDING"); //$NON-NLS-1$
Object s6 = cls.resolveValue("ST_KILLED"); //$NON-NLS-1$
if (state.equals(s1))
sb.append("uninitialized"); //$NON-NLS-1$
else if (state.equals(s2))
{
sb.append("unconnected"); //$NON-NLS-1$
}
else if (state.equals(s3))
{
sb.append("pending"); //$NON-NLS-1$
}
else if (state.equals(s4))
{
sb.append("connected"); //$NON-NLS-1$
}
else if (state.equals(s5))
{
sb.append("kill pending"); //$NON-NLS-1$
}
else if (state.equals(s6))
{
sb.append("killed"); //$NON-NLS-1$
}
if (sb.length() > 0)
sb.append(' ');
}
sb.append("local="); //$NON-NLS-1$
Object localAddress = object.resolveValue("localAddress"); //$NON-NLS-1$
if (localAddress instanceof IObject)
{
String v = ((IObject)localAddress).getClassSpecificName();
if (v != null)
sb.append(v);
}
sb.append(" remoteAddress="); //$NON-NLS-1$
Object remoteAddress = object.resolveValue("remoteAddress"); //$NON-NLS-1$
if (remoteAddress instanceof IObject)
{
String v = ((IObject)remoteAddress).getClassSpecificName();
if (v != null)
sb.append(v);
}
return sb.toString();
}
}
@Subjects({"java.lang.Module","java.lang.Package"})
public static class ModuleResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
IObject name = (IObject) obj.resolveValue("name"); //$NON-NLS-1$
return name != null ? name.getClassSpecificName() : null;
}
}
private static String resolveField(IObject obj, String field) throws SnapshotException
{
IObject fieldObject = (IObject) obj.resolveValue(field);
return fieldObject != null ? fieldObject.getClassSpecificName() : null;
}
@Subjects({"java.util.Locale"})
public static class LocaleResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
String languageTag = resolveField(obj, "languageTag"); //$NON-NLS-1$
if (languageTag != null && !languageTag.isEmpty())
return languageTag;
return resolveField(obj, "baseLocale"); //$NON-NLS-1$
}
}
@Subjects({"sun.util.locale.BaseLocale"})
public static class SunLocaleResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
String lang = resolveField(obj, "language"); //$NON-NLS-1$
String region = resolveField(obj, "region"); //$NON-NLS-1$
String script = resolveField(obj, "script"); //$NON-NLS-1$
String variant = resolveField(obj, "variant"); //$NON-NLS-1$
StringBuilder result = new StringBuilder();
if (lang != null)
result.append(lang);
if (script != null && !script.isEmpty())
{
if (result.length() > 0)
result.append('_');
result.append(script);
}
if (region != null && !region.isEmpty())
{
if (result.length() > 0)
result.append('_');
result.append(region);
}
if (variant != null && !variant.isEmpty())
{
if (result.length() > 0)
result.append('_');
result.append(variant);
}
return result.toString();
}
}
@Subjects({"com.ibm.icu.impl.locale.BaseLocale"})
public static class ICUBaseLocaleResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
String lang = resolveField(obj, "_language"); //$NON-NLS-1$
String region = resolveField(obj, "_region"); //$NON-NLS-1$
String script = resolveField(obj, "_script"); //$NON-NLS-1$
String variant = resolveField(obj, "_variant"); //$NON-NLS-1$
StringBuilder result = new StringBuilder();
if (lang != null)
result.append(lang);
if (script != null && !script.isEmpty())
{
if (result.length() > 0)
result.append('_');
result.append(script);
}
if (region != null && !region.isEmpty())
{
if (result.length() > 0)
result.append('_');
result.append(region);
}
if (variant != null && !variant.isEmpty())
{
if (result.length() > 0)
result.append('_');
result.append(variant);
}
return result.toString();
}
}
@Subjects({"com.ibm.icu.util.ULocale"})
public static class ULocaleResolver implements IClassSpecificNameResolver
{
public String resolve(IObject obj) throws SnapshotException
{
String localeID = resolveField(obj, "localeID"); //$NON-NLS-1$
if (localeID != null && !localeID.isEmpty())
return localeID;
String locale = resolveField(obj, "locale"); //$NON-NLS-1$
if (locale != null && !locale.isEmpty())
return locale;
String baseLocale = resolveField(obj, "baseLocale"); //$NON-NLS-1$
if (baseLocale != null && !baseLocale.isEmpty())
return locale;
return null;
}
}
}