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

Thu, 07 Feb 2013 20:47:06 -0800

author
darcy
date
Thu, 07 Feb 2013 20:47:06 -0800
changeset 1554
5125b9854d07
child 1589
87884cd0fea3
permissions
-rw-r--r--

7195131: Update 2 compiler combo tests for repeating annotations to include package and default use cases
Reviewed-by: darcy
Contributed-by: sonali.goel@oracle.com

darcy@1554 1 /*
darcy@1554 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
darcy@1554 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@1554 4 *
darcy@1554 5 * This code is free software; you can redistribute it and/or modify it
darcy@1554 6 * under the terms of the GNU General Public License version 2 only, as
darcy@1554 7 * published by the Free Software Foundation.
darcy@1554 8 *
darcy@1554 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@1554 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@1554 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@1554 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@1554 13 * accompanied this code).
darcy@1554 14 *
darcy@1554 15 * You should have received a copy of the GNU General Public License version
darcy@1554 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@1554 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@1554 18 *
darcy@1554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@1554 20 * or visit www.oracle.com if you need additional information or have any
darcy@1554 21 * questions.
darcy@1554 22 */
darcy@1554 23
darcy@1554 24 /**
darcy@1554 25 * @test
darcy@1554 26 * @bug 7195131
darcy@1554 27 * @author sogoel
darcy@1554 28 * @summary Combo test for all possible combinations for Target values
darcy@1554 29 * @build Helper
darcy@1554 30 * @compile TargetAnnoCombo.java TestCaseGenerator.java
darcy@1554 31 * @run main TargetAnnoCombo
darcy@1554 32 */
darcy@1554 33
darcy@1554 34 import java.io.IOException;
darcy@1554 35 import java.util.ArrayList;
darcy@1554 36 import java.util.Arrays;
darcy@1554 37 import java.util.HashSet;
darcy@1554 38 import java.util.Set;
darcy@1554 39 import javax.tools.Diagnostic;
darcy@1554 40 import javax.tools.DiagnosticCollector;
darcy@1554 41 import javax.tools.JavaFileObject;
darcy@1554 42
darcy@1554 43 /*
darcy@1554 44 * TargetAnnoCombo gets a list of test case numbers using TestCaseGenerator.
darcy@1554 45 * For each of the test case number, @Target sets for base and container annotations
darcy@1554 46 * are determined, source files are generated, compiled, and the result is verified
darcy@1554 47 * based on if the @Target set for base and container is a positive or negative combination.
darcy@1554 48 *
darcy@1554 49 * @Target sets for base and container annotations are determined using a bit mapping of
darcy@1554 50 * 10 ElementType enum constants defined in JDK8.
darcy@1554 51 *
darcy@1554 52 * Bit Target value
darcy@1554 53 * 0 "ElementType.ANNOTATION_TYPE"
darcy@1554 54 * 1 "ElementType.CONSTRUCTOR"
darcy@1554 55 * 2 "ElementType.FIELD"
darcy@1554 56 * 3 "ElementType.LOCAL_VARIABLE"
darcy@1554 57 * 4 "ElementType.METHOD"
darcy@1554 58 * 5 "ElementType.TYPE"
darcy@1554 59 * 6 "ElementType.PARAMETER"
darcy@1554 60 * 7 "ElementType.PACKAGE"
darcy@1554 61 * 8 "ElementType.TYPE_USE"
darcy@1554 62 * 9 "ElementType.TYPE_PARAMETER"
darcy@1554 63 *
darcy@1554 64 * Group 1:
darcy@1554 65 * 20 bits mapping, representing a test case number, is used for all target set
darcy@1554 66 * combinations ( 0 to 1048575 ) including empty @Target sets => @Target({}).
darcy@1554 67 * From this 20 bits, 10 bits are for base followed by 10 bits for container
darcy@1554 68 * where each bit maps to an ElementType enum constant defined in JDK8.
darcy@1554 69 *
darcy@1554 70 * Examples:
darcy@1554 71 * Test case number: 4, binary: 100 => container=100, base=[], container=["ElementType.FIELD"]
darcy@1554 72 * Test case number: 1003575, binary: 11110101000000110111 => base=1111010100, container=0000110111;
darcy@1554 73 * base=["ElementType.PARAMETER", "ElementType.TYPE_USE", "ElementType.METHOD", "ElementType.FIELD", "ElementType.PACKAGE", "ElementType.TYPE_PARAMETER"],
darcy@1554 74 * container=["ElementType.TYPE", "ElementType.METHOD", "ElementType.ANNOTATION_TYPE", "ElementType.CONSTRUCTOR", "ElementType.FIELD"]
darcy@1554 75 *
darcy@1554 76 * In the following groups, no @Target set is represented by null.
darcy@1554 77 * Group 2:
darcy@1554 78 * @Target is not defined on base.
darcy@1554 79 * Target sets for container are determined using the 10-bit binary number
darcy@1554 80 * resulting in 1024 test cases, mapping them to test case numbers from
darcy@1554 81 * 1048576 to (1048576 + 1023) => 1048576 to 1049599.
darcy@1554 82 *
darcy@1554 83 * Example:
darcy@1554 84 * Test case number: 1048587 => 1048587 - 1048576 = test case 11 in Group 2, binary: 1011 =>
darcy@1554 85 * base = null,
darcy@1554 86 * container = ["ElementType.ANNOTATION_TYPE","ElementType.CONSTRUCTOR","ElementType.LOCAL_VARIABLE"]
darcy@1554 87 *
darcy@1554 88 * Group 3:
darcy@1554 89 * @Target is not defined on container
darcy@1554 90 * Target sets for base are determined using the 10-bit binary number
darcy@1554 91 * resulting in 1024 test cases, mapping them to test case numbers from
darcy@1554 92 * 1049600 to (1049600 + 1023) => 1049600 to 1050623.
darcy@1554 93 *
darcy@1554 94 * Example:
darcy@1554 95 * Test case number: 1049708 => 1049708 - 1049600 = test case 108 in Group 3, binary: 1101100 =>
darcy@1554 96 * base = ["ElementType.FIELD", "ElementType.LOCAL_VARIABLE", "ElementType.TYPE", "ElementType.PARAMETER"],
darcy@1554 97 * container = null
darcy@1554 98 *
darcy@1554 99 * For the above group, test case number: 1049855 gives compiler error, JDK-8006547 filed
darcy@1554 100 *
darcy@1554 101 * Group 4:
darcy@1554 102 * @Target not defined for both base and container annotations.
darcy@1554 103 *
darcy@1554 104 * This is the last test and corresponds to test case number 1050624. base=null, container=null
darcy@1554 105 *
darcy@1554 106 * Examples to run this test:
darcy@1554 107 * 1. Run a specific test case number:
darcy@1554 108 * ${JTREG} -DTestCaseNum=10782 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
darcy@1554 109 * 2. Run specific number of tests:
darcy@1554 110 * ${JTREG} -DNumberOfTests=4 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
darcy@1554 111 * 3. Run specific number of tests with a seed:
darcy@1554 112 * ${JTREG} -DNumberOfTests=4 -DTestSeed=-972894659 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
darcy@1554 113 * 4. Run tests in default mode (number of tests = 1000):
darcy@1554 114 * ${JTREG} -DTestMode=DEFAULT -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
darcy@1554 115 * 5. Run all tests (FULL mode):
darcy@1554 116 * ${JTREG} -DTestMode=FULL -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
darcy@1554 117 *
darcy@1554 118 */
darcy@1554 119
darcy@1554 120 public class TargetAnnoCombo {
darcy@1554 121 int errors = 0;
darcy@1554 122 static final String TESTPKG = "testpkg";
darcy@1554 123 /*
darcy@1554 124 * Set it to true to get more debug information including base and
darcy@1554 125 * container target sets for a given test case number
darcy@1554 126 */
darcy@1554 127 static final boolean DEBUG = false;
darcy@1554 128
darcy@1554 129 // JDK 5/6/7/8 Targets
darcy@1554 130 static final String[] targetVals = {"ElementType.ANNOTATION_TYPE",
darcy@1554 131 "ElementType.CONSTRUCTOR", "ElementType.FIELD",
darcy@1554 132 "ElementType.LOCAL_VARIABLE", "ElementType.METHOD",
darcy@1554 133 "ElementType.TYPE", "ElementType.PARAMETER",
darcy@1554 134 "ElementType.PACKAGE", "ElementType.TYPE_USE",
darcy@1554 135 "ElementType.TYPE_PARAMETER"};
darcy@1554 136
darcy@1554 137 // TYPE_USE and TYPE_PARAMETER (added in JDK8) are not part of default Target set
darcy@1554 138 static final int DEFAULT_TARGET_CNT = 8;
darcy@1554 139
darcy@1554 140 public static void main(String args[]) throws Exception {
darcy@1554 141
darcy@1554 142 /* maxTestNum = (base and container combinations of targetVals elems [0 - 1048575 combos])
darcy@1554 143 * + (combinations where base or container has no Target [1024 combos])
darcy@1554 144 * + (no -1 even though 1st test is number 0 as last test is where both
darcy@1554 145 * base and container have no target)
darcy@1554 146 */
darcy@1554 147
darcy@1554 148 int maxTestNum = (int)Math.pow(2, 2*targetVals.length) + 2*(int)Math.pow(2, targetVals.length);
darcy@1554 149 TestCaseGenerator tcg = new TestCaseGenerator(maxTestNum);
darcy@1554 150 TargetAnnoCombo tac = new TargetAnnoCombo();
darcy@1554 151
darcy@1554 152 int testCtr = 0;
darcy@1554 153 int testCase = -1;
darcy@1554 154 while ( (testCase=tcg.getNextTestCase()) != -1 ) {
darcy@1554 155 tac.executeTestCase(testCase, maxTestNum);
darcy@1554 156 testCtr++;
darcy@1554 157 }
darcy@1554 158
darcy@1554 159 System.out.println("Total tests run: " + testCtr);
darcy@1554 160 if (tac.errors > 0)
darcy@1554 161 throw new Exception(tac.errors + " errors found");
darcy@1554 162 }
darcy@1554 163
darcy@1554 164 /*
darcy@1554 165 * For given testCase, determine the base and container annotation Target sets,
darcy@1554 166 * get if testCase should compile, get test source file(s), get compilation result and verify.
darcy@1554 167 *
darcy@1554 168 */
darcy@1554 169 private void executeTestCase(int testCase, int maxTestNum) {
darcy@1554 170
darcy@1554 171 // Determine base and container annotation Target sets for the testCase
darcy@1554 172 Set<String> baseAnnoTarget = null;
darcy@1554 173 Set<String> conAnnoTarget = null;
darcy@1554 174
darcy@1554 175 //Number of base and container combinations [0 - 1048575 combos]
darcy@1554 176 int baseContCombos = (int)Math.pow(2, 2*targetVals.length);
darcy@1554 177 //Number of either base or container combinations when one of them has no @Target [1024 combos]
darcy@1554 178 int targetValsCombos = (int)Math.pow(2, targetVals.length);
darcy@1554 179
darcy@1554 180 if (testCase >= baseContCombos) {
darcy@1554 181 //Base annotation do not have @Target
darcy@1554 182 if (testCase < baseContCombos + targetValsCombos) {
darcy@1554 183 baseAnnoTarget = null;
darcy@1554 184 conAnnoTarget = getSetFromBitVec(Integer.toBinaryString(testCase - baseContCombos));
darcy@1554 185 } else if (testCase < baseContCombos + 2*targetValsCombos) {
darcy@1554 186 //Container annotation do not have @Target
darcy@1554 187 baseAnnoTarget = getSetFromBitVec(Integer.toBinaryString(testCase - baseContCombos - targetValsCombos));
darcy@1554 188 conAnnoTarget = null;
darcy@1554 189 } else {
darcy@1554 190 //Both Base and Container annotation do not have @Target
darcy@1554 191 baseAnnoTarget = null;
darcy@1554 192 conAnnoTarget = null;
darcy@1554 193 }
darcy@1554 194 } else {
darcy@1554 195 //TestCase number is represented as 10-bits for base followed by container bits
darcy@1554 196 String bin = Integer.toBinaryString(testCase);
darcy@1554 197 String base="", cont=bin;
darcy@1554 198 if (bin.length() > targetVals.length){
darcy@1554 199 base = bin.substring(0, bin.length() - targetVals.length);
darcy@1554 200 cont = bin.substring(bin.length() - targetVals.length,bin.length());
darcy@1554 201 }
darcy@1554 202 baseAnnoTarget = getSetFromBitVec(base);
darcy@1554 203 conAnnoTarget = getSetFromBitVec(cont);
darcy@1554 204 }
darcy@1554 205
darcy@1554 206 debugPrint("Test case number = " + testCase + " => binary = " + Integer.toBinaryString(testCase));
darcy@1554 207 debugPrint(" => baseAnnoTarget = " + baseAnnoTarget);
darcy@1554 208 debugPrint(" => containerAnnoTarget = " + conAnnoTarget);
darcy@1554 209
darcy@1554 210 // Determine if a testCase should compile or not
darcy@1554 211 String className = "TC" + testCase;
darcy@1554 212 boolean shouldCompile = isValidSubSet(baseAnnoTarget, conAnnoTarget);
darcy@1554 213
darcy@1554 214 // Get test source file(s)
darcy@1554 215 Iterable<? extends JavaFileObject> files = getFileList(className, baseAnnoTarget,
darcy@1554 216 conAnnoTarget, shouldCompile);
darcy@1554 217
darcy@1554 218 // Get result of compiling test src file(s)
darcy@1554 219 boolean result = getCompileResult(className, shouldCompile, files);
darcy@1554 220
darcy@1554 221 // List test src code if test fails
darcy@1554 222 if(!result) {
darcy@1554 223 System.out.println("FAIL: Test " + testCase);
darcy@1554 224 try {
darcy@1554 225 for (JavaFileObject f: files) {
darcy@1554 226 System.out.println("File: " + f.getName() + "\n" + f.getCharContent(true));
darcy@1554 227 }
darcy@1554 228 } catch (IOException ioe) {
darcy@1554 229 System.out.println("Exception: " + ioe);
darcy@1554 230 }
darcy@1554 231 } else {
darcy@1554 232 debugPrint("PASS: Test " + testCase);
darcy@1554 233 }
darcy@1554 234 }
darcy@1554 235
darcy@1554 236 // Get a Set<String> based on bits that are set to 1
darcy@1554 237 public Set<String> getSetFromBitVec(String bitVec) {
darcy@1554 238 Set<String> ret = new HashSet<>();
darcy@1554 239 char[] bit = bitVec.toCharArray();
darcy@1554 240 for (int i=bit.length-1, j=0; i>=0; i--, j++){
darcy@1554 241 if (bit[i] == '1') {
darcy@1554 242 ret.add(targetVals[j]);
darcy@1554 243 }
darcy@1554 244 }
darcy@1554 245 return ret;
darcy@1554 246 }
darcy@1554 247
darcy@1554 248 // Compile the test source file(s) and return test result
darcy@1554 249 private boolean getCompileResult(String className, boolean shouldCompile,
darcy@1554 250 Iterable<? extends JavaFileObject> files) {
darcy@1554 251
darcy@1554 252 DiagnosticCollector<JavaFileObject> diagnostics =
darcy@1554 253 new DiagnosticCollector<JavaFileObject>();
darcy@1554 254 Helper.compileCode(diagnostics, files);
darcy@1554 255
darcy@1554 256 // Test case pass or fail
darcy@1554 257 boolean ok = false;
darcy@1554 258
darcy@1554 259 String errMesg = "";
darcy@1554 260 int numDiags = diagnostics.getDiagnostics().size();
darcy@1554 261
darcy@1554 262 if (numDiags == 0) {
darcy@1554 263 if (shouldCompile) {
darcy@1554 264 debugPrint("Test passed, compiled as expected.");
darcy@1554 265 ok = true;
darcy@1554 266 } else {
darcy@1554 267 errMesg = "Test failed, compiled unexpectedly.";
darcy@1554 268 ok = false;
darcy@1554 269 }
darcy@1554 270 } else {
darcy@1554 271 if (shouldCompile) {
darcy@1554 272 // did not compile
darcy@1554 273 errMesg = "Test failed, did not compile.";
darcy@1554 274 ok = false;
darcy@1554 275 } else {
darcy@1554 276 // Error in compilation as expected
darcy@1554 277 String expectedErrKey = "compiler.err.invalid.repeatable." +
darcy@1554 278 "annotation.incompatible.target";
darcy@1554 279 for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
darcy@1554 280 if((d.getKind() == Diagnostic.Kind.ERROR) &&
darcy@1554 281 d.getCode().contains(expectedErrKey)) {
darcy@1554 282 // Error message as expected
darcy@1554 283 debugPrint("Error message as expected.");
darcy@1554 284 ok = true;
darcy@1554 285 break;
darcy@1554 286 } else {
darcy@1554 287 // error message is incorrect
darcy@1554 288 ok = false;
darcy@1554 289 }
darcy@1554 290 }
darcy@1554 291 if (!ok) {
darcy@1554 292 errMesg = "Incorrect error received when compiling " +
darcy@1554 293 className + ", expected: " + expectedErrKey;
darcy@1554 294 }
darcy@1554 295 }
darcy@1554 296 }
darcy@1554 297
darcy@1554 298 if(!ok) {
darcy@1554 299 error(errMesg);
darcy@1554 300 for (Diagnostic<?> d : diagnostics.getDiagnostics())
darcy@1554 301 System.out.println(" Diags: " + d);
darcy@1554 302 }
darcy@1554 303 return ok;
darcy@1554 304 }
darcy@1554 305
darcy@1554 306 private void debugPrint(String string) {
darcy@1554 307 if(DEBUG)
darcy@1554 308 System.out.println(string);
darcy@1554 309 }
darcy@1554 310
darcy@1554 311 // Create src code and corresponding JavaFileObjects
darcy@1554 312 private Iterable<? extends JavaFileObject> getFileList(String className,
darcy@1554 313 Set<String> baseAnnoTarget, Set<String> conAnnoTarget,
darcy@1554 314 boolean shouldCompile) {
darcy@1554 315
darcy@1554 316 String srcContent = "";
darcy@1554 317 String pkgInfoContent = "";
darcy@1554 318 String template = Helper.template;
darcy@1554 319 String baseTarget = "", conTarget = "";
darcy@1554 320
darcy@1554 321 String target = Helper.ContentVars.TARGET.getVal();
darcy@1554 322 if(baseAnnoTarget != null) {
darcy@1554 323 baseTarget = target.replace("#VAL", baseAnnoTarget.toString())
darcy@1554 324 .replace("[", "{").replace("]", "}");
darcy@1554 325 }
darcy@1554 326 if(conAnnoTarget != null) {
darcy@1554 327 conTarget = target.replace("#VAL", conAnnoTarget.toString())
darcy@1554 328 .replace("[", "{").replace("]", "}");
darcy@1554 329 }
darcy@1554 330
darcy@1554 331 String annoData = Helper.ContentVars.IMPORTSTMTS.getVal() +
darcy@1554 332 conTarget +
darcy@1554 333 Helper.ContentVars.CONTAINER.getVal() +
darcy@1554 334 baseTarget +
darcy@1554 335 Helper.ContentVars.REPEATABLE.getVal() +
darcy@1554 336 Helper.ContentVars.BASE.getVal();
darcy@1554 337
darcy@1554 338 JavaFileObject pkgInfoFile = null;
darcy@1554 339
darcy@1554 340 /*
darcy@1554 341 * If shouldCompile = true and no @Target is specified for container annotation,
darcy@1554 342 * then all 8 ElementType enum constants are applicable as targets for
darcy@1554 343 * container annotation.
darcy@1554 344 */
darcy@1554 345 if(shouldCompile && conAnnoTarget == null) {
darcy@1554 346 //conAnnoTarget = new HashSet<String>(Arrays.asList(targetVals));
darcy@1554 347 conAnnoTarget = getDefaultTargetSet();
darcy@1554 348 }
darcy@1554 349
darcy@1554 350 if(shouldCompile) {
darcy@1554 351 boolean isPkgCasePresent = new ArrayList<String>(conAnnoTarget).contains("ElementType.PACKAGE");
darcy@1554 352 String repeatableAnno = Helper.ContentVars.BASEANNO.getVal() + " " + Helper.ContentVars.BASEANNO.getVal();
darcy@1554 353 for(String s: conAnnoTarget) {
darcy@1554 354 s = s.replace("ElementType.","");
darcy@1554 355 String replaceStr = "/*"+s+"*/";
darcy@1554 356 if(s.equalsIgnoreCase("PACKAGE")) {
darcy@1554 357 //Create packageInfo file
darcy@1554 358 String pkgInfoName = TESTPKG + "." + "package-info";
darcy@1554 359 pkgInfoContent = repeatableAnno + "\npackage " + TESTPKG + ";" + annoData;
darcy@1554 360 pkgInfoFile = Helper.getFile(pkgInfoName, pkgInfoContent);
darcy@1554 361 } else {
darcy@1554 362 template = template.replace(replaceStr, repeatableAnno);
darcy@1554 363 //srcContent = template.replace("#ClassName",className);
darcy@1554 364 if(!isPkgCasePresent) {
darcy@1554 365 srcContent = template.replace("/*ANNODATA*/", annoData).replace("#ClassName",className);
darcy@1554 366 } else {
darcy@1554 367 replaceStr = "/*PACKAGE*/";
darcy@1554 368 srcContent = template.replace(replaceStr, "package " + TESTPKG + ";")
darcy@1554 369 .replace("#ClassName", className);
darcy@1554 370 }
darcy@1554 371 }
darcy@1554 372 }
darcy@1554 373 } else {
darcy@1554 374 // For invalid cases, compilation should fail at declaration site
darcy@1554 375 template = "class #ClassName {}";
darcy@1554 376 srcContent = annoData + template.replace("#ClassName",className);
darcy@1554 377 }
darcy@1554 378 JavaFileObject srcFile = Helper.getFile(className, srcContent);
darcy@1554 379 Iterable<? extends JavaFileObject> files = null;
darcy@1554 380 if(pkgInfoFile != null)
darcy@1554 381 files = Arrays.asList(pkgInfoFile,srcFile);
darcy@1554 382 else
darcy@1554 383 files = Arrays.asList(srcFile);
darcy@1554 384 return files;
darcy@1554 385 }
darcy@1554 386
darcy@1554 387 private Set<String> getDefaultTargetSet() {
darcy@1554 388 Set<String> defaultSet = new HashSet<>();
darcy@1554 389 int ctr = 0;
darcy@1554 390 for(String s : targetVals) {
darcy@1554 391 if(ctr++ < DEFAULT_TARGET_CNT) {
darcy@1554 392 defaultSet.add(s);
darcy@1554 393 }
darcy@1554 394 }
darcy@1554 395 return defaultSet;
darcy@1554 396 }
darcy@1554 397
darcy@1554 398 private boolean isValidSubSet(Set<String> baseAnnoTarget, Set<String> conAnnoTarget) {
darcy@1554 399 /*
darcy@1554 400 * RULE 1: conAnnoTarget should be a subset of baseAnnoTarget
darcy@1554 401 * RULE 2: For empty @Target ({}) - annotation cannot be applied anywhere
darcy@1554 402 * - Empty sets for both is valid
darcy@1554 403 * - Empty baseTarget set is invalid with non-empty conTarget set
darcy@1554 404 * - Non-empty baseTarget set is valid with empty conTarget set
darcy@1554 405 * RULE 3: For no @Target specified - annotation can be applied to any JDK 7 targets
darcy@1554 406 * - No @Target for both is valid
darcy@1554 407 * - No @Target for baseTarget set with @Target conTarget set is valid
darcy@1554 408 * - @Target for baseTarget set with no @Target for conTarget is invalid
darcy@1554 409 */
darcy@1554 410
darcy@1554 411
darcy@1554 412 /* If baseAnno has no @Target, Foo can be either applied to @Target specified for container annotation
darcy@1554 413 * else will be applicable for all default targets if no @Target is present for container annotation.
darcy@1554 414 * In both cases, the set will be a valid set with no @Target for base annotation
darcy@1554 415 */
darcy@1554 416 if(baseAnnoTarget == null) {
darcy@1554 417 if(conAnnoTarget == null) return true;
darcy@1554 418 return !(conAnnoTarget.contains("ElementType.TYPE_USE") || conAnnoTarget.contains("ElementType.TYPE_PARAMETER"));
darcy@1554 419 }
darcy@1554 420
darcy@1554 421 Set<String> tempBaseSet = new HashSet<>(baseAnnoTarget);
darcy@1554 422 // If BaseAnno has TYPE, then ANNOTATION_TYPE is allowed by default
darcy@1554 423 if(baseAnnoTarget.contains("ElementType.TYPE")) {
darcy@1554 424 tempBaseSet.add("ElementType.ANNOTATION_TYPE");
darcy@1554 425 }
darcy@1554 426
darcy@1554 427 /*
darcy@1554 428 * If containerAnno has no @Target, only valid case if baseAnnoTarget has all targets defined
darcy@1554 429 * else invalid set
darcy@1554 430 */
darcy@1554 431 if(conAnnoTarget == null) {
darcy@1554 432 return (tempBaseSet.containsAll(getDefaultTargetSet()));
darcy@1554 433 }
darcy@1554 434
darcy@1554 435 // At this point, neither conAnnoTarget or baseAnnoTarget are null
darcy@1554 436 if(conAnnoTarget.size() == 0) return true;
darcy@1554 437
darcy@1554 438 // At this point, conAnnoTarget is non-empty
darcy@1554 439 if (baseAnnoTarget.size() == 0) return false;
darcy@1554 440
darcy@1554 441 // At this point, neither conAnnoTarget or baseAnnoTarget are empty
darcy@1554 442 return tempBaseSet.containsAll(conAnnoTarget);
darcy@1554 443 }
darcy@1554 444
darcy@1554 445 void error(String msg) {
darcy@1554 446 System.out.println("ERROR: " + msg);
darcy@1554 447 errors++;
darcy@1554 448 }
darcy@1554 449
darcy@1554 450 // Lists the start and end range for the given set of target vals
darcy@1554 451 void showGroups() {
darcy@1554 452 //Group 1: All target set combinations ( 0 to 1048575 ) including empty @Target sets => @Target({})
darcy@1554 453 int grpEnd1 = (int)Math.pow(2, 2*targetVals.length) - 1;
darcy@1554 454 System.out.println("[Group 1]: 0 - " + grpEnd1);
darcy@1554 455
darcy@1554 456 //Group 2: @Target not defined for base annotation ( 1048576 - 1049599 ).
darcy@1554 457 System.out.print("[Group 2]: " + (grpEnd1 + 1) + " - ");
darcy@1554 458 int grpEnd2 = grpEnd1 + 1 + (int)Math.pow(2, targetVals.length) - 1;
darcy@1554 459 System.out.println(grpEnd2);
darcy@1554 460
darcy@1554 461 //Group 3: @Target not defined for container annotation ( 1049600 - 1050623 ).
darcy@1554 462 System.out.print("[Group 3]: " + (grpEnd2 + 1) + " - ");
darcy@1554 463 int grpEnd3 = grpEnd2 + 1 + (int)Math.pow(2, targetVals.length) - 1;
darcy@1554 464 System.out.println(grpEnd3);
darcy@1554 465
darcy@1554 466 //Group 4: @Target not defined for both base and container annotations ( 1050624 ).
darcy@1554 467 System.out.println("[Group 4]: " + (grpEnd3 + 1));
darcy@1554 468 }
darcy@1554 469 }

mercurial