Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 8005085 8008762 8008751 8013065 8015323 8015257 |
aoqi@0 | 27 | * @summary Type annotations on anonymous and inner class. |
aoqi@0 | 28 | * Six TYPE_USE annotations are repeated(or not); Four combinations create |
aoqi@0 | 29 | * four test files, and each results in the test class and 2 anonymous classes. |
aoqi@0 | 30 | * Each element of these three classes is checked for expected number of the |
aoqi@0 | 31 | * four annotation Attributes. Expected annotation counts depend on type of |
aoqi@0 | 32 | * annotation place on type of element (a FIELD&TYPE_USE element on a field |
aoqi@0 | 33 | * results in 2). Elements with no annotations expect 0. |
aoqi@0 | 34 | * Source template is read in from testanoninner.template |
aoqi@0 | 35 | * |
aoqi@0 | 36 | */ |
aoqi@0 | 37 | import java.lang.annotation.*; |
aoqi@0 | 38 | import java.io.*; |
aoqi@0 | 39 | import java.util.List; |
aoqi@0 | 40 | import java.util.LinkedList; |
aoqi@0 | 41 | import com.sun.tools.classfile.*; |
aoqi@0 | 42 | import java.nio.file.Files; |
aoqi@0 | 43 | import java.nio.charset.*; |
aoqi@0 | 44 | import java.io.File; |
aoqi@0 | 45 | import java.io.IOException; |
aoqi@0 | 46 | |
aoqi@0 | 47 | |
aoqi@0 | 48 | import java.lang.annotation.*; |
aoqi@0 | 49 | import static java.lang.annotation.RetentionPolicy.*; |
aoqi@0 | 50 | import static java.lang.annotation.ElementType.*; |
aoqi@0 | 51 | |
aoqi@0 | 52 | /* |
aoqi@0 | 53 | * A source template is read in and testname and annotations are inserted |
aoqi@0 | 54 | * via replace(). |
aoqi@0 | 55 | */ |
aoqi@0 | 56 | public class TestAnonInnerClasses extends ClassfileTestHelper { |
aoqi@0 | 57 | // tally errors and test cases |
aoqi@0 | 58 | int errors = 0; |
aoqi@0 | 59 | int checks = 0; |
aoqi@0 | 60 | //Note expected test count in case of skips due to bugs. |
aoqi@0 | 61 | int tc = 0, xtc = 180; // 45 x 4 variations of repeated annotations. |
aoqi@0 | 62 | File testSrc = new File(System.getProperty("test.src")); |
aoqi@0 | 63 | |
aoqi@0 | 64 | String[] AnnoAttributes = { |
aoqi@0 | 65 | Attribute.RuntimeVisibleTypeAnnotations, |
aoqi@0 | 66 | Attribute.RuntimeInvisibleTypeAnnotations, |
aoqi@0 | 67 | Attribute.RuntimeVisibleAnnotations, |
aoqi@0 | 68 | Attribute.RuntimeInvisibleAnnotations |
aoqi@0 | 69 | }; |
aoqi@0 | 70 | |
aoqi@0 | 71 | // template for source files |
aoqi@0 | 72 | String srcTemplate = "testanoninner.template"; |
aoqi@0 | 73 | |
aoqi@0 | 74 | // Four test files generated based on combinations of repeating annotations. |
aoqi@0 | 75 | Boolean As= false, Bs=true, Cs=false, Ds=false, TAs=false,TBs=false; |
aoqi@0 | 76 | Boolean[][] bRepeat = new Boolean[][]{ |
aoqi@0 | 77 | /* no repeats */ {false, false, false, false, false, false}, |
aoqi@0 | 78 | /* repeat A,C,TA */ {true, false, true, false, true, false}, |
aoqi@0 | 79 | /* repeat B,D,TB */ {false, true, false, true, false, true}, |
aoqi@0 | 80 | /* repeat all */ {true, true, true, true, true, true} |
aoqi@0 | 81 | }; |
aoqi@0 | 82 | // Save descriptions of failed test case; does not terminate upon a failure. |
aoqi@0 | 83 | List<String> failed = new LinkedList<>(); |
aoqi@0 | 84 | |
aoqi@0 | 85 | public static void main(String[] args) throws Exception { |
aoqi@0 | 86 | new TestAnonInnerClasses().run(); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | // Check annotation counts and make reports sufficiently descriptive to |
aoqi@0 | 90 | // easily diagnose. |
aoqi@0 | 91 | void check(String testcase, int vtaX, int itaX, int vaX, int iaX, |
aoqi@0 | 92 | int vtaA, int itaA, int vaA, int iaA) { |
aoqi@0 | 93 | |
aoqi@0 | 94 | String descr = " checking " + testcase+" _TYPE_, expected: " + |
aoqi@0 | 95 | vtaX + ", " + itaX + ", " + vaX + ", " + iaX + "; actual: " + |
aoqi@0 | 96 | vtaA + ", " + itaA + ", " + vaA + ", " + iaA; |
aoqi@0 | 97 | String description; |
aoqi@0 | 98 | description=descr.replace("_TYPE_","RuntimeVisibleTypeAnnotations"); |
aoqi@0 | 99 | if (vtaX != vtaA) { |
aoqi@0 | 100 | errors++; |
aoqi@0 | 101 | failed.add(++checks + " " + testcase + ": (vtaX) " + vtaX + |
aoqi@0 | 102 | " != " + vtaA + " (vtaA)"); |
aoqi@0 | 103 | println(checks + " FAIL: " + description); |
aoqi@0 | 104 | } else { |
aoqi@0 | 105 | println(++checks + " PASS: " + description); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | description=descr.replace("_TYPE_","RuntimeInvisibleTypeAnnotations"); |
aoqi@0 | 108 | if (itaX != itaA) { |
aoqi@0 | 109 | errors++; |
aoqi@0 | 110 | failed.add(++checks + " " + testcase + ": (itaX) " + itaX + " != " + |
aoqi@0 | 111 | itaA + " (itaA)"); |
aoqi@0 | 112 | println(checks + " FAIL: " + description); |
aoqi@0 | 113 | } else { |
aoqi@0 | 114 | println(++checks + " PASS: " + description); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | description=descr.replace("_TYPE_","RuntimeVisibleAnnotations"); |
aoqi@0 | 117 | if (vaX != vaA) { |
aoqi@0 | 118 | errors++; |
aoqi@0 | 119 | failed.add(++checks + " " + testcase + ": (vaX) " + vaX + " != " + |
aoqi@0 | 120 | vaA + " (vaA)"); |
aoqi@0 | 121 | println(checks + " FAIL: " + description); |
aoqi@0 | 122 | } else { |
aoqi@0 | 123 | println(++checks + " PASS: " + description); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | description=descr.replace("_TYPE_","RuntimeInvisibleAnnotations"); |
aoqi@0 | 126 | if (iaX != iaA) { |
aoqi@0 | 127 | errors++; |
aoqi@0 | 128 | failed.add(++checks + " " + testcase + ": (iaX) " + iaX + " != " + |
aoqi@0 | 129 | iaA + " (iaA)"); |
aoqi@0 | 130 | println(checks + " FAIL: " + description); |
aoqi@0 | 131 | } else { |
aoqi@0 | 132 | println(++checks + " PASS: " + description); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | println(""); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | // Print failed cases (if any) and throw exception for fail. |
aoqi@0 | 138 | void report() { |
aoqi@0 | 139 | if (errors!=0) { |
aoqi@0 | 140 | System.err.println("Failed tests: " + errors + |
aoqi@0 | 141 | "\nfailed test cases:\n"); |
aoqi@0 | 142 | for (String t: failed) System.err.println(" " + t); |
aoqi@0 | 143 | throw new RuntimeException("FAIL: There were test failures."); |
aoqi@0 | 144 | } else |
aoqi@0 | 145 | System.out.println("PASSED all tests."); |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | void test(String ttype, ClassFile cf, Method m, Field f, boolean visible) { |
aoqi@0 | 149 | int vtaActual = 0, |
aoqi@0 | 150 | itaActual = 0, |
aoqi@0 | 151 | vaActual = 0, |
aoqi@0 | 152 | iaActual = 0, |
aoqi@0 | 153 | vtaExp = 0, |
aoqi@0 | 154 | itaExp = 0, |
aoqi@0 | 155 | vaExp = 0, |
aoqi@0 | 156 | iaExp = 0, |
aoqi@0 | 157 | index = 0, |
aoqi@0 | 158 | index2 = 0; |
aoqi@0 | 159 | String memberName = null, |
aoqi@0 | 160 | testcase = "undefined", |
aoqi@0 | 161 | testClassName = null; |
aoqi@0 | 162 | Attribute attr = null, |
aoqi@0 | 163 | cattr = null; |
aoqi@0 | 164 | Code_attribute CAttr = null; |
aoqi@0 | 165 | // Get counts of 4 annotation Attributes on element being checked. |
aoqi@0 | 166 | for (String AnnoType : AnnoAttributes) { |
aoqi@0 | 167 | try { |
aoqi@0 | 168 | switch (ttype) { |
aoqi@0 | 169 | case "METHOD": |
aoqi@0 | 170 | index = m.attributes.getIndex(cf.constant_pool, |
aoqi@0 | 171 | AnnoType); |
aoqi@0 | 172 | memberName = m.getName(cf.constant_pool); |
aoqi@0 | 173 | if (index != -1) |
aoqi@0 | 174 | attr = m.attributes.get(index); |
aoqi@0 | 175 | //fetch index annotations from code attribute. |
aoqi@0 | 176 | index2 = m.attributes.getIndex(cf.constant_pool, |
aoqi@0 | 177 | Attribute.Code); |
aoqi@0 | 178 | if (index2 != -1) { |
aoqi@0 | 179 | cattr = m.attributes.get(index2); |
aoqi@0 | 180 | assert cattr instanceof Code_attribute; |
aoqi@0 | 181 | CAttr = (Code_attribute)cattr; |
aoqi@0 | 182 | index2 = CAttr.attributes.getIndex(cf.constant_pool, |
aoqi@0 | 183 | AnnoType); |
aoqi@0 | 184 | if (index2 != -1) |
aoqi@0 | 185 | cattr = CAttr.attributes.get(index2); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | break; |
aoqi@0 | 188 | case "FIELD": |
aoqi@0 | 189 | index = f.attributes.getIndex(cf.constant_pool, |
aoqi@0 | 190 | AnnoType); |
aoqi@0 | 191 | memberName = f.getName(cf.constant_pool); |
aoqi@0 | 192 | if (index != -1) |
aoqi@0 | 193 | attr = f.attributes.get(index); |
aoqi@0 | 194 | //fetch index annotations from code attribute. |
aoqi@0 | 195 | index2 = cf.attributes.getIndex(cf.constant_pool, |
aoqi@0 | 196 | Attribute.Code); |
aoqi@0 | 197 | if (index2!= -1) { |
aoqi@0 | 198 | cattr = cf.attributes.get(index2); |
aoqi@0 | 199 | assert cattr instanceof Code_attribute; |
aoqi@0 | 200 | CAttr = (Code_attribute)cattr; |
aoqi@0 | 201 | index2 = CAttr.attributes.getIndex(cf.constant_pool, |
aoqi@0 | 202 | AnnoType); |
aoqi@0 | 203 | if (index2!= -1) |
aoqi@0 | 204 | cattr = CAttr.attributes.get(index2); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | break; |
aoqi@0 | 207 | |
aoqi@0 | 208 | default: |
aoqi@0 | 209 | memberName = cf.getName(); |
aoqi@0 | 210 | index = cf.attributes.getIndex(cf.constant_pool, |
aoqi@0 | 211 | AnnoType); |
aoqi@0 | 212 | if (index!= -1) attr = cf.attributes.get(index); |
aoqi@0 | 213 | break; |
aoqi@0 | 214 | } |
aoqi@0 | 215 | } |
aoqi@0 | 216 | catch (ConstantPoolException cpe) { cpe.printStackTrace(); } |
aoqi@0 | 217 | try { |
aoqi@0 | 218 | testClassName=cf.getName(); |
aoqi@0 | 219 | testcase = ttype + ": " + testClassName + ": " + |
aoqi@0 | 220 | memberName + ", "; |
aoqi@0 | 221 | } |
aoqi@0 | 222 | catch (ConstantPoolException cpe) { cpe.printStackTrace(); } |
aoqi@0 | 223 | if (index != -1) { |
aoqi@0 | 224 | switch (AnnoType) { |
aoqi@0 | 225 | case Attribute.RuntimeVisibleTypeAnnotations: |
aoqi@0 | 226 | //count RuntimeVisibleTypeAnnotations |
aoqi@0 | 227 | RuntimeVisibleTypeAnnotations_attribute RVTAa = |
aoqi@0 | 228 | (RuntimeVisibleTypeAnnotations_attribute)attr; |
aoqi@0 | 229 | vtaActual += RVTAa.annotations.length; |
aoqi@0 | 230 | break; |
aoqi@0 | 231 | case Attribute.RuntimeVisibleAnnotations: |
aoqi@0 | 232 | //count RuntimeVisibleAnnotations |
aoqi@0 | 233 | RuntimeVisibleAnnotations_attribute RVAa = |
aoqi@0 | 234 | (RuntimeVisibleAnnotations_attribute)attr; |
aoqi@0 | 235 | vaActual += RVAa.annotations.length; |
aoqi@0 | 236 | break; |
aoqi@0 | 237 | case Attribute.RuntimeInvisibleTypeAnnotations: |
aoqi@0 | 238 | //count RuntimeInvisibleTypeAnnotations |
aoqi@0 | 239 | RuntimeInvisibleTypeAnnotations_attribute RITAa = |
aoqi@0 | 240 | (RuntimeInvisibleTypeAnnotations_attribute)attr; |
aoqi@0 | 241 | itaActual += RITAa.annotations.length; |
aoqi@0 | 242 | break; |
aoqi@0 | 243 | case Attribute.RuntimeInvisibleAnnotations: |
aoqi@0 | 244 | //count RuntimeInvisibleAnnotations |
aoqi@0 | 245 | RuntimeInvisibleAnnotations_attribute RIAa = |
aoqi@0 | 246 | (RuntimeInvisibleAnnotations_attribute)attr; |
aoqi@0 | 247 | iaActual += RIAa.annotations.length; |
aoqi@0 | 248 | break; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | } |
aoqi@0 | 251 | // annotations from code attribute. |
aoqi@0 | 252 | if (index2 != -1) { |
aoqi@0 | 253 | switch (AnnoType) { |
aoqi@0 | 254 | case Attribute.RuntimeVisibleTypeAnnotations: |
aoqi@0 | 255 | //count RuntimeVisibleTypeAnnotations |
aoqi@0 | 256 | RuntimeVisibleTypeAnnotations_attribute RVTAa = |
aoqi@0 | 257 | (RuntimeVisibleTypeAnnotations_attribute)cattr; |
aoqi@0 | 258 | vtaActual += RVTAa.annotations.length; |
aoqi@0 | 259 | break; |
aoqi@0 | 260 | case Attribute.RuntimeVisibleAnnotations: |
aoqi@0 | 261 | //count RuntimeVisibleAnnotations |
aoqi@0 | 262 | RuntimeVisibleAnnotations_attribute RVAa = |
aoqi@0 | 263 | (RuntimeVisibleAnnotations_attribute)cattr; |
aoqi@0 | 264 | vaActual += RVAa.annotations.length; |
aoqi@0 | 265 | break; |
aoqi@0 | 266 | case Attribute.RuntimeInvisibleTypeAnnotations: |
aoqi@0 | 267 | //count RuntimeInvisibleTypeAnnotations |
aoqi@0 | 268 | RuntimeInvisibleTypeAnnotations_attribute RITAa = |
aoqi@0 | 269 | (RuntimeInvisibleTypeAnnotations_attribute)cattr; |
aoqi@0 | 270 | itaActual += RITAa.annotations.length; |
aoqi@0 | 271 | break; |
aoqi@0 | 272 | case Attribute.RuntimeInvisibleAnnotations: |
aoqi@0 | 273 | //count RuntimeInvisibleAnnotations |
aoqi@0 | 274 | RuntimeInvisibleAnnotations_attribute RIAa = |
aoqi@0 | 275 | (RuntimeInvisibleAnnotations_attribute)cattr; |
aoqi@0 | 276 | iaActual += RIAa.annotations.length; |
aoqi@0 | 277 | break; |
aoqi@0 | 278 | } |
aoqi@0 | 279 | } |
aoqi@0 | 280 | } |
aoqi@0 | 281 | |
aoqi@0 | 282 | switch (memberName) { |
aoqi@0 | 283 | //METHODs |
aoqi@0 | 284 | case "test" : vtaExp=4; itaExp=4; vaExp=0; iaExp=0; tc++; break; |
aoqi@0 | 285 | case "mtest": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 286 | case "m1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 287 | case "m2": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 288 | case "m3": vtaExp=10; itaExp=10; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 289 | case "tm": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 290 | //inner class |
aoqi@0 | 291 | case "i_m1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 292 | case "i_m2": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 293 | case "i_um": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 294 | //local class |
aoqi@0 | 295 | case "l_m1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 296 | case "l_m2": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 297 | case "l_um": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 298 | //anon class |
aoqi@0 | 299 | case "mm_m1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 300 | case "mm_m2": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 301 | case "mm_m3": vtaExp=10; itaExp=10;vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 302 | case "mm_tm": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 303 | //InnerAnon class |
aoqi@0 | 304 | case "ia_m1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 305 | case "ia_m2": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 306 | case "ia_um": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 307 | //FIELDs |
aoqi@0 | 308 | case "data": vtaExp = 2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 309 | case "odata1": vtaExp = 2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 310 | case "pdata1": vtaExp = 2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 311 | case "tdata": vtaExp = 2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 312 | case "sa1": vtaExp = 6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 313 | //inner class |
aoqi@0 | 314 | case "i_odata1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 315 | case "i_pdata1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 316 | case "i_udata": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 317 | case "i_sa1": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 318 | case "i_tdata": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 319 | //local class |
aoqi@0 | 320 | case "l_odata1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 321 | case "l_pdata1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 322 | case "l_udata": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 323 | case "l_sa1": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 324 | case "l_tdata": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 325 | //anon class |
aoqi@0 | 326 | case "mm_odata1": vtaExp = 2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 327 | case "mm_pdata1": vtaExp = 2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 328 | case "mm_sa1": vtaExp = 6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 329 | case "mm_tdata": vtaExp = 2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 330 | // InnerAnon class |
aoqi@0 | 331 | case "ia_odata1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 332 | case "ia_pdata1": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 333 | case "ia_udata": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 334 | case "ia_sa1": vtaExp=6; itaExp=6; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 335 | case "ia_tdata": vtaExp=2; itaExp=2; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 336 | case "IA": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 337 | case "IN": vtaExp=4; itaExp=4; vaExp=1; iaExp=1; tc++; break; |
aoqi@0 | 338 | // default cases are <init>, this$0, this$1, mmtest, atest |
aoqi@0 | 339 | default: vtaExp = 0; itaExp=0; vaExp=0; iaExp=0; break; |
aoqi@0 | 340 | } |
aoqi@0 | 341 | check(testcase,vtaExp, itaExp, vaExp, iaExp, |
aoqi@0 | 342 | vtaActual,itaActual,vaActual,iaActual); |
aoqi@0 | 343 | } |
aoqi@0 | 344 | |
aoqi@0 | 345 | public void run() { |
aoqi@0 | 346 | ClassFile cf = null; |
aoqi@0 | 347 | InputStream in = null; |
aoqi@0 | 348 | int testcount = 1; |
aoqi@0 | 349 | File testFile = null; |
aoqi@0 | 350 | // Generate source, check methods and fields for each combination. |
aoqi@0 | 351 | for (Boolean[] bCombo : bRepeat) { |
aoqi@0 | 352 | As=bCombo[0]; Bs=bCombo[1]; Cs=bCombo[2]; |
aoqi@0 | 353 | Ds=bCombo[3]; TAs=bCombo[4]; TBs=bCombo[5]; |
aoqi@0 | 354 | String testname = "Test" + testcount++; |
aoqi@0 | 355 | println("Combinations: " + As + ", " + Bs + ", " + Cs + ", " + Ds + |
aoqi@0 | 356 | ", " + TAs + ", " + TBs + |
aoqi@0 | 357 | "; see " + testname + ".java"); |
aoqi@0 | 358 | String[] classes = {testname + ".class", |
aoqi@0 | 359 | testname + "$Inner.class", |
aoqi@0 | 360 | testname + "$1Local1.class", |
aoqi@0 | 361 | testname + "$1.class", |
aoqi@0 | 362 | testname + "$1$1.class", |
aoqi@0 | 363 | testname + "$1$InnerAnon.class" |
aoqi@0 | 364 | }; |
aoqi@0 | 365 | // Create test source, create and compile File. |
aoqi@0 | 366 | String sourceString = getSource(srcTemplate, testname, |
aoqi@0 | 367 | As, Bs, Cs, Ds, TAs, TBs); |
aoqi@0 | 368 | System.out.println(sourceString); |
aoqi@0 | 369 | try { |
aoqi@0 | 370 | testFile = writeTestFile(testname+".java", sourceString); |
aoqi@0 | 371 | } |
aoqi@0 | 372 | catch (IOException ioe) { ioe.printStackTrace(); } |
aoqi@0 | 373 | // Compile test source and read classfile. |
aoqi@0 | 374 | File classFile = null; |
aoqi@0 | 375 | try { |
aoqi@0 | 376 | classFile = compile(testFile); |
aoqi@0 | 377 | } |
aoqi@0 | 378 | catch (Error err) { |
aoqi@0 | 379 | System.err.println("FAILED compile. Source:\n" + sourceString); |
aoqi@0 | 380 | throw err; |
aoqi@0 | 381 | } |
aoqi@0 | 382 | String testloc = classFile.getAbsolutePath().substring( |
aoqi@0 | 383 | 0,classFile.getAbsolutePath().indexOf(classFile.getPath())); |
aoqi@0 | 384 | for (String clazz : classes) { |
aoqi@0 | 385 | try { |
aoqi@0 | 386 | cf = ClassFile.read(new File(testloc+clazz)); |
aoqi@0 | 387 | } |
aoqi@0 | 388 | catch (Exception e) { e.printStackTrace(); } |
aoqi@0 | 389 | // Test for all methods and fields |
aoqi@0 | 390 | for (Method m: cf.methods) { |
aoqi@0 | 391 | test("METHOD", cf, m, null, true); |
aoqi@0 | 392 | } |
aoqi@0 | 393 | for (Field f: cf.fields) { |
aoqi@0 | 394 | test("FIELD", cf, null, f, true); |
aoqi@0 | 395 | } |
aoqi@0 | 396 | } |
aoqi@0 | 397 | } |
aoqi@0 | 398 | report(); |
aoqi@0 | 399 | if (tc!=xtc) System.out.println("Test Count: " + tc + " != " + |
aoqi@0 | 400 | "expected: " + xtc); |
aoqi@0 | 401 | } |
aoqi@0 | 402 | |
aoqi@0 | 403 | |
aoqi@0 | 404 | String getSrcTemplate(String sTemplate) { |
aoqi@0 | 405 | List<String> tmpl = null; |
aoqi@0 | 406 | String sTmpl = ""; |
aoqi@0 | 407 | try { |
aoqi@0 | 408 | tmpl = Files.readAllLines(new File(testSrc,sTemplate).toPath(), |
aoqi@0 | 409 | Charset.defaultCharset()); |
aoqi@0 | 410 | } |
aoqi@0 | 411 | catch (IOException ioe) { |
aoqi@0 | 412 | String error = "FAILED: Test failed to read template" + sTemplate; |
aoqi@0 | 413 | ioe.printStackTrace(); |
aoqi@0 | 414 | throw new RuntimeException(error); |
aoqi@0 | 415 | } |
aoqi@0 | 416 | for (String l : tmpl) |
aoqi@0 | 417 | sTmpl=sTmpl.concat(l).concat("\n"); |
aoqi@0 | 418 | return sTmpl; |
aoqi@0 | 419 | } |
aoqi@0 | 420 | |
aoqi@0 | 421 | // test class template |
aoqi@0 | 422 | String getSource(String templateName, String testname, |
aoqi@0 | 423 | Boolean Arepeats, Boolean Brepeats, |
aoqi@0 | 424 | Boolean Crepeats, Boolean Drepeats, |
aoqi@0 | 425 | Boolean TArepeats, Boolean TBrepeats) { |
aoqi@0 | 426 | String As = Arepeats ? "@A @A":"@A", |
aoqi@0 | 427 | Bs = Brepeats ? "@B @B":"@B", |
aoqi@0 | 428 | Cs = Crepeats ? "@C @C":"@C", |
aoqi@0 | 429 | Ds = Drepeats ? "@D @D":"@D", |
aoqi@0 | 430 | TAs = TArepeats ? "@TA @TA":"@TA", |
aoqi@0 | 431 | TBs = TBrepeats ? "@TB @TB":"@TB"; |
aoqi@0 | 432 | |
aoqi@0 | 433 | // split up replace() lines for readability |
aoqi@0 | 434 | String testsource = getSrcTemplate(templateName).replace("testname",testname); |
aoqi@0 | 435 | testsource = testsource.replace("_As",As).replace("_Bs",Bs).replace("_Cs",Cs); |
aoqi@0 | 436 | testsource = testsource.replace("_Ds",Ds).replace("_TAs",TAs).replace("_TBs",TBs); |
aoqi@0 | 437 | return testsource; |
aoqi@0 | 438 | } |
aoqi@0 | 439 | } |