test/tools/javac/annotations/repeatingAnnotations/combo/TargetAnnoCombo.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

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, 2014, 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 7151010 8006547 8007766 8029017
aoqi@0 27 * @summary Default test cases for running combinations for Target values
aoqi@0 28 * @build Helper
aoqi@0 29 * @run main TargetAnnoCombo
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.util.Set;
aoqi@0 33 import java.util.List;
aoqi@0 34 import java.io.IOException;
aoqi@0 35 import java.lang.annotation.ElementType;
aoqi@0 36 import java.util.ArrayList;
aoqi@0 37 import java.util.Arrays;
aoqi@0 38 import java.util.EnumSet;
aoqi@0 39 import javax.tools.Diagnostic;
aoqi@0 40 import javax.tools.DiagnosticCollector;
aoqi@0 41 import javax.tools.JavaFileObject;
aoqi@0 42
aoqi@0 43 import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
aoqi@0 44 import static java.lang.annotation.ElementType.CONSTRUCTOR;
aoqi@0 45 import static java.lang.annotation.ElementType.FIELD;
aoqi@0 46 import static java.lang.annotation.ElementType.METHOD;
aoqi@0 47 import static java.lang.annotation.ElementType.PARAMETER;
aoqi@0 48 import static java.lang.annotation.ElementType.TYPE;
aoqi@0 49 import static java.lang.annotation.ElementType.PACKAGE;
aoqi@0 50 import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
aoqi@0 51 import static java.lang.annotation.ElementType.TYPE_USE;
aoqi@0 52 import static java.lang.annotation.ElementType.TYPE_PARAMETER;
aoqi@0 53
aoqi@0 54 public class TargetAnnoCombo {
aoqi@0 55
aoqi@0 56 static final String TESTPKG = "testpkg";
aoqi@0 57
aoqi@0 58 // Set it to true to get more debug information including base and container
aoqi@0 59 // target sets for a given test case.
aoqi@0 60 static final boolean DEBUG = false;
aoqi@0 61
aoqi@0 62 // Define constant target sets to be used for the combination of the target values.
aoqi@0 63 final static Set<ElementType> noSet = null;
aoqi@0 64 final static Set<ElementType> empty = EnumSet.noneOf(ElementType.class);
aoqi@0 65
aoqi@0 66 // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,
aoqi@0 67 // PACKAGE, TYPE_PARAMETER, TYPE_USE]
aoqi@0 68 final static Set<ElementType> allTargets = EnumSet.allOf(ElementType.class);
aoqi@0 69
aoqi@0 70 // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,
aoqi@0 71 // PACKAGE]
aoqi@0 72 final static Set<ElementType> jdk7 = EnumSet.range(TYPE, PACKAGE);
aoqi@0 73
aoqi@0 74 // [TYPE_USE, TYPE_PARAMETER]
aoqi@0 75 final static Set<ElementType> jdk8 = EnumSet.range(TYPE_PARAMETER, TYPE_USE);
aoqi@0 76
aoqi@0 77 // List of test cases to run. This list is created in generate().
aoqi@0 78 // To run a specific test cases add case number in @run main line.
aoqi@0 79 List<TestCase> testCases = new ArrayList<TestCase>();
aoqi@0 80
aoqi@0 81 int errors = 0;
aoqi@0 82
aoqi@0 83 // Identify test cases that fail.
aoqi@0 84 enum IgnoreKind {
aoqi@0 85 RUN,
aoqi@0 86 IGNORE
aoqi@0 87 };
aoqi@0 88
aoqi@0 89 private class TestCase {
aoqi@0 90
aoqi@0 91 private Set<ElementType> baseAnnotations;
aoqi@0 92 private Set<ElementType> containerAnnotations;
aoqi@0 93 private IgnoreKind ignore;
aoqi@0 94
aoqi@0 95 public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations) {
aoqi@0 96 this(baseAnnotations, containerAnnotations, IgnoreKind.RUN);
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations,
aoqi@0 100 IgnoreKind ignoreKind) {
aoqi@0 101 this.baseAnnotations = baseAnnotations;
aoqi@0 102 this.containerAnnotations = containerAnnotations;
aoqi@0 103 this.ignore = ignoreKind;
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 public Set getBaseAnnotations() {
aoqi@0 107 return baseAnnotations;
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 public Set getContainerAnnotations() {
aoqi@0 111 return containerAnnotations;
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 public boolean isIgnored() {
aoqi@0 115 return ignore == IgnoreKind.IGNORE;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 // Determine if a testCase should compile or not.
aoqi@0 119 private boolean isValidSubSet() {
aoqi@0 120 /*
aoqi@0 121 * RULE 1: conAnnoTarget should be a subset of baseAnnoTarget
aoqi@0 122 * RULE 2: For empty @Target ({}) - annotation cannot be applied anywhere
aoqi@0 123 * - Empty sets for both is valid
aoqi@0 124 * - Empty baseTarget set is invalid with non-empty conTarget set
aoqi@0 125 * - Non-empty baseTarget set is valid with empty conTarget set
aoqi@0 126 * RULE 3: For no @Target specified - annotation can be applied to any JDK 7 targets
aoqi@0 127 * - No @Target for both is valid
aoqi@0 128 * - No @Target for baseTarget set with @Target conTarget set is valid
aoqi@0 129 * - @Target for baseTarget set with no @Target for conTarget is invalid
aoqi@0 130 */
aoqi@0 131
aoqi@0 132
aoqi@0 133 /* If baseAnno has no @Target, Foo can be either applied to @Target specified
aoqi@0 134 * for container annotation else will be applicable for all default targets
aoqi@0 135 * if no @Target is present for container annotation.
aoqi@0 136 * In both cases, the set will be a valid set with no @Target for base annotation
aoqi@0 137 */
aoqi@0 138 if (baseAnnotations == null) {
aoqi@0 139 if (containerAnnotations == null) {
aoqi@0 140 return true;
aoqi@0 141 }
aoqi@0 142 return !(containerAnnotations.contains(TYPE_USE) ||
aoqi@0 143 containerAnnotations.contains(TYPE_PARAMETER));
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 Set<ElementType> tempBaseSet = EnumSet.noneOf(ElementType.class);
aoqi@0 147 tempBaseSet.addAll(baseAnnotations);
aoqi@0 148
aoqi@0 149 // If BaseAnno has TYPE, then ANNOTATION_TYPE is allowed by default.
aoqi@0 150 if (baseAnnotations.contains(TYPE)) {
aoqi@0 151 tempBaseSet.add(ANNOTATION_TYPE);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 // If BaseAnno has TYPE_USE, then add the extra allowed types
aoqi@0 155 if (baseAnnotations.contains(TYPE_USE)) {
aoqi@0 156 tempBaseSet.add(ANNOTATION_TYPE);
aoqi@0 157 tempBaseSet.add(TYPE);
aoqi@0 158 tempBaseSet.add(TYPE_PARAMETER);
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 // If containerAnno has no @Target, only valid case if baseAnnoTarget has
aoqi@0 162 // all targets defined else invalid set.
aoqi@0 163 if (containerAnnotations == null) {
aoqi@0 164 return tempBaseSet.containsAll(jdk7);
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 // At this point, neither conAnnoTarget or baseAnnoTarget are null.
aoqi@0 168 if (containerAnnotations.isEmpty()) {
aoqi@0 169 return true;
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 // At this point, conAnnoTarget is non-empty.
aoqi@0 173 if (baseAnnotations.isEmpty()) {
aoqi@0 174 return false;
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 // At this point, neither conAnnoTarget or baseAnnoTarget are empty.
aoqi@0 178 return tempBaseSet.containsAll(containerAnnotations);
aoqi@0 179 }
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 public static void main(String args[]) throws Exception {
aoqi@0 183 TargetAnnoCombo tac = new TargetAnnoCombo();
aoqi@0 184 // Generates all test cases to be run.
aoqi@0 185 tac.generate();
aoqi@0 186 List<Integer> cases = new ArrayList<Integer>();
aoqi@0 187 for (int i = 0; i < args.length; i++) {
aoqi@0 188 cases.add(Integer.parseInt(args[i]));
aoqi@0 189 }
aoqi@0 190 if (cases.isEmpty()) {
aoqi@0 191 tac.run();
aoqi@0 192 } else {
aoqi@0 193 for (int index : cases) {
aoqi@0 194 tac.executeTestCase(tac.testCases.get(index), index);
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 private void generate() {
aoqi@0 200 // Adding test cases to run.
aoqi@0 201 testCases.addAll(Arrays.asList(
aoqi@0 202 // No base target against no container target.
aoqi@0 203 new TestCase(noSet, noSet),
aoqi@0 204 // No base target against empty container target.
aoqi@0 205 new TestCase(noSet, empty),
aoqi@0 206 // No base target against TYPE_USE only container target.
aoqi@0 207 new TestCase(noSet, less(jdk8, TYPE_PARAMETER)),
aoqi@0 208 // No base target against TYPE_PARAMETER only container target.
aoqi@0 209 new TestCase(noSet, less(jdk8, TYPE_USE)),
aoqi@0 210 // No base target against TYPE_USE + TYPE_PARAMETER only container target.
aoqi@0 211 new TestCase(noSet, jdk8),
aoqi@0 212 // No base target against TYPE_USE + some selection of jdk7 targets.
aoqi@0 213 new TestCase(noSet,
aoqi@0 214 plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_USE)),
aoqi@0 215 // No base target against TYPE_PARAMETER + some selection of jdk7 targets.
aoqi@0 216 new TestCase(noSet,
aoqi@0 217 plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_PARAMETER)),
aoqi@0 218 // No base target against each jdk7 target alone as container target.
aoqi@0 219 new TestCase(noSet, plus(empty, TYPE)),
aoqi@0 220 new TestCase(noSet, plus(empty, PARAMETER)),
aoqi@0 221 new TestCase(noSet, plus(empty, PACKAGE)),
aoqi@0 222 new TestCase(noSet, plus(empty, METHOD)),
aoqi@0 223 new TestCase(noSet, plus(empty, LOCAL_VARIABLE)),
aoqi@0 224 new TestCase(noSet, plus(empty, FIELD)),
aoqi@0 225 new TestCase(noSet, plus(empty, CONSTRUCTOR)),
aoqi@0 226 new TestCase(noSet, plus(empty, ANNOTATION_TYPE)),
aoqi@0 227 // Empty base target against no container target.
aoqi@0 228 new TestCase(empty, noSet),
aoqi@0 229 // Empty base target against empty container target.
aoqi@0 230 new TestCase(empty, empty),
aoqi@0 231 // Empty base target against any lone container target.
aoqi@0 232 new TestCase(empty, plus(empty, TYPE)),
aoqi@0 233 new TestCase(empty, plus(empty, PARAMETER)),
aoqi@0 234 new TestCase(empty, plus(empty, PACKAGE)),
aoqi@0 235 new TestCase(empty, plus(empty, METHOD)),
aoqi@0 236 new TestCase(empty, plus(empty, LOCAL_VARIABLE)),
aoqi@0 237 new TestCase(empty, plus(empty, FIELD)),
aoqi@0 238 new TestCase(empty, plus(empty, CONSTRUCTOR)),
aoqi@0 239 new TestCase(empty, plus(empty, ANNOTATION_TYPE)),
aoqi@0 240 new TestCase(empty, less(jdk8, TYPE_USE)),
aoqi@0 241 new TestCase(empty, less(jdk8, TYPE_PARAMETER)),
aoqi@0 242 // No container target against all all-but one jdk7 targets.
aoqi@0 243 new TestCase(less(jdk7, TYPE), noSet),
aoqi@0 244 new TestCase(less(jdk7, PARAMETER), noSet),
aoqi@0 245 new TestCase(less(jdk7, PACKAGE), noSet),
aoqi@0 246 new TestCase(less(jdk7, METHOD), noSet),
aoqi@0 247 new TestCase(less(jdk7, LOCAL_VARIABLE), noSet),
aoqi@0 248 new TestCase(less(jdk7, FIELD), noSet),
aoqi@0 249 new TestCase(less(jdk7, CONSTRUCTOR), noSet),
aoqi@0 250 new TestCase(less(jdk7, ANNOTATION_TYPE), noSet),
aoqi@0 251 // No container against all but TYPE and ANNOTATION_TYPE
aoqi@0 252 new TestCase(less(jdk7, TYPE, ANNOTATION_TYPE), noSet),
aoqi@0 253 // No container against jdk7 targets.
aoqi@0 254 new TestCase(jdk7, noSet),
aoqi@0 255 // No container against jdk7 targets plus one or both of TYPE_USE, TYPE_PARAMETER
aoqi@0 256 new TestCase(plus(jdk7, TYPE_USE), noSet),
aoqi@0 257 new TestCase(plus(jdk7, TYPE_PARAMETER), noSet),
aoqi@0 258 new TestCase(allTargets, noSet),
aoqi@0 259 // Empty container target against any lone target.
aoqi@0 260 new TestCase(plus(empty, TYPE), empty),
aoqi@0 261 new TestCase(plus(empty, PARAMETER), empty),
aoqi@0 262 new TestCase(plus(empty, PACKAGE), empty),
aoqi@0 263 new TestCase(plus(empty, METHOD), empty),
aoqi@0 264 new TestCase(plus(empty, LOCAL_VARIABLE), empty),
aoqi@0 265 new TestCase(plus(empty, FIELD), empty),
aoqi@0 266 new TestCase(plus(empty, CONSTRUCTOR), empty),
aoqi@0 267 new TestCase(plus(empty, ANNOTATION_TYPE), empty),
aoqi@0 268 new TestCase(plus(empty, TYPE_USE), empty),
aoqi@0 269 new TestCase(plus(empty, TYPE_PARAMETER), empty),
aoqi@0 270 // All base targets against all container targets.
aoqi@0 271 new TestCase(allTargets, allTargets),
aoqi@0 272 // All base targets against all but one container targets.
aoqi@0 273 new TestCase(allTargets, less(allTargets, TYPE)),
aoqi@0 274 new TestCase(allTargets, less(allTargets, PARAMETER)),
aoqi@0 275 new TestCase(allTargets, less(allTargets, PACKAGE)),
aoqi@0 276 new TestCase(allTargets, less(allTargets, METHOD)),
aoqi@0 277 new TestCase(allTargets, less(allTargets, LOCAL_VARIABLE)),
aoqi@0 278 new TestCase(allTargets, less(allTargets, FIELD)),
aoqi@0 279 new TestCase(allTargets, less(allTargets, CONSTRUCTOR)),
aoqi@0 280 new TestCase(allTargets, less(allTargets, ANNOTATION_TYPE)),
aoqi@0 281 new TestCase(allTargets, less(allTargets, TYPE_USE)),
aoqi@0 282 new TestCase(allTargets, less(allTargets, TYPE_PARAMETER)),
aoqi@0 283 // All container targets against all but one base targets.
aoqi@0 284 new TestCase(less(allTargets, TYPE), allTargets),
aoqi@0 285 new TestCase(less(allTargets, PARAMETER), allTargets),
aoqi@0 286 new TestCase(less(allTargets, PACKAGE), allTargets),
aoqi@0 287 new TestCase(less(allTargets, METHOD), allTargets),
aoqi@0 288 new TestCase(less(allTargets, LOCAL_VARIABLE), allTargets),
aoqi@0 289 new TestCase(less(allTargets, FIELD), allTargets),
aoqi@0 290 new TestCase(less(allTargets, CONSTRUCTOR), allTargets),
aoqi@0 291 new TestCase(less(allTargets, ANNOTATION_TYPE), allTargets),
aoqi@0 292 new TestCase(less(allTargets, TYPE_USE), allTargets),
aoqi@0 293 new TestCase(less(allTargets, TYPE_PARAMETER), allTargets)));
aoqi@0 294 // Generates 100 test cases for any lone base target contained in Set
aoqi@0 295 // allTargets against any lone container target.
aoqi@0 296 for (ElementType b : allTargets) {
aoqi@0 297 for (ElementType c : allTargets) {
aoqi@0 298 testCases.add(new TestCase(plus(empty, b), plus(empty, c)));
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 void run() throws Exception {
aoqi@0 304 int testCtr = 0;
aoqi@0 305 for (TestCase tc : testCases) {
aoqi@0 306 if (!tc.isIgnored()) {
aoqi@0 307 executeTestCase(tc, testCases.indexOf(tc));
aoqi@0 308 testCtr++;
aoqi@0 309 }
aoqi@0 310 }
aoqi@0 311 System.out.println("Total tests run: " + testCtr);
aoqi@0 312 if (errors > 0) {
aoqi@0 313 throw new Exception(errors + " errors found");
aoqi@0 314 }
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 private void executeTestCase(TestCase testCase, int index) {
aoqi@0 318 debugPrint("Test case number = " + index);
aoqi@0 319 debugPrint(" => baseAnnoTarget = " + testCase.getBaseAnnotations());
aoqi@0 320 debugPrint(" => containerAnnoTarget = " + testCase.getContainerAnnotations());
aoqi@0 321
aoqi@0 322 String className = "TC" + index;
aoqi@0 323 boolean shouldCompile = testCase.isValidSubSet();
aoqi@0 324 Iterable<? extends JavaFileObject> files = getFileList(className, testCase, shouldCompile);
aoqi@0 325 // Get result of compiling test src file(s).
aoqi@0 326 boolean result = getCompileResult(className, shouldCompile, files);
aoqi@0 327 // List test src code if test fails.
aoqi@0 328 if (!result) {
aoqi@0 329 System.out.println("FAIL: Test " + index);
aoqi@0 330 try {
aoqi@0 331 for (JavaFileObject f : files) {
aoqi@0 332 System.out.println("File: " + f.getName() + "\n" + f.getCharContent(true));
aoqi@0 333 }
aoqi@0 334 } catch (IOException ioe) {
aoqi@0 335 System.out.println("Exception: " + ioe);
aoqi@0 336 }
aoqi@0 337 } else {
aoqi@0 338 debugPrint("PASS: Test " + index);
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 // Create src code and corresponding JavaFileObjects.
aoqi@0 344 private Iterable<? extends JavaFileObject> getFileList(String className,
aoqi@0 345 TestCase testCase, boolean shouldCompile) {
aoqi@0 346 Set<ElementType> baseAnnoTarget = testCase.getBaseAnnotations();
aoqi@0 347 Set<ElementType> conAnnoTarget = testCase.getContainerAnnotations();
aoqi@0 348 String srcContent = "";
aoqi@0 349 String pkgInfoContent = "";
aoqi@0 350 String template = Helper.template;
aoqi@0 351 String baseTarget = "", conTarget = "";
aoqi@0 352
aoqi@0 353 String target = Helper.ContentVars.TARGET.getVal();
aoqi@0 354 if (baseAnnoTarget != null) {
aoqi@0 355 String tmp = target.replace("#VAL", convertToString(baseAnnoTarget).toString());
aoqi@0 356 baseTarget = tmp.replace("[", "{").replace("]", "}");
aoqi@0 357 }
aoqi@0 358 if (conAnnoTarget != null) {
aoqi@0 359 String tmp = target.replace("#VAL", convertToString(conAnnoTarget).toString());
aoqi@0 360 conTarget = tmp.replace("[", "{").replace("]", "}");
aoqi@0 361 }
aoqi@0 362
aoqi@0 363 String annoData = Helper.ContentVars.IMPORTSTMTS.getVal()
aoqi@0 364 + conTarget
aoqi@0 365 + Helper.ContentVars.CONTAINER.getVal()
aoqi@0 366 + baseTarget
aoqi@0 367 + Helper.ContentVars.REPEATABLE.getVal()
aoqi@0 368 + Helper.ContentVars.BASE.getVal();
aoqi@0 369
aoqi@0 370 JavaFileObject pkgInfoFile = null;
aoqi@0 371
aoqi@0 372 // If shouldCompile = true and no @Target is specified for container annotation,
aoqi@0 373 // then all 8 ElementType enum constants are applicable as targets for
aoqi@0 374 // container annotation.
aoqi@0 375 if (shouldCompile && conAnnoTarget == null) {
aoqi@0 376 Set<ElementType> copySet = EnumSet.noneOf(ElementType.class);
aoqi@0 377 copySet.addAll(jdk7);
aoqi@0 378 conAnnoTarget = copySet;
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 if (shouldCompile) {
aoqi@0 382 boolean isPkgCasePresent = conAnnoTarget.contains(PACKAGE);
aoqi@0 383 String repeatableAnno = Helper.ContentVars.BASEANNO.getVal()
aoqi@0 384 + " " + Helper.ContentVars.BASEANNO.getVal();
aoqi@0 385 for (ElementType s : conAnnoTarget) {
aoqi@0 386 String replaceStr = "/*" + s.name() + "*/";
aoqi@0 387 if (s.name().equalsIgnoreCase("PACKAGE")) {
aoqi@0 388 //Create packageInfo file.
aoqi@0 389 String pkgInfoName = TESTPKG + "." + "package-info";
aoqi@0 390 pkgInfoContent = repeatableAnno + "\npackage " + TESTPKG + ";" + annoData;
aoqi@0 391 pkgInfoFile = Helper.getFile(pkgInfoName, pkgInfoContent);
aoqi@0 392 } else {
aoqi@0 393 template = template.replace(replaceStr, repeatableAnno);
aoqi@0 394 if (!isPkgCasePresent) {
aoqi@0 395 srcContent = template.replace(
aoqi@0 396 "/*ANNODATA*/", annoData).replace("#ClassName", className);
aoqi@0 397 } else {
aoqi@0 398 replaceStr = "/*PACKAGE*/";
aoqi@0 399 String tmp = template.replace(replaceStr, "package " + TESTPKG + ";");
aoqi@0 400 srcContent = tmp.replace("#ClassName", className);
aoqi@0 401 }
aoqi@0 402 }
aoqi@0 403 }
aoqi@0 404 } else {
aoqi@0 405 // For invalid cases, compilation should fail at declaration site.
aoqi@0 406 template = "class #ClassName {}";
aoqi@0 407 srcContent = annoData + template.replace("#ClassName", className);
aoqi@0 408 }
aoqi@0 409 JavaFileObject srcFile = Helper.getFile(className, srcContent);
aoqi@0 410 Iterable<? extends JavaFileObject> files = null;
aoqi@0 411 if (pkgInfoFile != null) {
aoqi@0 412 files = Arrays.asList(pkgInfoFile, srcFile);
aoqi@0 413 } else {
aoqi@0 414 files = Arrays.asList(srcFile);
aoqi@0 415 }
aoqi@0 416 return files;
aoqi@0 417 }
aoqi@0 418
aoqi@0 419 // Compile the test source file(s) and return test result.
aoqi@0 420 private boolean getCompileResult(String className, boolean shouldCompile,
aoqi@0 421 Iterable<? extends JavaFileObject> files) {
aoqi@0 422
aoqi@0 423 DiagnosticCollector<JavaFileObject> diagnostics =
aoqi@0 424 new DiagnosticCollector<JavaFileObject>();
aoqi@0 425 Helper.compileCode(diagnostics, files);
aoqi@0 426 // Test case pass or fail.
aoqi@0 427 boolean ok = false;
aoqi@0 428 String errMesg = "";
aoqi@0 429 int numDiags = diagnostics.getDiagnostics().size();
aoqi@0 430 if (numDiags == 0) {
aoqi@0 431 if (shouldCompile) {
aoqi@0 432 debugPrint("Test passed, compiled as expected.");
aoqi@0 433 ok = true;
aoqi@0 434 } else {
aoqi@0 435 errMesg = "Test failed, compiled unexpectedly.";
aoqi@0 436 ok = false;
aoqi@0 437 }
aoqi@0 438 } else {
aoqi@0 439 if (shouldCompile) {
aoqi@0 440 // did not compile.
aoqi@0 441 errMesg = "Test failed, did not compile.";
aoqi@0 442 ok = false;
aoqi@0 443 } else {
aoqi@0 444 // Error in compilation as expected.
aoqi@0 445 String expectedErrKey = "compiler.err.invalid.repeatable."
aoqi@0 446 + "annotation.incompatible.target";
aoqi@0 447 for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
aoqi@0 448 if ((d.getKind() == Diagnostic.Kind.ERROR)
aoqi@0 449 && d.getCode().contains(expectedErrKey)) {
aoqi@0 450 // Error message as expected.
aoqi@0 451 debugPrint("Error message as expected.");
aoqi@0 452 ok = true;
aoqi@0 453 break;
aoqi@0 454 } else {
aoqi@0 455 // error message is incorrect.
aoqi@0 456 ok = false;
aoqi@0 457 }
aoqi@0 458 }
aoqi@0 459 if (!ok) {
aoqi@0 460 errMesg = "Incorrect error received when compiling "
aoqi@0 461 + className + ", expected: " + expectedErrKey;
aoqi@0 462 }
aoqi@0 463 }
aoqi@0 464 }
aoqi@0 465
aoqi@0 466 if (!ok) {
aoqi@0 467 error(errMesg);
aoqi@0 468 for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
aoqi@0 469 System.out.println(" Diags: " + d);
aoqi@0 470 }
aoqi@0 471 }
aoqi@0 472 return ok;
aoqi@0 473 }
aoqi@0 474
aoqi@0 475 private Set<ElementType> less(Set<ElementType> base, ElementType... sub) {
aoqi@0 476 Set<ElementType> res = EnumSet.noneOf(ElementType.class);
aoqi@0 477 res.addAll(base);
aoqi@0 478 for (ElementType t : sub) {
aoqi@0 479 res.remove(t);
aoqi@0 480 }
aoqi@0 481 return res;
aoqi@0 482 }
aoqi@0 483
aoqi@0 484 private Set<ElementType> plus(Set<ElementType> base, ElementType... add) {
aoqi@0 485 Set<ElementType> res = EnumSet.noneOf(ElementType.class);
aoqi@0 486 res.addAll(base);
aoqi@0 487 for (ElementType t : add) {
aoqi@0 488 res.add(t);
aoqi@0 489 }
aoqi@0 490 return res;
aoqi@0 491 }
aoqi@0 492
aoqi@0 493 // Iterate target set and add "ElementType." in front of every target type.
aoqi@0 494 private List<String> convertToString(Set<ElementType> annoTarget) {
aoqi@0 495 if (annoTarget == null) {
aoqi@0 496 return null;
aoqi@0 497 }
aoqi@0 498 List<String> annoTargets = new ArrayList<String>();
aoqi@0 499 for (ElementType e : annoTarget) {
aoqi@0 500 annoTargets.add("ElementType." + e.name());
aoqi@0 501 }
aoqi@0 502 return annoTargets;
aoqi@0 503 }
aoqi@0 504
aoqi@0 505 private void debugPrint(String string) {
aoqi@0 506 if (DEBUG) {
aoqi@0 507 System.out.println(string);
aoqi@0 508 }
aoqi@0 509 }
aoqi@0 510
aoqi@0 511 private void error(String msg) {
aoqi@0 512 System.out.println("ERROR: " + msg);
aoqi@0 513 errors++;
aoqi@0 514 }
aoqi@0 515 }
aoqi@0 516

mercurial