test/tools/javac/processing/T6920317.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1466
b52a38d4536c
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2012, 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 6920317
aoqi@0 27 * @summary package-info.java file has to be specified on the javac cmdline, else it will not be avail
aoqi@0 28 * @library /tools/javac/lib
aoqi@0 29 */
aoqi@0 30
aoqi@0 31 import java.io.*;
aoqi@0 32 import java.util.*;
aoqi@0 33 import javax.annotation.processing.*;
aoqi@0 34 import javax.lang.model.*;
aoqi@0 35 import javax.lang.model.element.*;
aoqi@0 36 import javax.lang.model.util.*;
aoqi@0 37 import javax.tools.*;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * The test exercises different ways of providing annotations for a package.
aoqi@0 41 * Each way provides an annotation with a unique argument. For each test
aoqi@0 42 * case, the test verifies that the annotation with the correct argument is
aoqi@0 43 * found by the compiler.
aoqi@0 44 */
aoqi@0 45 public class T6920317 {
aoqi@0 46 public static void main(String... args) throws Exception {
aoqi@0 47 new T6920317().run(args);
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 // Used to describe properties of files to be put on command line, source path, class path
aoqi@0 51 enum Kind {
aoqi@0 52 /** File is not used. */
aoqi@0 53 NONE,
aoqi@0 54 /** File is used. */
aoqi@0 55 OLD,
aoqi@0 56 /** Only applies to files on classpath/sourcepath, when there is another file on the
aoqi@0 57 * other path of type OLD, in which case, this file must be newer than the other one. */
aoqi@0 58 NEW,
aoqi@0 59 /** Only applies to files on classpath/sourcepath, when there is no file in any other
aoqi@0 60 * location, in which case, this file will be generated by the annotation processor. */
aoqi@0 61 GEN
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 void run(String... args) throws Exception {
aoqi@0 65 // if no args given, all test cases are run
aoqi@0 66 // if args given, they indicate the test cases to be run
aoqi@0 67 for (int i = 0; i < args.length; i++) {
aoqi@0 68 tests.add(Integer.valueOf(args[i]));
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 setup();
aoqi@0 72
aoqi@0 73 // Run tests for all combinations of files on command line, source path and class path.
aoqi@0 74 // Invalid combinations are skipped in the test method
aoqi@0 75 for (Kind cmdLine: EnumSet.of(Kind.NONE, Kind.OLD)) {
aoqi@0 76 for (Kind srcPath: Kind.values()) {
aoqi@0 77 for (Kind clsPath: Kind.values()) {
aoqi@0 78 try {
aoqi@0 79 test(cmdLine, srcPath, clsPath);
aoqi@0 80 } catch (Exception e) {
aoqi@0 81 e.printStackTrace();
aoqi@0 82 error("Exception " + e);
aoqi@0 83 // uncomment to stop on first failed test case
aoqi@0 84 // throw e;
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87 }
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 if (errors > 0)
aoqi@0 91 throw new Exception(errors + " errors occurred");
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 /** One time setup for files and directories to be used in the various test cases. */
aoqi@0 95 void setup() throws Exception {
aoqi@0 96 // Annotation used in test cases to annotate package. This file is
aoqi@0 97 // given on the command line in test cases.
aoqi@0 98 test_java = writeFile("Test.java", "package p; @interface Test { String value(); }");
aoqi@0 99 // Compile the annotation for use later in setup
aoqi@0 100 File tmpClasses = new File("tmp.classes");
aoqi@0 101 compile(tmpClasses, new String[] { }, test_java);
aoqi@0 102
aoqi@0 103 // package-info file to use on the command line when requied
aoqi@0 104 cl_pkgInfo_java = writeFile("cl/p/package-info.java", "@Test(\"CL\") package p;");
aoqi@0 105
aoqi@0 106 // source path containing package-info
aoqi@0 107 sp_old = new File("src.old");
aoqi@0 108 writeFile("src.old/p/package-info.java", "@Test(\"SP_OLD\") package p;");
aoqi@0 109
aoqi@0 110 // class path containing package-info
aoqi@0 111 cp_old = new File("classes.old");
aoqi@0 112 compile(cp_old, new String[] { "-classpath", tmpClasses.getPath() },
aoqi@0 113 writeFile("tmp.old/p/package-info.java", "@Test(\"CP_OLD\") package p;"));
aoqi@0 114
aoqi@0 115 // source path containing package-info which is newer than the one in cp-old
aoqi@0 116 sp_new = new File("src.new");
aoqi@0 117 File old_class = new File(cp_old, "p/package-info.class");
aoqi@0 118 writeFile("src.new/p/package-info.java", "@Test(\"SP_NEW\") package p;", old_class);
aoqi@0 119
aoqi@0 120 // class path containing package-info which is newer than the one in sp-old
aoqi@0 121 cp_new = new File("classes.new");
aoqi@0 122 File old_java = new File(sp_old, "p/package-info.java");
aoqi@0 123 compile(cp_new, new String[] { "-classpath", tmpClasses.getPath() },
aoqi@0 124 writeFile("tmp.new/p/package-info.java", "@Test(\"CP_NEW\") package p;", old_java));
aoqi@0 125
aoqi@0 126 // directory containing package-info.java to be "generated" later by annotation processor
aoqi@0 127 sp_gen = new File("src.gen");
aoqi@0 128 writeFile("src.gen/p/package-info.java", "@Test(\"SP_GEN\") package p;");
aoqi@0 129
aoqi@0 130 // directory containing package-info.class to be "generated" later by annotation processor
aoqi@0 131 cp_gen = new File("classes.gen");
aoqi@0 132 compile(cp_gen, new String[] { "-classpath", tmpClasses.getPath() },
aoqi@0 133 writeFile("tmp.gen/p/package-info.java", "@Test(\"CP_GEN\") package p;"));
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 void test(Kind cl, Kind sp, Kind cp) throws Exception {
aoqi@0 137 if (skip(cl, sp, cp))
aoqi@0 138 return;
aoqi@0 139
aoqi@0 140 ++count;
aoqi@0 141 // if test cases specified, skip this test case if not selected
aoqi@0 142 if (tests.size() > 0 && !tests.contains(count))
aoqi@0 143 return;
aoqi@0 144
aoqi@0 145 System.err.println("Test " + count + " cl:" + cl + " sp:" + sp + " cp:" + cp);
aoqi@0 146
aoqi@0 147 // test specific tmp directory
aoqi@0 148 File test_tmp = new File("tmp.test" + count);
aoqi@0 149 test_tmp.mkdirs();
aoqi@0 150
aoqi@0 151 // build up list of options and files to be compiled
aoqi@0 152 List<String> opts = new ArrayList<String>();
aoqi@0 153 List<File> files = new ArrayList<File>();
aoqi@0 154
aoqi@0 155 // expected value for annotation
aoqi@0 156 String expect = null;
aoqi@0 157
aoqi@0 158 opts.add("-processorpath");
aoqi@0 159 String testClasses = System.getProperty("test.classes");
aoqi@0 160 String testClassPath = System.getProperty("test.class.path", testClasses);
aoqi@0 161 opts.add(testClassPath);
aoqi@0 162 opts.add("-processor");
aoqi@0 163 opts.add(Processor.class.getName());
aoqi@0 164 opts.add("-proc:only");
aoqi@0 165 opts.add("-d");
aoqi@0 166 opts.add(test_tmp.getPath());
aoqi@0 167 //opts.add("-verbose");
aoqi@0 168 files.add(test_java);
aoqi@0 169
aoqi@0 170 /*
aoqi@0 171 * Analyze each of cl, cp, sp, building up the options and files to
aoqi@0 172 * be compiled, and determining the expected outcome fo the test case.
aoqi@0 173 */
aoqi@0 174
aoqi@0 175 // command line file: either omitted or given
aoqi@0 176 if (cl == Kind.OLD) {
aoqi@0 177 files.add(cl_pkgInfo_java);
aoqi@0 178 // command line files always supercede files on paths
aoqi@0 179 expect = "CL";
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 // source path:
aoqi@0 183 switch (sp) {
aoqi@0 184 case NONE:
aoqi@0 185 break;
aoqi@0 186
aoqi@0 187 case OLD:
aoqi@0 188 opts.add("-sourcepath");
aoqi@0 189 opts.add(sp_old.getPath());
aoqi@0 190 if (expect == null && cp == Kind.NONE) {
aoqi@0 191 assert cl == Kind.NONE && cp == Kind.NONE;
aoqi@0 192 expect = "SP_OLD";
aoqi@0 193 }
aoqi@0 194 break;
aoqi@0 195
aoqi@0 196 case NEW:
aoqi@0 197 opts.add("-sourcepath");
aoqi@0 198 opts.add(sp_new.getPath());
aoqi@0 199 if (expect == null) {
aoqi@0 200 assert cl == Kind.NONE && cp == Kind.OLD;
aoqi@0 201 expect = "SP_NEW";
aoqi@0 202 }
aoqi@0 203 break;
aoqi@0 204
aoqi@0 205 case GEN:
aoqi@0 206 opts.add("-Agen=" + new File(sp_gen, "p/package-info.java"));
aoqi@0 207 assert cl == Kind.NONE && cp == Kind.NONE;
aoqi@0 208 expect = "SP_GEN";
aoqi@0 209 break;
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 // class path:
aoqi@0 213 switch (cp) {
aoqi@0 214 case NONE:
aoqi@0 215 break;
aoqi@0 216
aoqi@0 217 case OLD:
aoqi@0 218 opts.add("-classpath");
aoqi@0 219 opts.add(cp_old.getPath());
aoqi@0 220 if (expect == null && sp == Kind.NONE) {
aoqi@0 221 assert cl == Kind.NONE && sp == Kind.NONE;
aoqi@0 222 expect = "CP_OLD";
aoqi@0 223 }
aoqi@0 224 break;
aoqi@0 225
aoqi@0 226 case NEW:
aoqi@0 227 opts.add("-classpath");
aoqi@0 228 opts.add(cp_new.getPath());
aoqi@0 229 if (expect == null) {
aoqi@0 230 assert cl == Kind.NONE && sp == Kind.OLD;
aoqi@0 231 expect = "CP_NEW";
aoqi@0 232 }
aoqi@0 233 break;
aoqi@0 234
aoqi@0 235 case GEN:
aoqi@0 236 opts.add("-Agen=" + new File(cp_gen, "p/package-info.class"));
aoqi@0 237 assert cl == Kind.NONE && sp == Kind.NONE;
aoqi@0 238 expect = "CP_GEN";
aoqi@0 239 break;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 // pass expected value to annotation processor
aoqi@0 243 assert expect != null;
aoqi@0 244 opts.add("-Aexpect=" + expect);
aoqi@0 245
aoqi@0 246 // compile the files with the options that have been built up
aoqi@0 247 compile(opts, files);
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 /**
aoqi@0 251 * Return true if this combination of parameters does not identify a useful test case.
aoqi@0 252 */
aoqi@0 253 boolean skip(Kind cl, Kind sp, Kind cp) {
aoqi@0 254 // skip if no package files required
aoqi@0 255 if (cl == Kind.NONE && sp == Kind.NONE && cp == Kind.NONE)
aoqi@0 256 return true;
aoqi@0 257
aoqi@0 258 // skip if both sp and sp are OLD, since results may be indeterminate
aoqi@0 259 if (sp == Kind.OLD && cp == Kind.OLD)
aoqi@0 260 return true;
aoqi@0 261
aoqi@0 262 // skip if sp or cp is NEW but the other is not OLD
aoqi@0 263 if ((sp == Kind.NEW && cp != Kind.OLD) || (cp == Kind.NEW && sp != Kind.OLD))
aoqi@0 264 return true;
aoqi@0 265
aoqi@0 266 // only use GEN if no other package-info files present
aoqi@0 267 if (sp == Kind.GEN && !(cl == Kind.NONE && cp == Kind.NONE) ||
aoqi@0 268 cp == Kind.GEN && !(cl == Kind.NONE && sp == Kind.NONE)) {
aoqi@0 269 return true;
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 // remaining combinations are valid
aoqi@0 273 return false;
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 /** Write a file with a given body. */
aoqi@0 277 File writeFile(String path, String body) throws Exception {
aoqi@0 278 File f = new File(path);
aoqi@0 279 if (f.getParentFile() != null)
aoqi@0 280 f.getParentFile().mkdirs();
aoqi@0 281 Writer out = new FileWriter(path);
aoqi@0 282 try {
aoqi@0 283 out.write(body);
aoqi@0 284 } finally {
aoqi@0 285 out.close();
aoqi@0 286 }
aoqi@0 287 return f;
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 /** Write a file with a given body, ensuring that the file is newer than a reference file. */
aoqi@0 291 File writeFile(String path, String body, File ref) throws Exception {
aoqi@0 292 for (int i = 0; i < 5; i++) {
aoqi@0 293 File f = writeFile(path, body);
aoqi@0 294 if (f.lastModified() > ref.lastModified())
aoqi@0 295 return f;
aoqi@0 296 Thread.sleep(2000);
aoqi@0 297 }
aoqi@0 298 throw new Exception("cannot create file " + path + " newer than " + ref);
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 /** Compile a file to a given directory, with options provided. */
aoqi@0 302 void compile(File dir, String[] opts, File src) throws Exception {
aoqi@0 303 dir.mkdirs();
aoqi@0 304 List<String> opts2 = new ArrayList<String>();
aoqi@0 305 opts2.addAll(Arrays.asList("-d", dir.getPath()));
aoqi@0 306 opts2.addAll(Arrays.asList(opts));
aoqi@0 307 compile(opts2, Collections.singletonList(src));
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 /** Compile files with options provided. */
aoqi@0 311 void compile(List<String> opts, List<File> files) throws Exception {
aoqi@0 312 System.err.println("javac: " + opts + " " + files);
aoqi@0 313 List<String> args = new ArrayList<String>();
aoqi@0 314 args.addAll(opts);
aoqi@0 315 for (File f: files)
aoqi@0 316 args.add(f.getPath());
aoqi@0 317 StringWriter sw = new StringWriter();
aoqi@0 318 PrintWriter pw = new PrintWriter(sw);
aoqi@0 319 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
aoqi@0 320 pw.flush();
aoqi@0 321 if (sw.getBuffer().length() > 0)
aoqi@0 322 System.err.println(sw.toString());
aoqi@0 323 if (rc != 0)
aoqi@0 324 throw new Exception("compilation failed: rc=" + rc);
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 /** Report an error. */
aoqi@0 328 void error(String msg) {
aoqi@0 329 System.err.println("Error: " + msg);
aoqi@0 330 errors++;
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 /** Test case counter. */
aoqi@0 334 int count;
aoqi@0 335
aoqi@0 336 /** Number of errors found. */
aoqi@0 337 int errors;
aoqi@0 338
aoqi@0 339 /** Optional set of test cases to be run; empty implies all test cases. */
aoqi@0 340 Set<Integer> tests = new HashSet<Integer>();
aoqi@0 341
aoqi@0 342 /* Files created by setup. */
aoqi@0 343 File test_java;
aoqi@0 344 File sp_old;
aoqi@0 345 File sp_new;
aoqi@0 346 File sp_gen;
aoqi@0 347 File cp_old;
aoqi@0 348 File cp_new;
aoqi@0 349 File cp_gen;
aoqi@0 350 File cl_pkgInfo_java;
aoqi@0 351
aoqi@0 352 /** Annotation processor used to verify the expected value for the
aoqi@0 353 package annotations found by javac. */
aoqi@0 354 @SupportedOptions({ "gen", "expect" })
aoqi@0 355 public static class Processor extends JavacTestingAbstractProcessor {
aoqi@0 356 public boolean process(Set<? extends TypeElement> annots, RoundEnvironment renv) {
aoqi@0 357 round++;
aoqi@0 358 System.err.println("Round " + round + " annots:" + annots + " rootElems:" + renv.getRootElements());
aoqi@0 359
aoqi@0 360 // if this is the first round and the gen option is given, use the filer to create
aoqi@0 361 // a copy of the file specified by the gen option.
aoqi@0 362 String gen = getOption("gen");
aoqi@0 363 if (round == 1 && gen != null) {
aoqi@0 364 try {
aoqi@0 365 Filer filer = processingEnv.getFiler();
aoqi@0 366 JavaFileObject f;
aoqi@0 367 if (gen.endsWith(".java"))
aoqi@0 368 f = filer.createSourceFile("p.package-info");
aoqi@0 369 else
aoqi@0 370 f = filer.createClassFile("p.package-info");
aoqi@0 371 System.err.println("copy " + gen + " to " + f.getName());
aoqi@0 372 write(f, read(new File(gen)));
aoqi@0 373 } catch (IOException e) {
aoqi@0 374 error("Cannot create package-info file: " + e);
aoqi@0 375 }
aoqi@0 376 }
aoqi@0 377
aoqi@0 378 // if annotation processing is complete, verify the package annotation
aoqi@0 379 // found by the compiler.
aoqi@0 380 if (renv.processingOver()) {
aoqi@0 381 System.err.println("final round");
aoqi@0 382 Elements eu = processingEnv.getElementUtils();
aoqi@0 383 TypeElement te = eu.getTypeElement("p.Test");
aoqi@0 384 PackageElement pe = eu.getPackageOf(te);
aoqi@0 385 System.err.println("final: te:" + te + " pe:" + pe);
aoqi@0 386 List<? extends AnnotationMirror> annos = pe.getAnnotationMirrors();
aoqi@0 387 System.err.println("final: annos:" + annos);
aoqi@0 388 if (annos.size() == 1) {
aoqi@0 389 String expect = "@" + te + "(\"" + getOption("expect") + "\")";
aoqi@0 390 String actual = annos.get(0).toString();
aoqi@0 391 checkEqual("package annotations", actual, expect);
aoqi@0 392 } else {
aoqi@0 393 error("Wrong number of annotations found: (" + annos.size() + ") " + annos);
aoqi@0 394 }
aoqi@0 395 }
aoqi@0 396
aoqi@0 397 return true;
aoqi@0 398 }
aoqi@0 399
aoqi@0 400 /** Get an option given to the annotation processor. */
aoqi@0 401 String getOption(String name) {
aoqi@0 402 return processingEnv.getOptions().get(name);
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 /** Read a file. */
aoqi@0 406 byte[] read(File file) {
aoqi@0 407 byte[] bytes = new byte[(int) file.length()];
aoqi@0 408 DataInputStream in = null;
aoqi@0 409 try {
aoqi@0 410 in = new DataInputStream(new FileInputStream(file));
aoqi@0 411 in.readFully(bytes);
aoqi@0 412 } catch (IOException e) {
aoqi@0 413 error("Error reading file: " + e);
aoqi@0 414 } finally {
aoqi@0 415 if (in != null) {
aoqi@0 416 try {
aoqi@0 417 in.close();
aoqi@0 418 } catch (IOException e) {
aoqi@0 419 error("Error closing file: " + e);
aoqi@0 420 }
aoqi@0 421 }
aoqi@0 422 }
aoqi@0 423 return bytes;
aoqi@0 424 }
aoqi@0 425
aoqi@0 426 /** Write a file. */
aoqi@0 427 void write(JavaFileObject file, byte[] bytes) {
aoqi@0 428 OutputStream out = null;
aoqi@0 429 try {
aoqi@0 430 out = file.openOutputStream();
aoqi@0 431 out.write(bytes, 0, bytes.length);
aoqi@0 432 } catch (IOException e) {
aoqi@0 433 error("Error writing file: " + e);
aoqi@0 434 } finally {
aoqi@0 435 if (out != null) {
aoqi@0 436 try {
aoqi@0 437 out.close();
aoqi@0 438 } catch (IOException e) {
aoqi@0 439 error("Error closing file: " + e);
aoqi@0 440 }
aoqi@0 441 }
aoqi@0 442 }
aoqi@0 443 }
aoqi@0 444
aoqi@0 445 /** Check two strings are equal, and report an error if they are not. */
aoqi@0 446 private void checkEqual(String label, String actual, String expect) {
aoqi@0 447 if (!actual.equals(expect)) {
aoqi@0 448 error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
aoqi@0 449 }
aoqi@0 450 }
aoqi@0 451
aoqi@0 452 /** Report an error to the annotation processing system. */
aoqi@0 453 void error(String msg) {
aoqi@0 454 Messager messager = processingEnv.getMessager();
aoqi@0 455 messager.printMessage(Diagnostic.Kind.ERROR, msg);
aoqi@0 456 }
aoqi@0 457
aoqi@0 458 int round;
aoqi@0 459 }
aoqi@0 460 }

mercurial