53
53
import static java .lang .classfile .constantpool .PoolEntry .TAG_LONG ;
54
54
import static java .lang .classfile .constantpool .PoolEntry .TAG_STRING ;
55
55
import static java .lang .classfile .constantpool .PoolEntry .*;
56
+ import static java .lang .constant .ConstantDescs .BSM_NULL_CONSTANT ;
56
57
import static java .util .Objects .requireNonNull ;
57
58
import static jdk .internal .classfile .impl .ClassPrinterImpl .Style .BLOCK ;
58
59
import static jdk .internal .classfile .impl .ClassPrinterImpl .Style .FLOW ;
@@ -274,7 +275,12 @@ public static void toYaml(Node node, Consumer<String> out) {
274
275
private static void toYaml (int indent , boolean skipFirstIndent , Node node , Consumer <String > out ) {
275
276
switch (node ) {
276
277
case LeafNode leaf -> {
277
- out .accept (quoteAndEscapeYaml (leaf .value ()));
278
+ var v = leaf .value ();
279
+ if (BSM_NULL_CONSTANT .equals (v )) {
280
+ out .accept ("null" );
281
+ } else {
282
+ out .accept (quoteAndEscapeYaml (v ));
283
+ }
278
284
}
279
285
case ListNodeImpl list -> {
280
286
switch (list .style ()) {
@@ -329,6 +335,7 @@ private static String quoteAndEscapeYaml(ConstantDesc value) {
329
335
String s = String .valueOf (value );
330
336
if (value instanceof Number ) return s ;
331
337
if (s .length () == 0 ) return "''" ;
338
+ if (s .equalsIgnoreCase ("null" )) return "'" + s + "'" ;
332
339
var sb = new StringBuilder (s .length () << 1 );
333
340
s .chars ().forEach (c -> {
334
341
switch (c ) {
@@ -358,7 +365,12 @@ public static void toJson(Node node, Consumer<String> out) {
358
365
private static void toJson (int indent , boolean skipFirstIndent , Node node , Consumer <String > out ) {
359
366
switch (node ) {
360
367
case LeafNode leaf -> {
361
- out .accept (quoteAndEscapeJson (leaf .value ()));
368
+ var v = leaf .value ();
369
+ if (BSM_NULL_CONSTANT .equals (v )) {
370
+ out .accept ("null" );
371
+ } else {
372
+ out .accept (quoteAndEscapeJson (v ));
373
+ }
362
374
}
363
375
case ListNodeImpl list -> {
364
376
out .accept ("[" );
@@ -434,7 +446,12 @@ private static void toXml(int indent, boolean skipFirstIndent, Node node, Consum
434
446
switch (node ) {
435
447
case LeafNode leaf -> {
436
448
out .accept ("<" + name + ">" );
437
- out .accept (xmlEscape (leaf .value ()));
449
+ var v = leaf .value ();
450
+ if (BSM_NULL_CONSTANT .equals (v )) {
451
+ out .accept ("<null/>" );
452
+ } else {
453
+ out .accept (xmlEscape (v ));
454
+ }
438
455
}
439
456
case ListNodeImpl list -> {
440
457
switch (list .style ()) {
@@ -542,7 +559,7 @@ private static Stream<ConstantDesc> convertVTIs(CodeAttribute lr, List<Verificat
542
559
ret .accept ("long" );
543
560
ret .accept ("long2" );
544
561
}
545
- case NULL -> ret .accept ("null" );
562
+ case NULL -> ret .accept (BSM_NULL_CONSTANT );
546
563
case TOP -> ret .accept ("?" );
547
564
case UNINITIALIZED_THIS -> ret .accept ("THIS" );
548
565
}
@@ -929,9 +946,9 @@ private static Node[] attributesToTree(List<Attribute<?>> attributes, Verbosity
929
946
nodes .add (map ("enclosing method" ,
930
947
"class" , ema .enclosingClass ().name ().stringValue (),
931
948
"method name" , ema .enclosingMethodName ()
932
- .map (Utf8Entry ::stringValue ).orElse ("null" ),
949
+ .< ConstantDesc > map (Utf8Entry ::stringValue ).orElse (BSM_NULL_CONSTANT ),
933
950
"method type" , ema .enclosingMethodType ()
934
- .map (Utf8Entry ::stringValue ).orElse ("null" )));
951
+ .< ConstantDesc > map (Utf8Entry ::stringValue ).orElse (BSM_NULL_CONSTANT )));
935
952
case ExceptionsAttribute exa ->
936
953
nodes .add (list ("exceptions" , "exc" , exa .exceptions ().stream ()
937
954
.map (e -> e .name ().stringValue ())));
@@ -940,23 +957,23 @@ private static Node[] attributesToTree(List<Attribute<?>> attributes, Verbosity
940
957
.map (ic -> new MapNodeImpl (FLOW , "cls" ).with (
941
958
leaf ("inner class" , ic .innerClass ().name ().stringValue ()),
942
959
leaf ("outer class" , ic .outerClass ()
943
- .map (cle -> cle .name ().stringValue ()).orElse ("null" )),
944
- leaf ("inner name" , ic .innerName ().map (Utf8Entry ::stringValue ).orElse ("null" )),
960
+ .map (cle -> ( ConstantDesc ) cle .name ().stringValue ()).orElse (BSM_NULL_CONSTANT )),
961
+ leaf ("inner name" , ic .innerName ().< ConstantDesc > map (Utf8Entry ::stringValue ).orElse (BSM_NULL_CONSTANT )),
945
962
list ("flags" , "flag" , ic .flags ().stream ().map (AccessFlag ::name ))))));
946
963
case MethodParametersAttribute mpa -> {
947
964
var n = new MapNodeImpl (BLOCK , "method parameters" );
948
965
for (int i = 0 ; i < mpa .parameters ().size (); i ++) {
949
966
var p = mpa .parameters ().get (i );
950
967
n .with (new MapNodeImpl (FLOW , i + 1 ).with (
951
- leaf ("name" , p .name ().map (Utf8Entry ::stringValue ).orElse ("null" )),
968
+ leaf ("name" , p .name ().< ConstantDesc > map (Utf8Entry ::stringValue ).orElse (BSM_NULL_CONSTANT )),
952
969
list ("flags" , "flag" , p .flags ().stream ().map (AccessFlag ::name ))));
953
970
}
954
971
}
955
972
case ModuleAttribute ma ->
956
973
nodes .add (new MapNodeImpl (BLOCK , "module" )
957
974
.with (leaf ("name" , ma .moduleName ().name ().stringValue ()),
958
975
list ("flags" ,"flag" , ma .moduleFlags ().stream ().map (AccessFlag ::name )),
959
- leaf ("version" , ma .moduleVersion ().map (Utf8Entry ::stringValue ).orElse ("null" )),
976
+ leaf ("version" , ma .moduleVersion ().< ConstantDesc > map (Utf8Entry ::stringValue ).orElse (BSM_NULL_CONSTANT )),
960
977
list ("uses" , "class" , ma .uses ().stream ().map (ce -> ce .name ().stringValue ())),
961
978
new ListNodeImpl (BLOCK , "requires" , ma .requires ().stream ().map (req ->
962
979
new MapNodeImpl (FLOW , "req" ).with (
0 commit comments