1 /*
2 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
171 private ServiceLoader<Processor> serviceLoader;
172 private SecurityException processorLoaderException;
173
174 private final JavaFileManager fileManager;
175
176 /**
177 * JavacMessages object used for localization
178 */
179 private JavacMessages messages;
180
181 private MultiTaskListener taskListener;
182 private final Symtab symtab;
183 private final DeferredCompletionFailureHandler dcfh;
184 private final Names names;
185 private final Enter enter;
186 private final Completer initialCompleter;
187 private final Check chk;
188
189 private final Context context;
190
191 /** Get the JavacProcessingEnvironment instance for this context. */
192 public static JavacProcessingEnvironment instance(Context context) {
193 JavacProcessingEnvironment instance = context.get(JavacProcessingEnvironment.class);
194 if (instance == null)
195 instance = new JavacProcessingEnvironment(context);
196 return instance;
197 }
198
199 protected JavacProcessingEnvironment(Context context) {
200 this.context = context;
201 context.put(JavacProcessingEnvironment.class, this);
202 log = Log.instance(context);
203 source = Source.instance(context);
204 diags = JCDiagnostic.Factory.instance(context);
205 options = Options.instance(context);
206 printProcessorInfo = options.isSet(Option.XPRINTPROCESSORINFO);
207 printRounds = options.isSet(Option.XPRINTROUNDS);
208 verbose = options.isSet(Option.VERBOSE);
209 lint = Lint.instance(context).isEnabled(PROCESSING);
210 compiler = JavaCompiler.instance(context);
219
220 // Initialize services before any processors are initialized
221 // in case processors use them.
222 filer = new JavacFiler(context);
223 messager = new JavacMessager(context, this);
224 elementUtils = JavacElements.instance(context);
225 typeUtils = JavacTypes.instance(context);
226 modules = Modules.instance(context);
227 types = Types.instance(context);
228 annotate = Annotate.instance(context);
229 processorOptions = initProcessorOptions();
230 unmatchedProcessorOptions = initUnmatchedProcessorOptions();
231 messages = JavacMessages.instance(context);
232 taskListener = MultiTaskListener.instance(context);
233 symtab = Symtab.instance(context);
234 dcfh = DeferredCompletionFailureHandler.instance(context);
235 names = Names.instance(context);
236 enter = Enter.instance(context);
237 initialCompleter = ClassFinder.instance(context).getCompleter();
238 chk = Check.instance(context);
239 initProcessorLoader();
240 }
241
242 public void setProcessors(Iterable<? extends Processor> processors) {
243 Assert.checkNull(discoveredProcs);
244 initProcessorIterator(processors);
245 }
246
247 private Set<String> initPlatformAnnotations() {
248 final String module_prefix =
249 Feature.MODULES.allowedInSource(source) ? "java.base/" : "";
250 return Set.of(module_prefix + "java.lang.Deprecated",
251 module_prefix + "java.lang.FunctionalInterface",
252 module_prefix + "java.lang.Override",
253 module_prefix + "java.lang.SafeVarargs",
254 module_prefix + "java.lang.SuppressWarnings",
255
256 module_prefix + "java.lang.annotation.Documented",
257 module_prefix + "java.lang.annotation.Inherited",
258 module_prefix + "java.lang.annotation.Native",
1656 }
1657
1658 @DefinedBy(Api.ANNOTATION_PROCESSING)
1659 public JavacElements getElementUtils() {
1660 return elementUtils;
1661 }
1662
1663 @DefinedBy(Api.ANNOTATION_PROCESSING)
1664 public JavacTypes getTypeUtils() {
1665 return typeUtils;
1666 }
1667
1668 @DefinedBy(Api.ANNOTATION_PROCESSING)
1669 public SourceVersion getSourceVersion() {
1670 return Source.toSourceVersion(source);
1671 }
1672
1673 @DefinedBy(Api.ANNOTATION_PROCESSING)
1674 public Locale getLocale() {
1675 return messages.getCurrentLocale();
1676 }
1677
1678 public Set<Symbol.PackageSymbol> getSpecifiedPackages() {
1679 return specifiedPackages;
1680 }
1681
1682 public static final Pattern noMatches = Pattern.compile("(\\P{all})+");
1683
1684 /**
1685 * Convert import-style string for supported annotations into a
1686 * regex matching that string. If the string is not a valid
1687 * import-style string, return a regex that won't match anything.
1688 */
1689 private static Pattern importStringToPattern(boolean allowModules, String s, Processor p, Log log) {
1690 String module;
1691 String pkg;
1692 int slash = s.indexOf('/');
1693 if (slash == (-1)) {
1694 if (s.equals("*")) {
1695 return MatchingUtils.validImportStringToPattern(s);
|
1 /*
2 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
171 private ServiceLoader<Processor> serviceLoader;
172 private SecurityException processorLoaderException;
173
174 private final JavaFileManager fileManager;
175
176 /**
177 * JavacMessages object used for localization
178 */
179 private JavacMessages messages;
180
181 private MultiTaskListener taskListener;
182 private final Symtab symtab;
183 private final DeferredCompletionFailureHandler dcfh;
184 private final Names names;
185 private final Enter enter;
186 private final Completer initialCompleter;
187 private final Check chk;
188
189 private final Context context;
190
191 /**
192 * Support for preview language features.
193 */
194 private final Preview preview;
195
196 /** Get the JavacProcessingEnvironment instance for this context. */
197 public static JavacProcessingEnvironment instance(Context context) {
198 JavacProcessingEnvironment instance = context.get(JavacProcessingEnvironment.class);
199 if (instance == null)
200 instance = new JavacProcessingEnvironment(context);
201 return instance;
202 }
203
204 protected JavacProcessingEnvironment(Context context) {
205 this.context = context;
206 context.put(JavacProcessingEnvironment.class, this);
207 log = Log.instance(context);
208 source = Source.instance(context);
209 diags = JCDiagnostic.Factory.instance(context);
210 options = Options.instance(context);
211 printProcessorInfo = options.isSet(Option.XPRINTPROCESSORINFO);
212 printRounds = options.isSet(Option.XPRINTROUNDS);
213 verbose = options.isSet(Option.VERBOSE);
214 lint = Lint.instance(context).isEnabled(PROCESSING);
215 compiler = JavaCompiler.instance(context);
224
225 // Initialize services before any processors are initialized
226 // in case processors use them.
227 filer = new JavacFiler(context);
228 messager = new JavacMessager(context, this);
229 elementUtils = JavacElements.instance(context);
230 typeUtils = JavacTypes.instance(context);
231 modules = Modules.instance(context);
232 types = Types.instance(context);
233 annotate = Annotate.instance(context);
234 processorOptions = initProcessorOptions();
235 unmatchedProcessorOptions = initUnmatchedProcessorOptions();
236 messages = JavacMessages.instance(context);
237 taskListener = MultiTaskListener.instance(context);
238 symtab = Symtab.instance(context);
239 dcfh = DeferredCompletionFailureHandler.instance(context);
240 names = Names.instance(context);
241 enter = Enter.instance(context);
242 initialCompleter = ClassFinder.instance(context).getCompleter();
243 chk = Check.instance(context);
244 preview = Preview.instance(context);
245 initProcessorLoader();
246 }
247
248 public void setProcessors(Iterable<? extends Processor> processors) {
249 Assert.checkNull(discoveredProcs);
250 initProcessorIterator(processors);
251 }
252
253 private Set<String> initPlatformAnnotations() {
254 final String module_prefix =
255 Feature.MODULES.allowedInSource(source) ? "java.base/" : "";
256 return Set.of(module_prefix + "java.lang.Deprecated",
257 module_prefix + "java.lang.FunctionalInterface",
258 module_prefix + "java.lang.Override",
259 module_prefix + "java.lang.SafeVarargs",
260 module_prefix + "java.lang.SuppressWarnings",
261
262 module_prefix + "java.lang.annotation.Documented",
263 module_prefix + "java.lang.annotation.Inherited",
264 module_prefix + "java.lang.annotation.Native",
1662 }
1663
1664 @DefinedBy(Api.ANNOTATION_PROCESSING)
1665 public JavacElements getElementUtils() {
1666 return elementUtils;
1667 }
1668
1669 @DefinedBy(Api.ANNOTATION_PROCESSING)
1670 public JavacTypes getTypeUtils() {
1671 return typeUtils;
1672 }
1673
1674 @DefinedBy(Api.ANNOTATION_PROCESSING)
1675 public SourceVersion getSourceVersion() {
1676 return Source.toSourceVersion(source);
1677 }
1678
1679 @DefinedBy(Api.ANNOTATION_PROCESSING)
1680 public Locale getLocale() {
1681 return messages.getCurrentLocale();
1682 }
1683
1684 @DefinedBy(Api.ANNOTATION_PROCESSING)
1685 public boolean isPreviewEnabled() {
1686 return preview.isEnabled();
1687 }
1688
1689 public Set<Symbol.PackageSymbol> getSpecifiedPackages() {
1690 return specifiedPackages;
1691 }
1692
1693 public static final Pattern noMatches = Pattern.compile("(\\P{all})+");
1694
1695 /**
1696 * Convert import-style string for supported annotations into a
1697 * regex matching that string. If the string is not a valid
1698 * import-style string, return a regex that won't match anything.
1699 */
1700 private static Pattern importStringToPattern(boolean allowModules, String s, Processor p, Log log) {
1701 String module;
1702 String pkg;
1703 int slash = s.indexOf('/');
1704 if (slash == (-1)) {
1705 if (s.equals("*")) {
1706 return MatchingUtils.validImportStringToPattern(s);
|