|
1 | 1 | /* |
2 | | - * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2013, 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 |
|
25 | 25 | * @test |
26 | 26 | * @bug 8005085 8005681 8008769 8010015 |
27 | 27 | * @summary Check (repeating)type annotations on lambda usage. |
28 | | - * @modules jdk.jdeps/com.sun.tools.classfile |
29 | 28 | * @run main CombinationsTargetTest3 |
30 | 29 | */ |
31 | 30 |
|
32 | | -import com.sun.tools.classfile.*; |
33 | 31 | import java.io.*; |
34 | | -import java.util.ArrayList; |
35 | | -import java.util.List; |
| 32 | +import java.lang.classfile.*; |
36 | 33 | import java.util.Vector; |
37 | 34 |
|
38 | 35 | public class CombinationsTargetTest3 extends ClassfileTestHelper { |
@@ -203,19 +200,19 @@ else if (source.equals(srce.src6) && |
203 | 200 | classFile=new File(classdir.concat(source.altClassName)); |
204 | 201 | source.innerClassname=null; |
205 | 202 | } |
206 | | - ClassFile cf = ClassFile.read(classFile); |
| 203 | + ClassModel cf = ClassFile.of().parse(classFile.toPath()); |
207 | 204 |
|
208 | | - println("Testing classfile: " + cf.getName()); |
| 205 | + println("Testing classfile: " + cf.thisClass().asInternalName()); |
209 | 206 | //Test class,fields and method counts. |
210 | 207 | test(cf); |
211 | 208 |
|
212 | | - for (Field f : cf.fields) { |
213 | | - test(cf, f); |
214 | | - test(cf, f, true); |
| 209 | + for (FieldModel f : cf.fields()) { |
| 210 | + test(f); |
| 211 | + test(f, true); |
215 | 212 | } |
216 | | - for (Method m: cf.methods) { |
217 | | - test(cf, m); |
218 | | - test(cf, m, true); |
| 213 | + for (MethodModel m: cf.methods()) { |
| 214 | + test(m); |
| 215 | + test(m, true); |
219 | 216 | } |
220 | 217 |
|
221 | 218 | countAnnotations(); // sets errors=0 before counting. |
@@ -539,139 +536,4 @@ String sourceString(String testname, String retentn, String annot2, |
539 | 536 | } |
540 | 537 | return imports + source; |
541 | 538 | } |
542 | | - |
543 | | - /************ Helper annotations counting methods ******************/ |
544 | | - void test(ClassFile cf) { |
545 | | - test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true); |
546 | | - test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false); |
547 | | - //RuntimeAnnotations since one annotation can result in two attributes. |
548 | | - test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true); |
549 | | - test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false); |
550 | | - } |
551 | | - |
552 | | - void test(ClassFile cf, Field f, Boolean local) { |
553 | | - if (!local) { |
554 | | - test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true); |
555 | | - test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false); |
556 | | - test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true); |
557 | | - test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false); |
558 | | - } else { |
559 | | - test("CODE",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true); |
560 | | - test("CODE",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false); |
561 | | - test("CODE",cf, f, null, Attribute.RuntimeVisibleAnnotations, true); |
562 | | - test("CODE",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false); |
563 | | - } |
564 | | - } |
565 | | - |
566 | | - void test(ClassFile cf, Field f) { |
567 | | - test(cf, f, false); |
568 | | - } |
569 | | - |
570 | | - // 'local' determines whether to look for annotations in code attribute or not. |
571 | | - void test(ClassFile cf, Method m, Boolean local) { |
572 | | - if (!local) { |
573 | | - test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
574 | | - test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
575 | | - test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true); |
576 | | - test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false); |
577 | | - } else { |
578 | | - test("MCODE",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
579 | | - test("MCODE",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
580 | | - test("MCODE",cf, null, m, Attribute.RuntimeVisibleAnnotations, true); |
581 | | - test("MCODE",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false); |
582 | | - } |
583 | | - } |
584 | | - |
585 | | - // default to not looking in code attribute |
586 | | - void test(ClassFile cf, Method m ) { |
587 | | - test(cf, m, false); |
588 | | - } |
589 | | - |
590 | | - // Test the result of Attributes.getIndex according to expectations |
591 | | - // encoded in the class/field/method name; increment annotations counts. |
592 | | - void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) { |
593 | | - String testtype = ttype; |
594 | | - String name = null; |
595 | | - int index = -1; |
596 | | - Attribute attr = null; |
597 | | - Code_attribute cAttr = null; |
598 | | - boolean isTAattr = annName.contains("TypeAnnotations"); |
599 | | - try { |
600 | | - switch(testtype) { |
601 | | - case "FIELD": |
602 | | - name = f.getName(cf.constant_pool); |
603 | | - index = f.attributes.getIndex(cf.constant_pool, annName); |
604 | | - if(index!= -1) |
605 | | - attr = f.attributes.get(index); |
606 | | - break; |
607 | | - case "CODE": |
608 | | - name = f.getName(cf.constant_pool); |
609 | | - //fetch index of and code attribute and annotations from code attribute. |
610 | | - index = cf.attributes.getIndex(cf.constant_pool, Attribute.Code); |
611 | | - if(index!= -1) { |
612 | | - attr = cf.attributes.get(index); |
613 | | - assert attr instanceof Code_attribute; |
614 | | - cAttr = (Code_attribute)attr; |
615 | | - index = cAttr.attributes.getIndex(cf.constant_pool, annName); |
616 | | - if(index!= -1) |
617 | | - attr = cAttr.attributes.get(index); |
618 | | - } |
619 | | - break; |
620 | | - case "METHOD": |
621 | | - name = m.getName(cf.constant_pool); |
622 | | - index = m.attributes.getIndex(cf.constant_pool, annName); |
623 | | - if(index!= -1) |
624 | | - attr = m.attributes.get(index); |
625 | | - break; |
626 | | - case "MCODE": |
627 | | - name = m.getName(cf.constant_pool); |
628 | | - //fetch index of and code attribute and annotations from code attribute. |
629 | | - index = m.attributes.getIndex(cf.constant_pool, Attribute.Code); |
630 | | - if(index!= -1) { |
631 | | - attr = m.attributes.get(index); |
632 | | - assert attr instanceof Code_attribute; |
633 | | - cAttr = (Code_attribute)attr; |
634 | | - index = cAttr.attributes.getIndex(cf.constant_pool, annName); |
635 | | - if(index!= -1) |
636 | | - attr = cAttr.attributes.get(index); |
637 | | - } |
638 | | - break; |
639 | | - default: |
640 | | - name = cf.getName(); |
641 | | - index = cf.attributes.getIndex(cf.constant_pool, annName); |
642 | | - if(index!= -1) attr = cf.attributes.get(index); |
643 | | - } |
644 | | - } catch(ConstantPoolException cpe) { cpe.printStackTrace(); } |
645 | | - |
646 | | - if (index != -1) { |
647 | | - if(isTAattr) { //count RuntimeTypeAnnotations |
648 | | - RuntimeTypeAnnotations_attribute tAttr = |
649 | | - (RuntimeTypeAnnotations_attribute)attr; |
650 | | - System.out.println(testtype + ": " + name + ", " + annName + ": " + |
651 | | - tAttr.annotations.length ); |
652 | | - if (tAttr.annotations.length > 0) { |
653 | | - for (int i = 0; i < tAttr.annotations.length; i++) { |
654 | | - System.out.println(" types:" + tAttr.annotations[i].position.type); |
655 | | - } |
656 | | - } else { |
657 | | - System.out.println(""); |
658 | | - } |
659 | | - allt += tAttr.annotations.length; |
660 | | - if (visible) |
661 | | - tvisibles += tAttr.annotations.length; |
662 | | - else |
663 | | - tinvisibles += tAttr.annotations.length; |
664 | | - } else { |
665 | | - RuntimeAnnotations_attribute tAttr = |
666 | | - (RuntimeAnnotations_attribute)attr; |
667 | | - System.out.println(testtype + ": " + name + ", " + annName + ": " + |
668 | | - tAttr.annotations.length ); |
669 | | - all += tAttr.annotations.length; |
670 | | - if (visible) |
671 | | - visibles += tAttr.annotations.length; |
672 | | - else |
673 | | - invisibles += tAttr.annotations.length; |
674 | | - } |
675 | | - } |
676 | | - } |
677 | 539 | } |
0 commit comments