1
1
/*
2
- * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
30
30
* @run main NoUnnecessaryLoadableDescriptorsTest
31
31
*/
32
32
33
- import com .sun .tools .classfile .*;
34
- import com .sun .tools .classfile .ConstantPool .CONSTANT_Utf8_info ;
33
+ import java .lang .classfile .Attributes ;
34
+ import java .lang .classfile .ClassFile ;
35
+ import java .lang .classfile .ClassModel ;
36
+ import java .lang .classfile .attribute .LoadableDescriptorsAttribute ;
37
+ import java .lang .classfile .constantpool .Utf8Entry ;
38
+ import java .util .List ;
35
39
36
40
public class NoUnnecessaryLoadableDescriptorsTest {
37
41
@@ -50,80 +54,65 @@ static value class Inner2 {}
50
54
}
51
55
52
56
public static void main (String [] args ) throws Exception {
57
+ ClassModel cls ;
58
+ LoadableDescriptorsAttribute loadableDescriptors ;
53
59
54
60
// There should be no LoadableDescriptors attribute in NoUnnecessaryLoadableDescriptorsTest.class
55
- ClassFile cls = ClassFile .read (NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest.class" ));
56
-
57
- if (cls == null ) {
58
- throw new AssertionError ("Could not locate the class files" );
61
+ try (var in = NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest.class" )) {
62
+ cls = ClassFile .of ().parse (in .readAllBytes ());
59
63
}
60
64
61
65
/* Check emission of LoadableDescriptors attribute */
62
- LoadableDescriptors_attribute LoadableDescriptors = (LoadableDescriptors_attribute ) cls .attributes .get (Attribute .LoadableDescriptors );
63
- if (LoadableDescriptors != null ) {
66
+ if (cls .findAttribute (Attributes .loadableDescriptors ()).isPresent ()) {
64
67
throw new AssertionError ("Unexpected LoadableDescriptors attribute!" );
65
68
}
66
69
67
70
// There should be no LoadableDescriptors attribute in NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest1.class
68
- cls = ClassFile .read (NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest1.class" ));
69
-
70
- if (cls == null ) {
71
- throw new AssertionError ("Could not locate the class files" );
71
+ try (var in = NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest1.class" )) {
72
+ cls = ClassFile .of ().parse (in .readAllBytes ());
72
73
}
73
74
74
75
/* Check emission of LoadableDescriptors attribute */
75
- LoadableDescriptors = (LoadableDescriptors_attribute ) cls .attributes .get (Attribute .LoadableDescriptors );
76
- if (LoadableDescriptors != null ) {
76
+ if (cls .findAttribute (Attributes .loadableDescriptors ()).isPresent ()) {
77
77
throw new AssertionError ("Unexpected LoadableDescriptors attribute!" );
78
78
}
79
79
80
- // There should be no LoadableDescriptors attribute in NoUnnecessaryLoadableDescriptorsTest$PreloadTest2.class
81
- cls = ClassFile .read (NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2.class" ));
82
-
83
- if (cls == null ) {
84
- throw new AssertionError ("Could not locate the class files" );
80
+ // There should be no LoadableDescriptors attribute in NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2.class
81
+ try (var in = NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2.class" )) {
82
+ cls = ClassFile .of ().parse (in .readAllBytes ());
85
83
}
86
84
87
85
/* Check emission of LoadableDescriptors attribute */
88
- LoadableDescriptors = (LoadableDescriptors_attribute ) cls .attributes .get (Attribute .LoadableDescriptors );
89
- if (LoadableDescriptors != null ) {
86
+ if (cls .findAttribute (Attributes .loadableDescriptors ()).isPresent ()) {
90
87
throw new AssertionError ("Unexpected LoadableDescriptors attribute!" );
91
88
}
92
89
93
90
// There should be no LoadableDescriptors attribute in NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner2.class
94
- cls = ClassFile .read (NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner1$Inner2.class" ));
95
-
96
- if (cls == null ) {
97
- throw new AssertionError ("Could not locate the class files" );
91
+ try (var in = NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner1$Inner2.class" )) {
92
+ cls = ClassFile .of ().parse (in .readAllBytes ());
98
93
}
99
94
100
95
/* Check emission of LoadableDescriptors attribute */
101
- LoadableDescriptors = (LoadableDescriptors_attribute ) cls .attributes .get (Attribute .LoadableDescriptors );
102
- if (LoadableDescriptors != null ) {
96
+ if (cls .findAttribute (Attributes .loadableDescriptors ()).isPresent ()) {
103
97
throw new AssertionError ("Unexpected LoadableDescriptors attribute!" );
104
98
}
105
99
106
100
// There should be ONE LoadableDescriptors attribute entry in NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner1.class
107
- cls = ClassFile .read (NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner1.class" ));
101
+ try (var in = NoUnnecessaryLoadableDescriptorsTest .class .getResourceAsStream ("NoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner1.class" )) {
102
+ cls = ClassFile .of ().parse (in .readAllBytes ());
103
+ }
108
104
109
105
if (cls == null ) {
110
106
throw new AssertionError ("Could not locate the class files" );
111
107
}
112
108
113
109
/* Check emission of LoadableDescriptors attribute */
114
- LoadableDescriptors = (LoadableDescriptors_attribute ) cls .attributes .get (Attribute .LoadableDescriptors );
115
- if (LoadableDescriptors == null ) {
116
- throw new AssertionError ("Missing LoadableDescriptors attribute!" );
117
- }
118
-
119
- if (LoadableDescriptors .number_of_descriptors != 1 ) {
120
- throw new AssertionError ("Incorrect number of LoadableDescriptors classes" );
121
- }
110
+ loadableDescriptors = cls .findAttribute (Attributes .loadableDescriptors ()).orElseThrow ();
122
111
123
- CONSTANT_Utf8_info utf8Info = cls . constant_pool . getUTF8Info ( LoadableDescriptors . descriptors [ 0 ] );
124
- System . err . println ( "utf8 " + utf8Info . value );
125
- if (!utf8Info . value . equals ("LNoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner1$Inner2;" )) {
126
- throw new AssertionError ("Expected LoadableDescriptors class entry is missing , but found " + utf8Info . value );
112
+ List < String > expected = List . of ( "LNoUnnecessaryLoadableDescriptorsTest$LoadableDescriptorsTest2$Inner1$Inner2;" );
113
+ List < String > found = loadableDescriptors . loadableDescriptors (). stream (). map ( Utf8Entry :: stringValue ). toList ( );
114
+ if (!expected . equals (found )) {
115
+ throw new AssertionError ("Expected one LoadableDescriptors class entry Inner2 , but found " + found );
127
116
}
128
117
}
129
118
}
0 commit comments