jjg@1521: /* darcy@1534: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. jjg@1521: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1521: * jjg@1521: * This code is free software; you can redistribute it and/or modify it jjg@1521: * under the terms of the GNU General Public License version 2 only, as jjg@1521: * published by the Free Software Foundation. jjg@1521: * jjg@1521: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1521: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1521: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1521: * version 2 for more details (a copy is included in the LICENSE file that jjg@1521: * accompanied this code). jjg@1521: * jjg@1521: * You should have received a copy of the GNU General Public License version jjg@1521: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1521: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1521: * jjg@1521: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1521: * or visit www.oracle.com if you need additional information or have any jjg@1521: * questions. jjg@1521: */ jjg@1521: jjg@1521: /* jjg@1521: * @test jjg@1521: * @bug 8005085 8005877 8004829 8005681 8006734 8006775 jjg@1521: * @summary Combinations of Target ElementTypes on (repeated)type annotations. jjg@1521: */ jjg@1521: jjg@1521: import com.sun.tools.classfile.*; jjg@1521: import java.io.File; jjg@1521: jjg@1521: public class CombinationsTargetTest1 extends ClassfileTestHelper { jjg@1755: jjg@1755: // Test count helps identify test case in event of failure. jjg@1521: int testcount = 0; jjg@1755: jjg@1755: // Base test case template descriptions jjg@1755: enum srce { jjg@1755: src1("(repeating) type annotations at class level"), jjg@1755: src2("(repeating) type annotations on method"), jjg@1755: src3("(repeating) type annotations on wildcard, type arguments in anonymous class"), jjg@1755: src4("(repeating) type annotations on type parameters, bounds and type arguments on class decl"), jjg@1755: src5("(repeating) type annotations on type parameters, bounds and type arguments on method"), jjg@1755: src6("(repeating) type annotations on type parameters, bounds and type arguments in method"); jjg@1755: jjg@1755: String description; jjg@1755: jjg@1755: srce(String desc) { jjg@1755: this.description = this + ": " +desc; jjg@1755: } jjg@1755: } jjg@1521: jjg@1521: String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR", jjg@1521: "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"}; jjg@1521: jjg@1521: // local class tests will have an inner class. jjg@1521: Boolean hasInnerClass=false; jjg@1521: String innerClassname=""; jjg@1521: jjg@1521: public static void main(String[] args) throws Exception { jjg@1521: new CombinationsTargetTest1().run(); jjg@1521: } jjg@1521: jjg@1521: void run() throws Exception { jjg@1521: // Determines which repeat and order in source(ABMix). jjg@1521: Boolean As= false, BDs=true, ABMix=false; jjg@1521: int testrun=0; jjg@1521: Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats jjg@1521: {true,false,false}, //repeat @A jjg@1521: {false,true,false}, //repeat @B jjg@1521: {true,true,false}, //repeat both jjg@1521: {false,false,true} //repeat mix jjg@1521: }; jjg@1521: for(Boolean[] bCombo : bRepeat) { jjg@1521: As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2]; jjg@1521: for(String et : ETypes) { jjg@1521: switch(et) { jjg@1521: case "METHOD": jjg@1755: test( 8, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1); jjg@1755: test(10, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src2); jjg@1755: test( 6, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src3); jjg@1755: test(10, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src5); jjg@1755: test( 0, 8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1); jjg@1755: test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src2); jjg@1755: test( 0, 6, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src3); jjg@1755: test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src5); jjg@1521: break; jjg@1521: case "CONSTRUCTOR": jjg@1521: case "FIELD": jjg@1755: test( 8, 0, 4, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1); jjg@1755: test( 6, 0, 3, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src4); jjg@1755: test( 9, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src6); jjg@1755: test( 0, 8, 0, 4, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1); jjg@1755: test( 0, 6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4); jjg@1755: test( 0, 9, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src6); jjg@1521: break; jjg@1521: default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/ jjg@1755: test( 8, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src1); jjg@1755: test( 6, 0, 3, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src4); jjg@1755: test( 0, 8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1); jjg@1755: test( 0, 6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4); jjg@1521: } jjg@1521: } jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats, jjg@1521: Boolean BDrepeats, Boolean ABmix, String rtn, String et2, jjg@1755: Integer N, srce source) throws Exception { jjg@1521: ++testcount; jjg@1521: expected_tvisibles = tvis; jjg@1521: expected_tinvisibles = tinv; jjg@1521: expected_visibles = vis; jjg@1521: expected_invisibles = inv; jjg@1521: File testFile = null; jjg@1521: String tname="Test" + N.toString(); jjg@1521: hasInnerClass=false; jjg@1521: String testDef = "Test " + testcount + " parameters: tinv=" + tinv + jjg@1521: ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis + jjg@1521: ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats + jjg@1521: ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " + jjg@1521: et2 + ", src=" + source; jjg@1521: jjg@1521: println(testDef); jjg@1521: // Create test source and File. jjg@1521: String sourceString = sourceString(tname, rtn, et2, Arepeats, jjg@1521: BDrepeats, ABmix, source); jjg@1521: testFile = writeTestFile(tname+".java", sourceString); jjg@1521: // Compile test source and read classfile. jjg@1521: File classFile = null; jjg@1521: try { jjg@1521: classFile = compile(testFile); jjg@1521: } catch (Error err) { jjg@1521: System.err.println("Failed compile. Source:\n" + sourceString); jjg@1521: throw err; jjg@1521: } jjg@1521: //if sourcString() set hasInnerClass it also set innerClassname. jjg@1521: if(hasInnerClass) { jjg@1521: StringBuffer sb = new StringBuffer(classFile.getAbsolutePath()); jjg@1755: classFile=new File(sb.insert(sb.lastIndexOf(".class"), jjg@1755: innerClassname).toString()); jjg@1521: } jjg@1521: ClassFile cf = ClassFile.read(classFile); jjg@1521: jjg@1521: //Test class,fields and method counts. jjg@1521: test(cf); jjg@1521: jjg@1521: for (Field f : cf.fields) { jjg@1521: test(cf, f); jjg@1521: } jjg@1521: for (Method m: cf.methods) { jjg@1521: test(cf, m); jjg@1521: } jjg@1521: countAnnotations(); jjg@1521: if (errors > 0) { jjg@1521: System.err.println( testDef ); jjg@1521: System.err.println( "Source:\n" + sourceString ); jjg@1521: throw new Exception( errors + " errors found" ); jjg@1521: } jjg@1521: println("Pass"); jjg@1521: } jjg@1521: jjg@1521: // jjg@1521: // Source for test cases jjg@1521: // jjg@1521: String sourceString(String testname, String retentn, String annot2, jjg@1521: Boolean Arepeats, Boolean BDrepeats, Boolean ABmix, jjg@1755: srce src) { jjg@1521: jjg@1521: String As = "@A", Bs = "@B", Ds = "@D"; jjg@1521: if(Arepeats) As = "@A @A"; jjg@1521: if(BDrepeats) { jjg@1521: Bs = "@B @B"; jjg@1521: Ds = "@D @D"; jjg@1521: } jjg@1521: if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; } jjg@1521: jjg@1521: // Source to check for TYPE_USE and TYPE_PARAMETER annotations. jjg@1521: // Source base (annotations) is same for all test cases. jjg@1521: String source = new String(); jjg@1521: String imports = new String("import java.lang.annotation.*; \n" + jjg@1521: "import static java.lang.annotation.RetentionPolicy.*; \n" + jjg@1521: "import static java.lang.annotation.ElementType.*; \n" + jjg@1521: "import java.util.List; \n" + jjg@1521: "import java.util.HashMap; \n" + jjg@1521: "import java.util.Map; \n\n"); jjg@1521: jjg@1521: String sourceBase = new String("@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + darcy@1531: "@Repeatable( AC.class )\n" + jjg@1521: "@interface A { }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + jjg@1521: "@interface AC { A[] value(); }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + darcy@1531: "@Repeatable( BC.class )\n" + jjg@1521: "@interface B { }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + jjg@1521: "@interface BC { B[] value(); } \n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_PARAMETER,_OTHER_})\n" + jjg@1521: "@interface C { }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + darcy@1531: "@Repeatable(DC.class)\n" + jjg@1521: "@interface D { }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + jjg@1755: "@interface DC { D[] value(); }\n"); jjg@1521: jjg@1521: // Test case sources with sample generated source. jjg@1521: switch(src) { jjg@1755: case src1: // repeating type annotations at class level jjg@1521: /* jjg@1521: * @A @B class Test1 { jjg@1521: * @A @B Test1(){} jjg@1521: * @A @B Integer i1 = 0; jjg@1521: * String @A @B [] @A @B [] sa = null; jjg@1521: * // type usage in method body jjg@1521: * String test(Test1 this, String param, String ... vararg) { jjg@1521: * Object o = new String [3]; jjg@1521: * return (String) null; jjg@1521: * }} jjg@1521: */ jjg@1521: source = new String( jjg@1755: "// " + src.description + "\n" + jjg@1755: "_As_ _Bs_ class " + testname + " {\n" + jjg@1755: "_As_ _Bs_ " + testname +"(){} \n" + jjg@1755: "_As_ _Bs_ Integer i1 = 0; \n" + jjg@1755: "String _As_ _Bs_ [] _As_ _Bs_ [] sa = null; \n" + jjg@1755: "// type usage in method body \n" + jjg@1755: "String test("+testname+" this, " + jjg@1755: "String param, String ... vararg) { \n" + jjg@1755: " Object o = new String [3]; \n" + jjg@1755: " return (String) null; \n" + jjg@1755: "}\n" + jjg@1755: "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + jjg@1755: "\n"; jjg@1521: break; jjg@1755: case src2: // (repeating) type annotations on method. jjg@1521: /* jjg@1521: * class Test12 { jjg@1521: * Test12(){} jjg@1521: * // type usage on method jjg@1521: * @A @B String test(@A @B Test12 this, @A @B String param, @A @B String @A @B ... vararg) { jjg@1521: * Object o = new String [3]; jjg@1521: * return (String) null; jjg@1521: * }} jjg@1521: */ jjg@1521: source = new String( jjg@1755: "// " + src.description + "\n" + jjg@1755: "class " + testname + " {\n" + jjg@1755: testname +"(){} \n" + jjg@1755: "// type usage on method \n" + jjg@1755: "_As_ _Bs_ String test(_As_ _Bs_ "+testname+" this, " + jjg@1755: "_As_ _Bs_ String param, _As_ _Bs_ String _As_ _Bs_ ... vararg) { \n" + jjg@1755: " Object o = new String [3]; \n" + jjg@1755: " return (String) null; \n" + jjg@1755: "}\n" + jjg@1755: "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + jjg@1755: "\n"; jjg@1521: break; jjg@1755: case src3: //(repeating) annotations on wildcard, type arguments in anonymous class. jjg@1521: /* jjg@1521: * class Test13 { jjg@1521: * public T data = null; jjg@1521: * T getData() { return data;} jjg@1521: * String mtest( Test13 t){ return t.getData(); } jjg@1521: * public void test() { jjg@1755: * mtest( new Test13() { jjg@1521: * void m1(List<@A @B ? extends @A @B Object> lst) {} jjg@1521: * void m2() throws@A @B Exception { } jjg@1521: * }); jjg@1521: * } jjg@1521: * } jjg@1521: */ jjg@1521: source = new String( source + jjg@1755: "// " + src.description + "\n" + jjg@1755: "class " + testname + " {\n" + jjg@1755: " public T data = null;\n" + jjg@1755: " T getData() { return data;}\n" + jjg@1755: " String mtest( " + testname + " t){ return t.getData(); }\n" + jjg@1755: " public void test() {\n" + jjg@1755: " mtest( new " + testname + "() {\n" + jjg@1755: " void m1(List<_As_ _Bs_ ? extends _As_ _Bs_ Object> lst) {}\n" + jjg@1755: " void m2() throws_As_ _Bs_ Exception { }\n" + jjg@1755: " });\n" + jjg@1755: " }\n" + jjg@1755: "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + jjg@1755: "\n"; jjg@1755: hasInnerClass=true; jjg@1755: innerClassname="$1"; jjg@1521: break; jjg@1755: case src4: // (repeating)annotations on type parameters, bounds and type arguments on class decl. jjg@1521: /* jjg@1521: * @A @B @D jjg@1521: * class Test2<@A @B @C @D T extends @A @B Object> { jjg@1521: * Map, Integer> map = jjg@1521: * new HashMap, Integer>(); jjg@1521: * Map,Integer> map2 = new HashMap<>(); jjg@1521: * String test(Test2 this) { return null;} jjg@1521: * String genericMethod(T t) { return null; } jjg@1521: * } jjg@1521: */ jjg@1521: source = new String( source + jjg@1755: "// " + src.description + "\n" + jjg@1755: "_As_ _Bs_ _Ds_\n" + //8004829: A and B on type parameter below. jjg@1755: "class " + testname + "<_As_ _Bs_ @C _Ds_ T extends _As_ _Bs_ Object> {\n" + jjg@1755: " Map, Integer> map =\n" + jjg@1755: " new HashMap, Integer>();\n" + jjg@1755: " Map,Integer> map2 = new HashMap<>();\n" + jjg@1755: " String test(" + testname + " this) { return null;}\n" + jjg@1755: " String genericMethod(T t) { return null; }\n" + jjg@1755: "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + jjg@1755: "\n"; jjg@1755: break; jjg@1755: case src5: // (repeating) annotations on type parameters, bounds and type arguments on method. jjg@1521: /* jjg@1521: * class Test14 { jjg@1521: * Map, Integer> map = jjg@1521: * new HashMap, Integer>(); jjg@1521: * Map, Integer> map2 = new HashMap<>(); jjg@1521: * String test(@A @B Test14<@D T> this) { return null;} jjg@1521: * <@C @D T> @A @B String genericMethod(@A @B @D T t) { return null; } jjg@1521: * } jjg@1521: */ jjg@1521: source = new String( source + jjg@1755: "// " + src.description + "\n" + jjg@1755: "class " + testname + " {\n" + jjg@1755: " Map, Integer> map =\n" + jjg@1755: " new HashMap, Integer>();\n" + jjg@1755: " Map, Integer> map2 = new HashMap<>();\n" + jjg@1755: " String test(_As_ _Bs_ " + testname + "<_Ds_ T> this) { return null;}\n" + jjg@1755: " <@C _Ds_ T> _As_ _Bs_ String genericMethod(_As_ _Bs_ _Ds_ T t) { return null; }\n" + jjg@1755: "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + jjg@1755: "\n"; jjg@1755: break; jjg@1755: case src6: // repeating annotations on type parameters, bounds and type arguments in method. jjg@1521: /* jjg@1521: * class Test7{ jjg@1521: * Map, E > foo(E e) { jjg@1521: * class maptest <@A @B @D E> { jjg@1521: * Map,@A @B @D E> getMap() { jjg@1521: * return new HashMap,E>(); jjg@1521: * } jjg@1521: * } jjg@1521: * return new maptest().getMap(); jjg@1521: * } jjg@1521: * Map,String> shm = foo(new String("hello")); jjg@1521: * } jjg@1521: */ jjg@1521: source = new String( source + jjg@1755: "// " + src.description + "\n" + jjg@1755: "class "+ testname + "{\n" + jjg@1755: " Map, E > foo(E e) {\n" + jjg@1755: " class maptest <_As_ _Bs_ _Ds_ E> {\n" + // inner class $1maptest jjg@1755: " Map,_As_ _Bs_ _Ds_ E> getMap() { \n" + jjg@1755: " return new HashMap,E>();\n" + jjg@1755: " }\n" + jjg@1755: " }\n" + jjg@1755: " return new maptest().getMap();\n" + jjg@1755: " }\n" + jjg@1755: " Map,String> shm = foo(new String(\"hello\"));\n" + jjg@1755: "}\n\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + jjg@1755: "\n"; jjg@1755: hasInnerClass=true; jjg@1755: innerClassname="$1maptest"; jjg@1755: break; jjg@1521: } jjg@1521: return imports + source; jjg@1521: } jjg@1521: }