2424/*
2525 * @test 6716452
2626 * @summary need a method to get an index of an attribute
27- * @modules jdk.jdeps/com.sun.tools.classfile
27+ * @modules java.base/jdk.internal.classfile
28+ * java.base/jdk.internal.classfile.attribute
29+ * java.base/jdk.internal.classfile.constantpool
30+ * java.base/jdk.internal.classfile.instruction
31+ * java.base/jdk.internal.classfile.components
2832 */
2933
3034import java .io .*;
31- import com .sun .tools .classfile .*;
35+ import java .nio .file .Files ;
36+
37+ import jdk .internal .classfile .*;
38+ import jdk .internal .classfile .attribute .*;
3239
3340public class T6716452 {
3441 public static void main (String [] args ) throws Exception {
@@ -38,49 +45,44 @@ public static void main(String[] args) throws Exception {
3845 public void run () throws Exception {
3946 File javaFile = writeTestFile ();
4047 File classFile = compileTestFile (javaFile );
41-
42- ClassFile cf = ClassFile .read (classFile );
43- for (Method m : cf .methods ) {
44- test (cf , m );
48+ ClassModel cm = Classfile .of ().parse (classFile .toPath ());
49+ for (MethodModel mm : cm .methods ()) {
50+ test (mm );
4551 }
4652
4753 if (errors > 0 )
4854 throw new Exception (errors + " errors found" );
4955 }
5056
51- void test (ClassFile cf , Method m ) {
52- test (cf , m , Attribute . Code , Code_attribute .class );
53- test (cf , m , Attribute . Exceptions , Exceptions_attribute .class );
57+ void test (MethodModel mm ) {
58+ test (mm , Attributes . CODE , CodeAttribute .class );
59+ test (mm , Attributes . EXCEPTIONS , ExceptionsAttribute .class );
5460 }
5561
56- // test the result of Attributes.getIndex according to expectations
62+ // test the result of MethodModel.findAttribute, MethodModel.attributes().indexOf() according to expectations
5763 // encoded in the method's name
58- void test (ClassFile cf , Method m , String name , Class <?> c ) {
59- int index = m .attributes .getIndex (cf .constant_pool , name );
60- try {
61- String m_name = m .getName (cf .constant_pool );
62- System .err .println ("Method " + m_name + " name:" + name + " index:" + index + " class: " + c );
63- boolean expect = (m_name .equals ("<init>" ) && name .equals ("Code" ))
64- || (m_name .indexOf (name ) != -1 );
65- boolean found = (index != -1 );
66- if (expect ) {
67- if (found ) {
68- Attribute attr = m .attributes .get (index );
69- if (!c .isAssignableFrom (attr .getClass ())) {
70- error (m + ": unexpected attribute found,"
71- + " expected " + c .getName ()
72- + " found " + attr .getClass ().getName ());
73- }
74- } else {
75- error (m + ": expected attribute " + name + " not found" );
64+ <T extends Attribute <T >> void test (MethodModel mm , AttributeMapper <T > attr , Class <?> c ) {
65+ Attribute <T > attr_instance = mm .findAttribute (attr ).orElse (null );
66+ int index = mm .attributes ().indexOf (attr_instance );
67+ String mm_name = mm .methodName ().stringValue ();
68+ System .err .println ("Method " + mm_name + " name:" + attr .name () + " index:" + index + " class: " + c );
69+ boolean expect = (mm_name .equals ("<init>" ) && attr .name ().equals ("Code" ))
70+ || (mm_name .contains (attr .name ()));
71+ boolean found = (index != -1 );
72+ if (expect ) {
73+ if (found ) {
74+ if (!c .isAssignableFrom (mm .attributes ().get (index ).getClass ())) {
75+ error (mm + ": unexpected attribute found,"
76+ + " expected " + c .getName ()
77+ + " found " + mm .attributes ().get (index ).attributeName ());
7678 }
7779 } else {
78- if (found ) {
79- error (m + ": unexpected attribute " + name );
80- }
80+ error (mm + ": expected attribute " + attr .name () + " not found" );
81+ }
82+ } else {
83+ if (found ) {
84+ error (mm + ": unexpected attribute " + attr .name ());
8185 }
82- } catch (ConstantPoolException e ) {
83- error (m + ": " + e );
8486 }
8587 }
8688
0 commit comments