Tue, 20 Dec 2016 15:24:55 -0800
Added tag jdk8u152-b00 for changeset 03e31cfcd499
1 /*
2 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 6358168
27 * @summary JavaCompiler.hasBeenUsed is not set in delegateCompiler
28 */
30 import java.io.*;
31 import java.net.*;
32 import java.util.*;
33 import javax.annotation.processing.*;
34 import javax.lang.model.element.*;
35 import javax.tools.*;
36 import com.sun.tools.javac.file.*;
37 import com.sun.tools.javac.file.JavacFileManager;
38 import com.sun.tools.javac.main.JavaCompiler;
39 import com.sun.tools.javac.main.*;
40 import com.sun.tools.javac.util.*;
41 import com.sun.tools.javac.util.List; // disambiguate
44 @SupportedAnnotationTypes("*")
45 public class T6358168 extends AbstractProcessor {
46 private static String testClasses = System.getProperty("test.classes");
47 private static String testSrc = System.getProperty("test.src");
48 private static String self = T6358168.class.getName();
50 public static void main(String... args) throws Throwable {
52 JavacFileManager fm = new JavacFileManager(new Context(), false, null);
53 JavaFileObject f = fm.getFileForInput(testSrc + File.separatorChar + T6358168.class.getName() + ".java");
55 try {
56 // first, test case with no annotation processing
57 testNoAnnotationProcessing(fm, f);
59 // now, test case with annotation processing
60 testAnnotationProcessing(fm, f);
61 }
62 catch (Throwable t) {
63 AssertionError e = new AssertionError();
64 e.initCause(t);
65 throw e;
66 }
67 }
69 static void testNoAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
70 Context context = new Context();
71 fm.setContext(context);
73 Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
74 compilerMain.setOptions(Options.instance(context));
75 compilerMain.filenames = new LinkedHashSet<File>();
76 compilerMain.processArgs(new String[] { "-d", "." });
78 JavaCompiler compiler = JavaCompiler.instance(context);
79 compiler.compile(List.of(f));
80 try {
81 compiler.compile(List.of(f));
82 throw new Error("Error: AssertionError not thrown after second call of compile");
83 } catch (AssertionError e) {
84 System.err.println("Exception from compiler (expected): " + e);
85 }
86 }
88 static void testAnnotationProcessing(JavacFileManager fm, JavaFileObject f) throws Throwable {
89 Context context = new Context();
90 fm.setContext(context);
92 Main compilerMain = new Main("javac", new PrintWriter(System.err, true));
93 compilerMain.setOptions(Options.instance(context));
94 compilerMain.filenames = new LinkedHashSet<File>();
95 compilerMain.processArgs(new String[] {
96 "-XprintRounds",
97 "-processorpath", testClasses,
98 "-processor", self,
99 "-d", "."});
101 JavaCompiler compiler = JavaCompiler.instance(context);
102 compiler.initProcessAnnotations(null);
103 JavaCompiler compiler2 = compiler.processAnnotations(compiler.enterTrees(compiler.parseFiles(List.of(f))));
104 try {
105 compiler2.compile(List.of(f));
106 throw new Error("Error: AssertionError not thrown after second call of compile");
107 } catch (AssertionError e) {
108 System.err.println("Exception from compiler (expected): " + e);
109 }
110 }
112 public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
113 return true;
114 }
115 }