Thu, 31 Jan 2013 12:16:03 -0800
8007313: Remove use of {ContainerFor/ContainedBy} from langtools
Reviewed-by: jjg
jjg@1521 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. |
jjg@1521 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1521 | 4 | * |
jjg@1521 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1521 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1521 | 7 | * published by the Free Software Foundation. |
jjg@1521 | 8 | * |
jjg@1521 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1521 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1521 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1521 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1521 | 13 | * accompanied this code). |
jjg@1521 | 14 | * |
jjg@1521 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1521 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1521 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1521 | 18 | * |
jjg@1521 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1521 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1521 | 21 | * questions. |
jjg@1521 | 22 | */ |
jjg@1521 | 23 | |
jjg@1521 | 24 | /* |
jjg@1521 | 25 | * @test |
jjg@1521 | 26 | * @bug 8005085 8005877 8004829 8005681 8006734 8006775 |
jjg@1521 | 27 | * @ignore |
jjg@1521 | 28 | * @summary Combinations of Target ElementTypes on (repeated)type annotations. |
jjg@1521 | 29 | */ |
jjg@1521 | 30 | |
jjg@1521 | 31 | import com.sun.tools.classfile.*; |
jjg@1521 | 32 | import java.io.File; |
jjg@1521 | 33 | |
jjg@1521 | 34 | public class CombinationsTargetTest2 extends ClassfileTestHelper { |
jjg@1521 | 35 | // Helps identify test case in event of failure. |
jjg@1521 | 36 | int testcount = 0; |
jjg@1521 | 37 | int src3 = 3, src8 = 8, src9 = 9; |
jjg@1521 | 38 | |
jjg@1521 | 39 | String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR", |
jjg@1521 | 40 | "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"}; |
jjg@1521 | 41 | |
jjg@1521 | 42 | // local class tests will have an inner class. |
jjg@1521 | 43 | Boolean hasInnerClass=false; |
jjg@1521 | 44 | String innerClassname=""; |
jjg@1521 | 45 | |
jjg@1521 | 46 | public static void main(String[] args) throws Exception { |
jjg@1521 | 47 | new CombinationsTargetTest2().run(); |
jjg@1521 | 48 | } |
jjg@1521 | 49 | |
jjg@1521 | 50 | void run() throws Exception { |
jjg@1521 | 51 | // Determines which repeat and order in source(ABMix). |
jjg@1521 | 52 | Boolean As= false, BDs=true, ABMix=false; |
jjg@1521 | 53 | int testrun=0; |
jjg@1521 | 54 | // A repeats and/or B/D repeats, ABMix for order of As and Bs. |
jjg@1521 | 55 | Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats |
jjg@1521 | 56 | {true,false,false}, //repeat @A |
jjg@1521 | 57 | {false,true,false}, //repeat @B |
jjg@1521 | 58 | {true,true,false}, //repeat both |
jjg@1521 | 59 | {false,false,true} //repeat mix |
jjg@1521 | 60 | }; |
jjg@1521 | 61 | for(Boolean[] bCombo : bRepeat) { |
jjg@1521 | 62 | As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2]; |
jjg@1521 | 63 | for(String et : ETypes) { |
jjg@1521 | 64 | switch(et) { |
jjg@1521 | 65 | case "METHOD": |
jjg@1521 | 66 | test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src3); |
jjg@1521 | 67 | test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src3); |
jjg@1521 | 68 | break; |
jjg@1521 | 69 | case "CONSTRUCTOR": |
jjg@1521 | 70 | case "FIELD": |
jjg@1521 | 71 | test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src3); |
jjg@1521 | 72 | test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src8); |
jjg@1521 | 73 | test( 6, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src9); |
jjg@1521 | 74 | test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src3); |
jjg@1521 | 75 | test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src8); |
jjg@1521 | 76 | test( 0, 6, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src9); |
jjg@1521 | 77 | break; |
jjg@1521 | 78 | default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/ |
jjg@1521 | 79 | break; |
jjg@1521 | 80 | } |
jjg@1521 | 81 | } |
jjg@1521 | 82 | } |
jjg@1521 | 83 | } |
jjg@1521 | 84 | |
jjg@1521 | 85 | public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats, |
jjg@1521 | 86 | Boolean BDrepeats, Boolean ABmix, String rtn, String et2, |
jjg@1521 | 87 | Integer N, int source) throws Exception { |
jjg@1521 | 88 | ++testcount; |
jjg@1521 | 89 | expected_tvisibles = tvis; |
jjg@1521 | 90 | expected_tinvisibles = tinv; |
jjg@1521 | 91 | expected_visibles = vis; |
jjg@1521 | 92 | expected_invisibles = inv; |
jjg@1521 | 93 | File testFile = null; |
jjg@1521 | 94 | String tname="Test" + N.toString(); |
jjg@1521 | 95 | hasInnerClass=false; |
jjg@1521 | 96 | String testDef = "Test " + testcount + " parameters: tinv=" + tinv + |
jjg@1521 | 97 | ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis + |
jjg@1521 | 98 | ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats + |
jjg@1521 | 99 | ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " + |
jjg@1521 | 100 | et2 + ", src=" + source; |
jjg@1521 | 101 | |
jjg@1521 | 102 | // Uncomment this block to run the tests but skip failing scenarios. |
jjg@1521 | 103 | // // 8005681 - skip cases with repeated annotations on new, array, cast. |
jjg@1521 | 104 | // if((source==3 || source==8 || source==9) && (ABmix || (Arepeats && BDrepeats))) { |
jjg@1521 | 105 | // System.out.println(testDef+"\n8005681-skip repeated annotations on new,array,cast"); |
jjg@1521 | 106 | // return; |
jjg@1521 | 107 | // } |
jjg@1521 | 108 | |
jjg@1521 | 109 | println(testDef); |
jjg@1521 | 110 | // Create test source and File. |
jjg@1521 | 111 | String sourceString = sourceString(tname, rtn, et2, Arepeats, |
jjg@1521 | 112 | BDrepeats, ABmix, source); |
jjg@1521 | 113 | testFile = writeTestFile(tname+".java", sourceString); |
jjg@1521 | 114 | // Compile test source and read classfile. |
jjg@1521 | 115 | File classFile = null; |
jjg@1521 | 116 | try { |
jjg@1521 | 117 | classFile = compile(testFile); |
jjg@1521 | 118 | } catch (Error err) { |
jjg@1521 | 119 | System.err.println("Failed compile. Source:\n" + sourceString); |
jjg@1521 | 120 | throw err; |
jjg@1521 | 121 | } |
jjg@1521 | 122 | //if sourcString() set hasInnerClass it also set innerClassname. |
jjg@1521 | 123 | if(hasInnerClass) { |
jjg@1521 | 124 | StringBuffer sb = new StringBuffer(classFile.getAbsolutePath()); |
jjg@1521 | 125 | classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString()); |
jjg@1521 | 126 | } |
jjg@1521 | 127 | ClassFile cf = ClassFile.read(classFile); |
jjg@1521 | 128 | |
jjg@1521 | 129 | //Test class,fields and method counts. |
jjg@1521 | 130 | test(cf); |
jjg@1521 | 131 | |
jjg@1521 | 132 | for (Field f : cf.fields) { |
jjg@1521 | 133 | test(cf, f); |
jjg@1521 | 134 | } |
jjg@1521 | 135 | for (Method m: cf.methods) { |
jjg@1521 | 136 | test(cf, m); |
jjg@1521 | 137 | } |
jjg@1521 | 138 | countAnnotations(); |
jjg@1521 | 139 | if (errors > 0) { |
jjg@1521 | 140 | System.err.println( testDef ); |
jjg@1521 | 141 | System.err.println( "Source:\n" + sourceString ); |
jjg@1521 | 142 | throw new Exception( errors + " errors found" ); |
jjg@1521 | 143 | } |
jjg@1521 | 144 | println("Pass"); |
jjg@1521 | 145 | } |
jjg@1521 | 146 | |
jjg@1521 | 147 | // |
jjg@1521 | 148 | // Source for test cases |
jjg@1521 | 149 | // |
jjg@1521 | 150 | String sourceString(String testname, String retentn, String annot2, |
jjg@1521 | 151 | Boolean Arepeats, Boolean BDrepeats, Boolean ABmix, |
jjg@1521 | 152 | int src) { |
jjg@1521 | 153 | |
jjg@1521 | 154 | String As = "@A", Bs = "@B", Ds = "@D"; |
jjg@1521 | 155 | if(Arepeats) As = "@A @A"; |
jjg@1521 | 156 | if(BDrepeats) { |
jjg@1521 | 157 | Bs = "@B @B"; |
jjg@1521 | 158 | Ds = "@D @D"; |
jjg@1521 | 159 | } |
jjg@1521 | 160 | if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; } |
jjg@1521 | 161 | |
jjg@1521 | 162 | // Source to check for TYPE_USE and TYPE_PARAMETER annotations. |
jjg@1521 | 163 | // Source base (annotations) is same for all test cases. |
jjg@1521 | 164 | String source = new String(); |
jjg@1521 | 165 | String imports = new String("import java.lang.annotation.*; \n" + |
jjg@1521 | 166 | "import static java.lang.annotation.RetentionPolicy.*; \n" + |
jjg@1521 | 167 | "import static java.lang.annotation.ElementType.*; \n" + |
jjg@1521 | 168 | "import java.util.List; \n" + |
jjg@1521 | 169 | "import java.util.HashMap; \n" + |
jjg@1521 | 170 | "import java.util.Map; \n\n"); |
jjg@1521 | 171 | |
jjg@1521 | 172 | String sourceBase = new String("@Retention("+retentn+")\n" + |
jjg@1521 | 173 | "@Target({TYPE_USE,_OTHER_})\n" + |
darcy@1531 | 174 | "@Repeatable( AC.class )\n" + |
jjg@1521 | 175 | "@interface A { }\n\n" + |
jjg@1521 | 176 | |
jjg@1521 | 177 | "@Retention("+retentn+")\n" + |
jjg@1521 | 178 | "@Target({TYPE_USE,_OTHER_})\n" + |
jjg@1521 | 179 | "@interface AC { A[] value(); }\n\n" + |
jjg@1521 | 180 | |
jjg@1521 | 181 | "@Retention("+retentn+")\n" + |
jjg@1521 | 182 | "@Target({TYPE_USE,_OTHER_})\n" + |
darcy@1531 | 183 | "@Repeatable( BC.class )\n" + |
jjg@1521 | 184 | "@interface B { }\n\n" + |
jjg@1521 | 185 | |
jjg@1521 | 186 | "@Retention("+retentn+")\n" + |
jjg@1521 | 187 | "@Target({TYPE_USE,_OTHER_})\n" + |
jjg@1521 | 188 | "@interface BC { B[] value(); } \n\n" + |
jjg@1521 | 189 | |
jjg@1521 | 190 | "@Retention("+retentn+")\n" + |
jjg@1521 | 191 | "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + |
darcy@1531 | 192 | "@Repeatable(DC.class)\n" + |
jjg@1521 | 193 | "@interface D { }\n\n" + |
jjg@1521 | 194 | |
jjg@1521 | 195 | "@Retention("+retentn+")\n" + |
jjg@1521 | 196 | "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + |
jjg@1521 | 197 | "@interface DC { D[] value(); }\n\n"); |
jjg@1521 | 198 | |
jjg@1521 | 199 | // Test case sources with sample generated source |
jjg@1521 | 200 | switch(src) { |
jjg@1521 | 201 | case 3: // (repeating) type annotations on field in method body |
jjg@1521 | 202 | /* |
jjg@1521 | 203 | * class Test1 { |
jjg@1521 | 204 | * Test1(){} |
jjg@1521 | 205 | * // type usage in method body |
jjg@1521 | 206 | * String test(Test1 this, String param, String ... vararg) { |
jjg@1521 | 207 | * @A @B |
jjg@1521 | 208 | * Object o = new @A @B String @A @B [3]; |
jjg@1521 | 209 | * return (@A @B String) null; |
jjg@1521 | 210 | * }} |
jjg@1521 | 211 | */ |
jjg@1521 | 212 | source = new String( |
jjg@1521 | 213 | "class " + testname + " {\n" + |
jjg@1521 | 214 | "" + testname +"(){} \n" + |
jjg@1521 | 215 | "// type usage in method body \n" + |
jjg@1521 | 216 | "String test("+testname+" this, " + |
jjg@1521 | 217 | "String param, String ... vararg) { \n" + |
jjg@1521 | 218 | " _As_ _Bs_\n Object o = new _As_ _Bs_ String _As_ _Bs_ [3]; \n" + |
jjg@1521 | 219 | " return (_As_ _Bs_ String) null; \n" + |
jjg@1521 | 220 | "} \n" + |
jjg@1521 | 221 | "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + |
jjg@1521 | 222 | "\n\n"; |
jjg@1521 | 223 | break; |
jjg@1521 | 224 | case 8: // (repeating) annotations on type parameters, bounds and type arguments in new statement. |
jjg@1521 | 225 | /* |
jjg@1521 | 226 | * class Test2<T extends Object> { |
jjg@1521 | 227 | * Map<List<String>, Integer> map = |
jjg@1521 | 228 | * new HashMap<@A @B List<@A @B String>, @A @B Integer>(); |
jjg@1521 | 229 | * Map<List<String>, Integer> map2 = new @A @B HashMap<>(); |
jjg@1521 | 230 | * String test(Test2<T> this) { return null;} |
jjg@1521 | 231 | * <T> String genericMethod(T t) { return null; } |
jjg@1521 | 232 | * } |
jjg@1521 | 233 | */ |
jjg@1521 | 234 | source = new String( source + |
jjg@1521 | 235 | "// (repeating) annotations on type parameters, bounds and type arguments. \n" + |
jjg@1521 | 236 | "class " + testname + "<T extends Object> {\n" + |
jjg@1521 | 237 | " Map<List<String>, Integer> map =\n" + |
jjg@1521 | 238 | " new HashMap<_As_ _Bs_ List<_As_ _Bs_ String>, _As_ _Bs_ Integer>();\n" + |
jjg@1521 | 239 | " Map<List<String>, Integer> map2 = new _As_ _Bs_ HashMap<>();\n" + |
jjg@1521 | 240 | " String test(" + testname + "<T> this) { return null;}\n" + |
jjg@1521 | 241 | " <T> String genericMethod(T t) { return null; }\n" + |
jjg@1521 | 242 | "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + |
jjg@1521 | 243 | "\n\n"; |
jjg@1521 | 244 | break; |
jjg@1521 | 245 | case 9: // (repeating)annotations on type parameters of class, method return value in method. |
jjg@1521 | 246 | /* |
jjg@1521 | 247 | * class Test3{ |
jjg@1521 | 248 | * <E extends Comparable> Map<List<E>, E > foo(E e) { |
jjg@1521 | 249 | * class maptest <E> { |
jjg@1521 | 250 | * Map<List<E>,E> getMap() { |
jjg@1521 | 251 | * Map<List<E>,E> Em = new HashMap<List<@A @B @D E>,@A @B @D E>(); |
jjg@1521 | 252 | * return Em; |
jjg@1521 | 253 | * } |
jjg@1521 | 254 | * } |
jjg@1521 | 255 | * return new maptest<E>().getMap(); |
jjg@1521 | 256 | * } |
jjg@1521 | 257 | * Map<List<String>,String> shm = foo(new String("hello")); |
jjg@1521 | 258 | * } |
jjg@1521 | 259 | */ |
jjg@1521 | 260 | source = new String( source + |
jjg@1521 | 261 | "// (repeating)annotations on type parameters of class, method return value in method. \n" + |
jjg@1521 | 262 | "class "+ testname + "{\n" + |
jjg@1521 | 263 | " <E extends Comparable> Map<List<E>, E > foo(E e) {\n" + |
jjg@1521 | 264 | " class maptest <E> {\n" + // inner class $1maptest |
jjg@1521 | 265 | " Map<List<E>,E> getMap() { \n" + |
jjg@1521 | 266 | " Map<List<E>,E> Em = new HashMap<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E>();\n" + |
jjg@1521 | 267 | " return Em;\n" + |
jjg@1521 | 268 | " }\n" + |
jjg@1521 | 269 | " }\n" + |
jjg@1521 | 270 | " return new maptest<E>().getMap();\n" + |
jjg@1521 | 271 | " }\n" + |
jjg@1521 | 272 | " Map<List<String>,String> shm = foo(new String(\"hello\"));\n" + |
jjg@1521 | 273 | "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + |
jjg@1521 | 274 | "\n\n"; |
jjg@1521 | 275 | hasInnerClass=true; |
jjg@1521 | 276 | innerClassname="$1maptest"; |
jjg@1521 | 277 | break; |
jjg@1521 | 278 | |
jjg@1521 | 279 | } |
jjg@1521 | 280 | return imports + source; |
jjg@1521 | 281 | } |
jjg@1521 | 282 | } |