author | jjg |
Thu, 30 Oct 2014 16:08:43 -0700 | |
changeset 27379 | 5ae894733e09 |
parent 26266 | 2d24bda701dc |
child 29291 | 076c277565f7 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22163
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.api; |
|
27 |
||
27379
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
28 |
import java.io.IOException; |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
29 |
import java.nio.CharBuffer; |
10 | 30 |
import java.util.*; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
31 |
import java.util.concurrent.Callable; |
10 | 32 |
import java.util.concurrent.atomic.AtomicBoolean; |
33 |
||
34 |
import javax.annotation.processing.Processor; |
|
35 |
import javax.lang.model.element.Element; |
|
36 |
import javax.lang.model.element.TypeElement; |
|
37 |
import javax.tools.*; |
|
38 |
||
39 |
import com.sun.source.tree.*; |
|
40 |
import com.sun.tools.javac.code.*; |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
41 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
10 | 42 |
import com.sun.tools.javac.comp.*; |
43 |
import com.sun.tools.javac.main.*; |
|
12016 | 44 |
import com.sun.tools.javac.main.JavaCompiler; |
10 | 45 |
import com.sun.tools.javac.parser.Parser; |
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
46 |
import com.sun.tools.javac.parser.ParserFactory; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
47 |
import com.sun.tools.javac.processing.AnnotationProcessingError; |
10 | 48 |
import com.sun.tools.javac.tree.*; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
49 |
import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
50 |
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
10 | 51 |
import com.sun.tools.javac.util.*; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
52 |
import com.sun.tools.javac.util.DefinedBy.Api; |
10 | 53 |
import com.sun.tools.javac.util.List; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
54 |
import com.sun.tools.javac.util.Log.PrefixKind; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
55 |
import com.sun.tools.javac.util.Log.WriterKind; |
10 | 56 |
|
57 |
/** |
|
5848
c5a4ce47e780
6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5847
diff
changeset
|
58 |
* Provides access to functionality specific to the JDK Java Compiler, javac. |
10 | 59 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
60 |
* <p><b>This is NOT part of any supported API. |
10 | 61 |
* If you write code that depends on this, you do so at your own |
62 |
* risk. This code and its internal interfaces are subject to change |
|
63 |
* or deletion without notice.</b></p> |
|
64 |
* |
|
65 |
* @author Peter von der Ahé |
|
66 |
* @author Jonathan Gibbons |
|
67 |
*/ |
|
12016 | 68 |
public class JavacTaskImpl extends BasicJavacTask { |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
69 |
private final Arguments args; |
10 | 70 |
private JavaCompiler compiler; |
27379
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
71 |
private JavaFileManager fileManager; |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1355
diff
changeset
|
72 |
private Locale locale; |
10 | 73 |
private Map<JavaFileObject, JCCompilationUnit> notYetEntered; |
74 |
private ListBuffer<Env<AttrContext>> genList; |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
12016
diff
changeset
|
75 |
private final AtomicBoolean used = new AtomicBoolean(); |
10 | 76 |
private Iterable<? extends Processor> processors; |
77 |
||
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
78 |
JavacTaskImpl(Context context) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
79 |
super(context, true); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
80 |
args = Arguments.instance(context); |
27379
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
81 |
fileManager = context.get(JavaFileManager.class); |
10 | 82 |
} |
83 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
84 |
@Override @DefinedBy(Api.COMPILER) |
16304
475504933a2d
8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
14952
diff
changeset
|
85 |
public Boolean call() { |
475504933a2d
8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
14952
diff
changeset
|
86 |
return doCall().isOK(); |
475504933a2d
8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
14952
diff
changeset
|
87 |
} |
475504933a2d
8006212: javac, convert jtreg tests from shell script to java
vromero
parents:
14952
diff
changeset
|
88 |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
89 |
/* Internal version of call exposing Main.Result. */ |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
90 |
public Main.Result doCall() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
91 |
try { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
92 |
return handleExceptions(new Callable<Main.Result>() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
93 |
@Override |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
94 |
public Main.Result call() throws Exception { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
95 |
prepareCompiler(false); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
96 |
compiler.compile(args.getFileObjects(), args.getClassNames(), processors); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
97 |
return (compiler.errorCount() > 0) ? Main.Result.ERROR : Main.Result.OK; // FIXME? |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
98 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
99 |
}, Main.Result.SYSERR, Main.Result.ABNORMAL); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
100 |
} finally { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
101 |
try { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
102 |
cleanup(); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
103 |
} catch (ClientCodeException e) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
104 |
throw new RuntimeException(e.getCause()); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
105 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
106 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
107 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
108 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
109 |
@Override @DefinedBy(Api.COMPILER) |
10 | 110 |
public void setProcessors(Iterable<? extends Processor> processors) { |
111 |
processors.getClass(); // null check |
|
112 |
// not mt-safe |
|
113 |
if (used.get()) |
|
114 |
throw new IllegalStateException(); |
|
115 |
this.processors = processors; |
|
116 |
} |
|
117 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
118 |
@Override @DefinedBy(Api.COMPILER) |
10 | 119 |
public void setLocale(Locale locale) { |
120 |
if (used.get()) |
|
121 |
throw new IllegalStateException(); |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1355
diff
changeset
|
122 |
this.locale = locale; |
10 | 123 |
} |
124 |
||
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
125 |
private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorResult) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
126 |
try { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
127 |
return c.call(); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
128 |
} catch (FatalError ex) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
129 |
Log log = Log.instance(context); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
130 |
Options options = Options.instance(context); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
131 |
log.printRawLines(ex.getMessage()); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
132 |
if (ex.getCause() != null && options.isSet("dev")) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
133 |
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
134 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
135 |
return sysErrorResult; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
136 |
} catch (AnnotationProcessingError | ClientCodeException e) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
137 |
// AnnotationProcessingError is thrown from JavacProcessingEnvironment, |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
138 |
// to forward errors thrown from an annotation processor |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
139 |
// ClientCodeException is thrown from ClientCodeWrapper, |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
140 |
// to forward errors thrown from user-supplied code for Compiler API |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
141 |
// as specified by javax.tools.JavaCompiler#getTask |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
142 |
// and javax.tools.JavaCompiler.CompilationTask#call |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
143 |
throw new RuntimeException(e.getCause()); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
144 |
} catch (PropagatedException e) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
145 |
throw e.getCause(); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
146 |
} catch (IllegalStateException e) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
147 |
throw e; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
148 |
} catch (Exception | Error ex) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
149 |
// Nasty. If we've already reported an error, compensate |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
150 |
// for buggy compiler error recovery by swallowing thrown |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
151 |
// exceptions. |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
152 |
if (compiler == null || compiler.errorCount() == 0 |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
153 |
|| Options.instance(context).isSet("dev")) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
154 |
Log log = Log.instance(context); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
155 |
log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version()); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
156 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
157 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
158 |
return abnormalErrorResult; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
159 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
160 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
161 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
162 |
private void prepareCompiler(boolean forParse) { |
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
163 |
if (used.getAndSet(true)) { |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
164 |
if (compiler == null) |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
165 |
throw new PropagatedException(new IllegalStateException()); |
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
166 |
} else { |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
167 |
args.validate(); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
168 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
169 |
//initialize compiler's default locale |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
170 |
context.put(Locale.class, locale); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
171 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
172 |
// hack |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
173 |
JavacMessages messages = context.get(JavacMessages.messagesKey); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
174 |
if (messages != null && !messages.getCurrentLocale().equals(locale)) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
175 |
messages.setCurrentLocale(locale); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
176 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
177 |
initPlugins(args.getPluginOpts()); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
178 |
initDocLint(args.getDocLintOpts()); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
179 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
180 |
// init JavaCompiler and queues |
10 | 181 |
compiler = JavaCompiler.instance(context); |
182 |
compiler.keepComments = true; |
|
183 |
compiler.genEndPos = true; |
|
22163 | 184 |
notYetEntered = new HashMap<>(); |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
185 |
if (forParse) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
186 |
compiler.initProcessAnnotations(processors); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
187 |
for (JavaFileObject file: args.getFileObjects()) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
188 |
notYetEntered.put(file, null); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
189 |
genList = new ListBuffer<>(); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
190 |
} |
10 | 191 |
} |
192 |
} |
|
193 |
||
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
194 |
<T> String toString(Iterable<T> items, String sep) { |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
195 |
String currSep = ""; |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
196 |
StringBuilder sb = new StringBuilder(); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
197 |
for (T item: items) { |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
198 |
sb.append(currSep); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
199 |
sb.append(item.toString()); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
200 |
currSep = sep; |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
201 |
} |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
202 |
return sb.toString(); |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
203 |
} |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
204 |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
205 |
void cleanup() { |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
206 |
if (compiler != null) |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
207 |
compiler.close(); |
27379
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
208 |
if (fileManager instanceof BaseFileManager && ((BaseFileManager) fileManager).autoClose) { |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
209 |
try { |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
210 |
fileManager.close(); |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
211 |
} catch (IOException ignore) { |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
212 |
} |
5ae894733e09
8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents:
26266
diff
changeset
|
213 |
} |
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
214 |
compiler = null; |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
215 |
context = null; |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
216 |
notYetEntered = null; |
10 | 217 |
} |
218 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
219 |
@Override @DefinedBy(Api.COMPILER_TREE) |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
220 |
public Iterable<? extends CompilationUnitTree> parse() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
221 |
return handleExceptions(new Callable<Iterable<? extends CompilationUnitTree>>() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
222 |
@Override |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
223 |
public Iterable<? extends CompilationUnitTree> call() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
224 |
return parseInternal(); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
225 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
226 |
}, List.<CompilationUnitTree>nil(), List.<CompilationUnitTree>nil()); |
10 | 227 |
} |
228 |
||
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
229 |
private Iterable<? extends CompilationUnitTree> parseInternal() { |
10 | 230 |
try { |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
231 |
prepareCompiler(true); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
232 |
List<JCCompilationUnit> units = compiler.parseFiles(args.getFileObjects()); |
10 | 233 |
for (JCCompilationUnit unit: units) { |
234 |
JavaFileObject file = unit.getSourceFile(); |
|
235 |
if (notYetEntered.containsKey(file)) |
|
236 |
notYetEntered.put(file, unit); |
|
237 |
} |
|
238 |
return units; |
|
239 |
} |
|
240 |
finally { |
|
241 |
parsed = true; |
|
242 |
if (compiler != null && compiler.log != null) |
|
243 |
compiler.log.flush(); |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
private boolean parsed = false; |
|
248 |
||
249 |
/** |
|
250 |
* Translate all the abstract syntax trees to elements. |
|
251 |
* |
|
252 |
* @return a list of elements corresponding to the top level |
|
253 |
* classes in the abstract syntax trees |
|
254 |
*/ |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
255 |
public Iterable<? extends TypeElement> enter() { |
10 | 256 |
return enter(null); |
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* Translate the given abstract syntax trees to elements. |
|
261 |
* |
|
262 |
* @param trees a list of abstract syntax trees. |
|
263 |
* @return a list of elements corresponding to the top level |
|
264 |
* classes in the abstract syntax trees |
|
265 |
*/ |
|
266 |
public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees) |
|
267 |
{ |
|
10631
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
10193
diff
changeset
|
268 |
if (trees == null && notYetEntered != null && notYetEntered.isEmpty()) |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
10193
diff
changeset
|
269 |
return List.nil(); |
d9914010b902
7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents:
10193
diff
changeset
|
270 |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
271 |
prepareCompiler(true); |
10 | 272 |
|
273 |
ListBuffer<JCCompilationUnit> roots = null; |
|
274 |
||
275 |
if (trees == null) { |
|
276 |
// If there are still files which were specified to be compiled |
|
277 |
// (i.e. in fileObjects) but which have not yet been entered, |
|
278 |
// then we make sure they have been parsed and add them to the |
|
279 |
// list to be entered. |
|
280 |
if (notYetEntered.size() > 0) { |
|
281 |
if (!parsed) |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
282 |
parseInternal(); // TODO would be nice to specify files needed to be parsed |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
283 |
for (JavaFileObject file: args.getFileObjects()) { |
10 | 284 |
JCCompilationUnit unit = notYetEntered.remove(file); |
285 |
if (unit != null) { |
|
286 |
if (roots == null) |
|
22163 | 287 |
roots = new ListBuffer<>(); |
10 | 288 |
roots.append(unit); |
289 |
} |
|
290 |
} |
|
291 |
notYetEntered.clear(); |
|
292 |
} |
|
293 |
} |
|
294 |
else { |
|
295 |
for (CompilationUnitTree cu : trees) { |
|
296 |
if (cu instanceof JCCompilationUnit) { |
|
297 |
if (roots == null) |
|
22163 | 298 |
roots = new ListBuffer<>(); |
10 | 299 |
roots.append((JCCompilationUnit)cu); |
300 |
notYetEntered.remove(cu.getSourceFile()); |
|
301 |
} |
|
302 |
else |
|
303 |
throw new IllegalArgumentException(cu.toString()); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
if (roots == null) |
|
308 |
return List.nil(); |
|
309 |
||
310 |
try { |
|
311 |
List<JCCompilationUnit> units = compiler.enterTrees(roots.toList()); |
|
312 |
||
313 |
if (notYetEntered.isEmpty()) |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22163
diff
changeset
|
314 |
compiler.processAnnotations(units); |
10 | 315 |
|
22163 | 316 |
ListBuffer<TypeElement> elements = new ListBuffer<>(); |
10 | 317 |
for (JCCompilationUnit unit : units) { |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
318 |
for (JCTree node : unit.defs) { |
10950 | 319 |
if (node.hasTag(JCTree.Tag.CLASSDEF)) { |
6716
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
320 |
JCClassDecl cdef = (JCClassDecl) node; |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
321 |
if (cdef.sym != null) // maybe null if errors in anno processing |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
322 |
elements.append(cdef.sym); |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
323 |
} |
71df48777dd1
6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents:
5848
diff
changeset
|
324 |
} |
10 | 325 |
} |
326 |
return elements.toList(); |
|
327 |
} |
|
328 |
finally { |
|
329 |
compiler.log.flush(); |
|
330 |
} |
|
331 |
} |
|
332 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
333 |
@Override @DefinedBy(Api.COMPILER_TREE) |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
334 |
public Iterable<? extends Element> analyze() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
335 |
return handleExceptions(new Callable<Iterable<? extends Element>>() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
336 |
@Override |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
337 |
public Iterable<? extends Element> call() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
338 |
return analyze(null); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
339 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
340 |
}, List.<Element>nil(), List.<Element>nil()); |
10 | 341 |
} |
342 |
||
343 |
/** |
|
344 |
* Complete all analysis on the given classes. |
|
345 |
* This can be used to ensure that all compile time errors are reported. |
|
346 |
* The classes must have previously been returned from {@link #enter}. |
|
347 |
* If null is specified, all outstanding classes will be analyzed. |
|
348 |
* |
|
349 |
* @param classes a list of class elements |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
350 |
* @return the elements that were analyzed |
10 | 351 |
*/ |
352 |
// This implementation requires that we open up privileges on JavaCompiler. |
|
353 |
// An alternative implementation would be to move this code to JavaCompiler and |
|
354 |
// wrap it here |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
355 |
public Iterable<? extends Element> analyze(Iterable<? extends TypeElement> classes) { |
10 | 356 |
enter(null); // ensure all classes have been entered |
357 |
||
22163 | 358 |
final ListBuffer<Element> results = new ListBuffer<>(); |
10 | 359 |
try { |
360 |
if (classes == null) { |
|
361 |
handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results); |
|
362 |
} else { |
|
363 |
Filter f = new Filter() { |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
364 |
@Override |
10 | 365 |
public void process(Env<AttrContext> env) { |
366 |
handleFlowResults(compiler.flow(compiler.attribute(env)), results); |
|
367 |
} |
|
368 |
}; |
|
369 |
f.run(compiler.todo, classes); |
|
370 |
} |
|
371 |
} finally { |
|
372 |
compiler.log.flush(); |
|
373 |
} |
|
374 |
return results; |
|
375 |
} |
|
376 |
// where |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
377 |
private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) { |
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
378 |
for (Env<AttrContext> env: queue) { |
10 | 379 |
switch (env.tree.getTag()) { |
10950 | 380 |
case CLASSDEF: |
10 | 381 |
JCClassDecl cdef = (JCClassDecl) env.tree; |
382 |
if (cdef.sym != null) |
|
383 |
elems.append(cdef.sym); |
|
384 |
break; |
|
10950 | 385 |
case TOPLEVEL: |
10 | 386 |
JCCompilationUnit unit = (JCCompilationUnit) env.tree; |
387 |
if (unit.packge != null) |
|
388 |
elems.append(unit.packge); |
|
389 |
break; |
|
390 |
} |
|
391 |
} |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
392 |
genList.addAll(queue); |
10 | 393 |
} |
394 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
395 |
@Override @DefinedBy(Api.COMPILER_TREE) |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
396 |
public Iterable<? extends JavaFileObject> generate() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
397 |
return handleExceptions(new Callable<Iterable<? extends JavaFileObject>>() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
398 |
@Override |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
399 |
public Iterable<? extends JavaFileObject> call() { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
400 |
return generate(null); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
401 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
402 |
}, List.<JavaFileObject>nil(), List.<JavaFileObject>nil()); |
10 | 403 |
} |
404 |
||
405 |
/** |
|
406 |
* Generate code corresponding to the given classes. |
|
407 |
* The classes must have previously been returned from {@link #enter}. |
|
408 |
* If there are classes outstanding to be analyzed, that will be done before |
|
409 |
* any classes are generated. |
|
410 |
* If null is specified, code will be generated for all outstanding classes. |
|
411 |
* |
|
412 |
* @param classes a list of class elements |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
413 |
* @return the files that were generated |
10 | 414 |
*/ |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
415 |
public Iterable<? extends JavaFileObject> generate(Iterable<? extends TypeElement> classes) { |
22163 | 416 |
final ListBuffer<JavaFileObject> results = new ListBuffer<>(); |
10 | 417 |
try { |
418 |
analyze(null); // ensure all classes have been parsed, entered, and analyzed |
|
419 |
||
420 |
if (classes == null) { |
|
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
421 |
compiler.generate(compiler.desugar(genList), results); |
10 | 422 |
genList.clear(); |
423 |
} |
|
424 |
else { |
|
425 |
Filter f = new Filter() { |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
426 |
@Override |
10 | 427 |
public void process(Env<AttrContext> env) { |
864
b1cf6afb8244
6724551: Use Queues instead of Lists to link compiler phases
jjg
parents:
735
diff
changeset
|
428 |
compiler.generate(compiler.desugar(ListBuffer.of(env)), results); |
10 | 429 |
} |
430 |
}; |
|
431 |
f.run(genList, classes); |
|
432 |
} |
|
433 |
if (genList.isEmpty()) { |
|
434 |
compiler.reportDeferredDiagnostics(); |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
435 |
cleanup(); |
10 | 436 |
} |
437 |
} |
|
438 |
finally { |
|
8844
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
439 |
if (compiler != null) |
18170a311747
7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents:
6716
diff
changeset
|
440 |
compiler.log.flush(); |
10 | 441 |
} |
442 |
return results; |
|
443 |
} |
|
444 |
||
445 |
public Iterable<? extends Tree> pathFor(CompilationUnitTree unit, Tree node) { |
|
446 |
return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse(); |
|
447 |
} |
|
448 |
||
449 |
abstract class Filter { |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
450 |
void run(Queue<Env<AttrContext>> list, Iterable<? extends TypeElement> classes) { |
22163 | 451 |
Set<TypeElement> set = new HashSet<>(); |
10 | 452 |
for (TypeElement item: classes) |
453 |
set.add(item); |
|
454 |
||
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
16316
diff
changeset
|
455 |
ListBuffer<Env<AttrContext>> defer = new ListBuffer<>(); |
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
456 |
while (list.peek() != null) { |
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
457 |
Env<AttrContext> env = list.remove(); |
10 | 458 |
ClassSymbol csym = env.enclClass.sym; |
459 |
if (csym != null && set.contains(csym.outermostClass())) |
|
460 |
process(env); |
|
461 |
else |
|
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
462 |
defer = defer.append(env); |
10 | 463 |
} |
464 |
||
1355
74cc97efef51
6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents:
1258
diff
changeset
|
465 |
list.addAll(defer); |
10 | 466 |
} |
467 |
||
468 |
abstract void process(Env<AttrContext> env); |
|
469 |
} |
|
470 |
||
471 |
/** |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
472 |
* For internal use only. This method will be |
10 | 473 |
* removed without warning. |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
474 |
* @param expr the type expression to be analyzed |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
475 |
* @param scope the scope in which to analyze the type expression |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
476 |
* @return the type |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
477 |
* @throws IllegalArgumentException if the type expression of null or empty |
10 | 478 |
*/ |
479 |
public Type parseType(String expr, TypeElement scope) { |
|
480 |
if (expr == null || expr.equals("")) |
|
481 |
throw new IllegalArgumentException(); |
|
482 |
compiler = JavaCompiler.instance(context); |
|
483 |
JavaFileObject prev = compiler.log.useSource(null); |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
484 |
ParserFactory parserFactory = ParserFactory.instance(context); |
10 | 485 |
Attr attr = Attr.instance(context); |
486 |
try { |
|
1258
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
487 |
CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
488 |
Parser parser = parserFactory.newParser(buf, false, false, false); |
1cf37d8837d1
6724118: change JavaCompiler to not use Scanner directly
jjg
parents:
864
diff
changeset
|
489 |
JCTree tree = parser.parseType(); |
10 | 490 |
return attr.attribType(tree, (Symbol.TypeSymbol)scope); |
491 |
} finally { |
|
492 |
compiler.log.useSource(prev); |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
} |