2424 */
2525package jdk .tools .jlink .internal .plugins ;
2626
27- import com .sun .tools .classfile .Annotation ;
28- import com .sun .tools .classfile .Attribute ;
29- import com .sun .tools .classfile .Attributes ;
30- import com .sun .tools .classfile .ClassFile ;
31- import com .sun .tools .classfile .ConstantPool ;
32- import com .sun .tools .classfile .ConstantPoolException ;
33- import com .sun .tools .classfile .Field ;
34- import com .sun .tools .classfile .LocalVariableTable_attribute ;
35- import com .sun .tools .classfile .LocalVariableTypeTable_attribute ;
36- import com .sun .tools .classfile .Method ;
37- import com .sun .tools .classfile .RuntimeInvisibleAnnotations_attribute ;
38- import com .sun .tools .classfile .RuntimeParameterAnnotations_attribute ;
39- import com .sun .tools .classfile .RuntimeVisibleAnnotations_attribute ;
40- import com .sun .tools .classfile .Signature_attribute ;
27+ import jdk .internal .classfile .*;
28+ import jdk .internal .classfile .attribute .*;
29+ import jdk .internal .classfile .constantpool .*;
4130import java .io .ByteArrayInputStream ;
4231import java .io .ByteArrayOutputStream ;
4332import java .io .DataInputStream ;
@@ -80,10 +69,10 @@ private static final class CompactCPHelper {
8069
8170 private static final class DescriptorsScanner {
8271
83- private final ClassFile cf ;
72+ private final ClassModel cm ;
8473
85- private DescriptorsScanner (ClassFile cf ) {
86- this .cf = cf ;
74+ private DescriptorsScanner (ClassModel cm ) {
75+ this .cm = cm ;
8776 }
8877
8978 private Set <Integer > scan () throws Exception {
@@ -94,130 +83,109 @@ private Set<Integer> scan() throws Exception {
9483
9584 scanMethods (utf8Descriptors );
9685
97- scanAttributes (cf .attributes , utf8Descriptors );
86+ scanAttributes (cm .attributes () , utf8Descriptors );
9887
9988 return utf8Descriptors ;
10089 }
10190
102- private void scanAttributes (Attributes attributes ,
91+ private void scanAttributes (List < Attribute <?>> attributes ,
10392 Set <Integer > utf8Descriptors ) throws Exception {
104- for (Attribute a : attributes ) {
105- if (a instanceof Signature_attribute ) {
106- Signature_attribute sig = (Signature_attribute ) a ;
107- utf8Descriptors .add (sig .signature_index );
108- } else if (a instanceof RuntimeVisibleAnnotations_attribute ) {
109- RuntimeVisibleAnnotations_attribute an
110- = (RuntimeVisibleAnnotations_attribute ) a ;
111- for (Annotation annotation : an .annotations ) {
112- scanAnnotation (annotation , utf8Descriptors );
93+ for (Attribute <?> a : attributes ) {
94+ switch (a ) {
95+ case SignatureAttribute sig -> {
96+ utf8Descriptors .add (sig .signature ().index ());
11397 }
114- } else if (a instanceof RuntimeInvisibleAnnotations_attribute ) {
115- RuntimeInvisibleAnnotations_attribute an
116- = (RuntimeInvisibleAnnotations_attribute ) a ;
117- for (Annotation annotation : an .annotations ) {
118- scanAnnotation (annotation , utf8Descriptors );
98+ case RuntimeVisibleAnnotationsAttribute an -> {
99+ for (Annotation annotation : an .annotations ())
100+ scanAnnotation (annotation , utf8Descriptors );
119101 }
120- } else if (a instanceof RuntimeParameterAnnotations_attribute ) {
121- RuntimeParameterAnnotations_attribute rap
122- = (RuntimeParameterAnnotations_attribute ) a ;
123- for (Annotation [] arr : rap .parameter_annotations ) {
124- for (Annotation an : arr ) {
125- scanAnnotation (an , utf8Descriptors );
102+ case RuntimeInvisibleAnnotationsAttribute an -> {
103+ for (Annotation annotation : an .annotations ())
104+ scanAnnotation (annotation , utf8Descriptors );
105+ }
106+ case RuntimeVisibleParameterAnnotationsAttribute rap -> {
107+ for (List <Annotation > arr : rap .parameterAnnotations ()) {
108+ for (Annotation an : arr )
109+ scanAnnotation (an , utf8Descriptors );
110+ }
111+ }
112+ case RuntimeInvisibleParameterAnnotationsAttribute rap -> {
113+ for (List <Annotation > arr : rap .parameterAnnotations ()) {
114+ for (Annotation an : arr )
115+ scanAnnotation (an , utf8Descriptors );
126116 }
127117 }
128- } else if (a instanceof LocalVariableTable_attribute ) {
129- LocalVariableTable_attribute lvt
130- = (LocalVariableTable_attribute ) a ;
131- for (LocalVariableTable_attribute .Entry entry
132- : lvt .local_variable_table ) {
133- utf8Descriptors .add (entry .descriptor_index );
118+ case LocalVariableTableAttribute lvt -> {
119+ for (LocalVariableInfo entry : lvt .localVariables ())
120+ utf8Descriptors .add (entry .name ().index ());
134121 }
135- } else if (a instanceof LocalVariableTypeTable_attribute ) {
136- LocalVariableTypeTable_attribute lvt
137- = (LocalVariableTypeTable_attribute ) a ;
138- for (LocalVariableTypeTable_attribute .Entry entry
139- : lvt .local_variable_table ) {
140- utf8Descriptors .add (entry .signature_index );
122+ case LocalVariableTypeTableAttribute lvt -> {
123+ for (LocalVariableTypeInfo entry : lvt .localVariableTypes ())
124+ utf8Descriptors .add (entry .signature ().index ());
141125 }
126+ default -> {}
142127 }
143128 }
144129 }
145130
146131 private void scanAnnotation (Annotation annotation ,
147132 Set <Integer > utf8Descriptors ) throws Exception {
148- utf8Descriptors .add (annotation .type_index );
149- for (Annotation . element_value_pair evp : annotation .element_value_pairs ) {
150- utf8Descriptors .add (evp .element_name_index );
151- scanElementValue (evp .value , utf8Descriptors );
133+ utf8Descriptors .add (annotation .className (). index () );
134+ for (AnnotationElement evp : annotation .elements () ) {
135+ utf8Descriptors .add (evp .name (). index () );
136+ scanElementValue (evp .value () , utf8Descriptors );
152137 }
153138 }
154139
155- private void scanElementValue (Annotation . element_value value ,
140+ private void scanElementValue (AnnotationValue value ,
156141 Set <Integer > utf8Descriptors ) throws Exception {
157- if (value instanceof Annotation .Enum_element_value ) {
158- Annotation .Enum_element_value eev
159- = (Annotation .Enum_element_value ) value ;
160- utf8Descriptors .add (eev .type_name_index );
161- }
162- if (value instanceof Annotation .Class_element_value ) {
163- Annotation .Class_element_value eev
164- = (Annotation .Class_element_value ) value ;
165- utf8Descriptors .add (eev .class_info_index );
166- }
167- if (value instanceof Annotation .Annotation_element_value ) {
168- Annotation .Annotation_element_value aev
169- = (Annotation .Annotation_element_value ) value ;
170- scanAnnotation (aev .annotation_value , utf8Descriptors );
171- }
172- if (value instanceof Annotation .Array_element_value ) {
173- Annotation .Array_element_value aev
174- = (Annotation .Array_element_value ) value ;
175- for (Annotation .element_value v : aev .values ) {
176- scanElementValue (v , utf8Descriptors );
142+ switch (value ) {
143+ case AnnotationValue .OfEnum eev ->
144+ utf8Descriptors .add (eev .className ().index ());
145+ case AnnotationValue .OfClass eev ->
146+ utf8Descriptors .add (eev .className ().index ());
147+ case AnnotationValue .OfAnnotation aev ->
148+ scanAnnotation (aev .annotation (), utf8Descriptors );
149+ case AnnotationValue .OfArray aev -> {
150+ for (AnnotationValue v : aev .values ())
151+ scanElementValue (v , utf8Descriptors );
177152 }
153+ default -> {}
178154 }
179155 }
180156
181157 private void scanFields (Set <Integer > utf8Descriptors )
182158 throws Exception {
183- for (Field field : cf .fields ) {
184- int descriptorIndex = field .descriptor .index ;
159+ for (FieldModel field : cm .fields () ) {
160+ int descriptorIndex = field .fieldType () .index () ;
185161 utf8Descriptors .add (descriptorIndex );
186- scanAttributes (field .attributes , utf8Descriptors );
162+ scanAttributes (field .attributes () , utf8Descriptors );
187163 }
188164
189165 }
190166
191167 private void scanMethods (Set <Integer > utf8Descriptors )
192168 throws Exception {
193- for (Method m : cf .methods ) {
194- int descriptorIndex = m .descriptor .index ;
169+ for (MethodModel m : cm .methods () ) {
170+ int descriptorIndex = m .methodType () .index () ;
195171 utf8Descriptors .add (descriptorIndex );
196- scanAttributes (m .attributes , utf8Descriptors );
172+ scanAttributes (m .attributes () , utf8Descriptors );
197173 }
198174 }
199175
200176 private void scanConstantPool (Set <Integer > utf8Descriptors )
201177 throws Exception {
202- for (int i = 1 ; i < cf . constant_pool . size (); i ++ ) {
178+ for (int i = 1 ; i < cm . constantPool (). entryCount (); i += cm . constantPool (). entryByIndex ( i ). width () ) {
203179 try {
204- ConstantPool .CPInfo info = cf .constant_pool .get (i );
205- if (info instanceof ConstantPool .CONSTANT_NameAndType_info ) {
206- ConstantPool .CONSTANT_NameAndType_info nameAndType
207- = (ConstantPool .CONSTANT_NameAndType_info ) info ;
208- utf8Descriptors .add (nameAndType .type_index );
180+ PoolEntry info = cm .constantPool ().entryByIndex (i );
181+ switch (info ) {
182+ case NameAndTypeEntry nameAndType ->
183+ utf8Descriptors .add (nameAndType .type ().index ());
184+ case MethodTypeEntry mt ->
185+ utf8Descriptors .add (mt .descriptor ().index ());
186+ default -> {}
209187 }
210- if (info instanceof ConstantPool .CONSTANT_MethodType_info ) {
211- ConstantPool .CONSTANT_MethodType_info mt
212- = (ConstantPool .CONSTANT_MethodType_info ) info ;
213- utf8Descriptors .add (mt .descriptor_index );
214- }
215-
216- if (info instanceof ConstantPool .CONSTANT_Double_info
217- || info instanceof ConstantPool .CONSTANT_Long_info ) {
218- i ++;
219- }
220- } catch (ConstantPool .InvalidIndex ex ) {
188+ } catch (ConstantPoolException ex ) {
221189 throw new IOException (ex );
222190 }
223191 }
@@ -227,13 +195,7 @@ private void scanConstantPool(Set<Integer> utf8Descriptors)
227195 public byte [] transform (ResourcePoolEntry resource , ResourcePoolBuilder out ,
228196 StringTable strings ) throws IOException , Exception {
229197 byte [] content = resource .contentBytes ();
230- ClassFile cf ;
231- try (InputStream stream = new ByteArrayInputStream (content )) {
232- cf = ClassFile .read (stream );
233- } catch (ConstantPoolException ex ) {
234- throw new IOException ("Compressor EX " + ex + " for "
235- + resource .path () + " content.length " + content .length , ex );
236- }
198+ ClassModel cf = Classfile .of ().parse (content );
237199 DescriptorsScanner scanner = new DescriptorsScanner (cf );
238200 return optimize (resource , out , strings , scanner .scan (), content );
239201 }
@@ -254,16 +216,14 @@ private byte[] optimize(ResourcePoolEntry resource, ResourcePoolBuilder resource
254216 int tag = stream .readUnsignedByte ();
255217 byte [] arr ;
256218 switch (tag ) {
257- case ConstantPool . CONSTANT_Utf8 : {
219+ case Classfile . TAG_UTF8 : {
258220 String original = stream .readUTF ();
259221 // 2 cases, a Descriptor or a simple String
260222 if (descriptorIndexes .contains (i )) {
261223 SignatureParser .ParseResult parseResult
262224 = SignatureParser .parseSignatureDescriptor (original );
263225 List <Integer > indexes
264- = parseResult .types .stream ().map ((type ) -> {
265- return strings .addString (type );
266- }).toList ();
226+ = parseResult .types .stream ().map (strings ::addString ).toList ();
267227 if (!indexes .isEmpty ()) {
268228 out .write (StringSharingDecompressor .EXTERNALIZED_STRING_DESCRIPTOR );
269229 int sigIndex = strings .addString (parseResult .formatted );
@@ -280,11 +240,9 @@ private byte[] optimize(ResourcePoolEntry resource, ResourcePoolBuilder resource
280240
281241 break ;
282242 }
283-
284- case ConstantPool .CONSTANT_Long :
285- case ConstantPool .CONSTANT_Double : {
243+ case Classfile .TAG_LONG :
244+ case Classfile .TAG_DOUBLE :
286245 i ++;
287- }
288246 default : {
289247 out .write (tag );
290248 int size = SIZES [tag ];
0 commit comments