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