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