1 /*
2 * Copyright (c) 2009, 2025, 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
23 * questions.
24 */
25
26 package com.sun.tools.javac.file;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.ref.SoftReference;
31 import java.lang.reflect.Constructor;
32 import java.net.URL;
33 import java.net.URLClassLoader;
34 import java.nio.ByteBuffer;
35 import java.nio.CharBuffer;
36 import java.nio.charset.Charset;
37 import java.nio.charset.CharsetDecoder;
38 import java.nio.charset.CoderResult;
39 import java.nio.charset.CodingErrorAction;
40 import java.nio.charset.IllegalCharsetNameException;
41 import java.nio.charset.UnsupportedCharsetException;
42 import java.nio.file.NoSuchFileException;
43 import java.nio.file.Path;
44 import java.util.Collection;
45 import java.util.HashMap;
46 import java.util.HashSet;
47 import java.util.Iterator;
48 import java.util.Map;
49 import java.util.Objects;
50 import java.util.Set;
51
52 import javax.tools.JavaFileManager;
53 import javax.tools.JavaFileObject;
54 import javax.tools.JavaFileObject.Kind;
55
56 import com.sun.tools.javac.code.Lint;
57 import com.sun.tools.javac.code.Lint.LintCategory;
58 import com.sun.tools.javac.main.JavaCompiler;
59 import com.sun.tools.javac.main.JavaCompiler.CodeReflectionSupport;
60 import com.sun.tools.javac.main.Option;
61 import com.sun.tools.javac.main.OptionHelper;
62 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
63 import com.sun.tools.javac.resources.CompilerProperties.Errors;
64 import com.sun.tools.javac.resources.CompilerProperties.LintWarnings;
65 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
66 import com.sun.tools.javac.util.Context;
67 import com.sun.tools.javac.util.DefinedBy;
68 import com.sun.tools.javac.util.DefinedBy.Api;
69 import com.sun.tools.javac.util.Log;
70 import com.sun.tools.javac.util.Options;
71
72 /**
73 * Utility methods for building a file manager.
74 * There are no references here to file-system specific objects such as
75 * java.io.File or java.nio.file.Path.
76 */
77 public abstract class BaseFileManager implements JavaFileManager {
78
79 private static final byte[] EMPTY_ARRAY = new byte[0];
80
81 @SuppressWarnings("this-escape")
82 protected BaseFileManager(Charset charset) {
83 this.charset = charset;
84 locations = createLocations();
85 }
86
87 /**
88 * Set the context for JavacPathFileManager.
89 * @param context the context containing items to be associated with the file manager
90 */
91 public void setContext(Context context) {
92 log = Log.instance(context);
93 lint = Lint.instance(context);
94 options = Options.instance(context);
95
96 // Initialize locations
97 locations.update(log, FSInfo.instance(context));
98
99 // Apply options
100 options.whenReady(this::applyOptions);
101 }
102
103 protected void applyOptions(Options options) {
104
105 // Setting this option is an indication that close() should defer actually closing
106 // the file manager until after a specified period of inactivity.
107 // This is to accommodate clients which save references to Symbols created for use
108 // within doclets or annotation processors, and which then attempt to use those
109 // references after the tool exits, having closed any internally managed file manager.
110 // Ideally, such clients should run the tool via the javax.tools API, providing their
111 // own file manager, which can be closed by the client when all use of that file
112 // manager is complete.
113 // If the option has a numeric value, it will be interpreted as the duration,
114 // in seconds, of the period of inactivity to wait for, before the file manager
115 // is actually closed.
116 // See also deferredClose().
117 String s = options.get("fileManager.deferClose");
118 if (s != null) {
119 try {
120 deferredCloseTimeout = (int) (Float.parseFloat(s) * 1000);
121 } catch (NumberFormatException e) {
122 deferredCloseTimeout = 60 * 1000; // default: one minute, in millis
123 }
124 }
125 }
126
127 protected Locations createLocations() {
128 return new Locations();
129 }
130
131 /**
132 * The log to be used for error reporting.
133 */
134 public Log log;
135
136 /**
137 * User provided charset (through javax.tools).
138 */
139 protected Charset charset;
140
141 protected Options options;
142
143 protected Lint lint;
144
145 protected final Locations locations;
146
147 private final HashSet<Path> outputFilesWritten = new HashSet<>();
148
149 /**
150 * A flag for clients to use to indicate that this file manager should
151 * be closed when it is no longer required.
152 */
153 public boolean autoClose;
154
155 /**
156 * Wait for a period of inactivity before calling close().
157 * The length of the period of inactivity is given by {@code deferredCloseTimeout}
158 */
159 protected void deferredClose() {
160 Thread t = new Thread(getClass().getName() + " DeferredClose") {
161 @Override
162 public void run() {
163 try {
164 synchronized (BaseFileManager.this) {
165 long now = System.currentTimeMillis();
166 while (now < lastUsedTime + deferredCloseTimeout) {
167 BaseFileManager.this.wait(lastUsedTime + deferredCloseTimeout - now);
168 now = System.currentTimeMillis();
169 }
170 deferredCloseTimeout = 0;
171 close();
172 }
173 } catch (InterruptedException e) {
174 } catch (IOException e) {
175 }
176 }
177 };
178 t.setDaemon(true);
179 t.start();
180 }
181
182 synchronized void updateLastUsedTime() {
183 if (deferredCloseTimeout > 0) { // avoid updating the time unnecessarily
184 lastUsedTime = System.currentTimeMillis();
185 }
186 }
187
188 private long lastUsedTime = System.currentTimeMillis();
189 protected long deferredCloseTimeout = 0;
190
191 public void clear() {
192 new HashSet<>(options.keySet()).forEach(k -> options.remove(k));
193 }
194
195 protected ClassLoader getClassLoader(URL[] urls) {
196 ClassLoader thisClassLoader = CodeReflectionSupport.CODE_LAYER != null ?
197 CodeReflectionSupport.CODE_LAYER.findLoader("jdk.incubator.code") :
198 getClass().getClassLoader();
199
200 // Allow the following to specify a closeable classloader
201 // other than URLClassLoader.
202
203 // 1: Allow client to specify the class to use via hidden option
204 String classLoaderClass = options.get("procloader");
205 if (classLoaderClass != null) {
206 try {
207 Class<? extends ClassLoader> loader =
208 Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
209 Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
210 Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
211 return constr.newInstance(urls, thisClassLoader);
212 } catch (ReflectiveOperationException t) {
213 // ignore errors loading user-provided class loader, fall through
214 }
215 }
216 return new URLClassLoader(urls, thisClassLoader);
217 }
218
219 public boolean isDefaultBootClassPath() {
220 return locations.isDefaultBootClassPath();
221 }
222
223 public boolean isDefaultSystemModulesPath() {
224 return locations.isDefaultSystemModulesPath();
225 }
226
227 // <editor-fold defaultstate="collapsed" desc="Option handling">
228 @Override @DefinedBy(Api.COMPILER)
229 public boolean handleOption(String current, Iterator<String> remaining) {
230 OptionHelper helper = new GrumpyHelper(log) {
231 @Override
232 public String get(Option option) {
233 return options.get(option);
234 }
235
236 @Override
237 public void put(String name, String value) {
238 options.put(name, value);
239 }
240
241 @Override
242 public void remove(String name) {
243 options.remove(name);
244 }
245
246 @Override
247 public boolean handleFileManagerOption(Option option, String value) {
248 return handleOption(option, value);
249 }
250
251 @Override
252 public void initialize() {
253 options.initialize();
254 }
255 };
256
257 Option o = Option.lookup(current, javacFileManagerOptions);
258 if (o == null) {
259 return false;
260 }
261
262 try {
263 o.handleOption(helper, current, remaining);
264 } catch (Option.InvalidValueException e) {
265 throw new IllegalArgumentException(e.getMessage(), e);
266 }
267
268 return true;
269 }
270 // where
271 protected static final Set<Option> javacFileManagerOptions =
272 Option.getJavacFileManagerOptions();
273
274 @Override @DefinedBy(Api.COMPILER)
275 public int isSupportedOption(String option) {
276 Option o = Option.lookup(option, javacFileManagerOptions);
277 return (o == null) ? -1 : o.hasArg() ? 1 : 0;
278 }
279
280 protected String multiReleaseValue;
281 protected boolean previewMode;
282
283 /**
284 * Common back end for OptionHelper handleFileManagerOption.
285 * @param option the option whose value to be set
286 * @param value the value for the option
287 * @return true if successful, and false otherwise
288 */
289 public boolean handleOption(Option option, String value) {
290 switch (option) {
291 case ENCODING:
292 encodingName = value;
293 return true;
294
295 case MULTIRELEASE:
296 multiReleaseValue = value;
297 locations.setMultiReleaseValue(value);
298 return true;
299
300 case PREVIEWMODE:
301 previewMode = Boolean.parseBoolean(value);
302 locations.setPreviewMode(previewMode);
303 return true;
304
305 default:
306 return locations.handleOption(option, value);
307 }
308 }
309
310 /**
311 * Call handleOption for collection of options and corresponding values.
312 * @param map a collection of options and corresponding values
313 * @return true if all the calls are successful
314 */
315 public boolean handleOptions(Map<Option, String> map) {
316 boolean ok = true;
317 for (Map.Entry<Option, String> e: map.entrySet()) {
318 try {
319 ok = ok & handleOption(e.getKey(), e.getValue());
320 } catch (IllegalArgumentException ex) {
321 log.error(Errors.IllegalArgumentForOption(e.getKey().getPrimaryName(), ex.getMessage()));
322 ok = false;
323 }
324 }
325 return ok;
326 }
327
328 // </editor-fold>
329
330 // <editor-fold defaultstate="collapsed" desc="Encoding">
331 private String encodingName;
332 private String defaultEncodingName;
333 private String getDefaultEncodingName() {
334 if (defaultEncodingName == null) {
335 defaultEncodingName = Charset.defaultCharset().name();
336 }
337 return defaultEncodingName;
338 }
339
340 public String getEncodingName() {
341 return (encodingName != null) ? encodingName : getDefaultEncodingName();
342 }
343
344 public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
345 String encName = getEncodingName();
346 CharsetDecoder decoder;
347 try {
348 decoder = getDecoder(encName, ignoreEncodingErrors);
349 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
350 log.error(Errors.UnsupportedEncoding(encName));
351 return CharBuffer.allocate(1).flip();
352 }
353
354 // slightly overestimate the buffer size to avoid reallocation.
355 float factor =
356 decoder.averageCharsPerByte() * 0.8f +
357 decoder.maxCharsPerByte() * 0.2f;
358 CharBuffer dest = CharBuffer.
359 allocate(10 + (int)(inbuf.remaining()*factor));
360
361 while (true) {
362 CoderResult result = decoder.decode(inbuf, dest, true);
363 dest.flip();
364
365 if (result.isUnderflow()) { // done reading
366 // make sure there is at least one extra character
367 if (dest.limit() == dest.capacity()) {
368 dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
369 dest.flip();
370 }
371 return dest;
372 } else if (result.isOverflow()) { // buffer too small; expand
373 int newCapacity =
374 10 + dest.capacity() +
375 (int)(inbuf.remaining()*decoder.maxCharsPerByte());
376 dest = CharBuffer.allocate(newCapacity).put(dest);
377 } else if (result.isMalformed() || result.isUnmappable()) {
378 // bad character in input
379 StringBuilder unmappable = new StringBuilder();
380 int len = result.length();
381
382 for (int i = 0; i < len; i++) {
383 unmappable.append(String.format("%02X", inbuf.get()));
384 }
385
386 String charsetName = charset == null ? encName : charset.name();
387
388 log.error(dest.limit(),
389 Errors.IllegalCharForEncoding(unmappable.toString(), charsetName));
390
391 // undo the flip() to prepare the output buffer
392 // for more translation
393 dest.position(dest.limit());
394 dest.limit(dest.capacity());
395 dest.put((char)0xfffd); // backward compatible
396 } else {
397 throw new AssertionError(result);
398 }
399 }
400 // unreached
401 }
402
403 public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
404 Charset cs = (this.charset == null)
405 ? Charset.forName(encodingName)
406 : this.charset;
407 CharsetDecoder decoder = cs.newDecoder();
408
409 CodingErrorAction action;
410 if (ignoreEncodingErrors)
411 action = CodingErrorAction.REPLACE;
412 else
413 action = CodingErrorAction.REPORT;
414
415 return decoder
416 .onMalformedInput(action)
417 .onUnmappableCharacter(action);
418 }
419 // </editor-fold>
420
421 // <editor-fold defaultstate="collapsed" desc="ByteBuffers">
422 /**
423 * Make a {@link ByteBuffer} from an input stream.
424 * @param in the stream
425 * @return a byte buffer containing the contents of the stream
426 * @throws IOException if an error occurred while reading the stream
427 */
428 public ByteBuffer makeByteBuffer(InputStream in) throws IOException {
429 byte[] array;
430 synchronized (this) {
431 if ((array = byteArrayCache) != null)
432 byteArrayCache = null;
433 else
434 array = EMPTY_ARRAY;
435 }
436 com.sun.tools.javac.util.ByteBuffer buf = new com.sun.tools.javac.util.ByteBuffer(array);
437 buf.appendStream(in);
438 return buf.asByteBuffer();
439 }
440
441 public void recycleByteBuffer(ByteBuffer buf) {
442 if (buf.hasArray()) {
443 synchronized (this) {
444 byteArrayCache = buf.array();
445 }
446 }
447 }
448
449 private byte[] byteArrayCache;
450 // </editor-fold>
451
452 // <editor-fold defaultstate="collapsed" desc="Content cache">
453 public CharBuffer getCachedContent(JavaFileObject file) {
454 ContentCacheEntry e = contentCache.get(file);
455 if (e == null)
456 return null;
457
458 if (!e.isValid(file)) {
459 contentCache.remove(file);
460 return null;
461 }
462
463 return e.getValue();
464 }
465
466 public void cache(JavaFileObject file, CharBuffer cb) {
467 contentCache.put(file, new ContentCacheEntry(file, cb));
468 }
469
470 public void flushCache(JavaFileObject file) {
471 contentCache.remove(file);
472 }
473
474 public synchronized void resetOutputFilesWritten() {
475 outputFilesWritten.clear();
476 }
477
478 protected final Map<JavaFileObject, ContentCacheEntry> contentCache = new HashMap<>();
479
480 protected static class ContentCacheEntry {
481 final long timestamp;
482 final SoftReference<CharBuffer> ref;
483
484 ContentCacheEntry(JavaFileObject file, CharBuffer cb) {
485 this.timestamp = file.getLastModified();
486 this.ref = new SoftReference<>(cb);
487 }
488
489 boolean isValid(JavaFileObject file) {
490 return timestamp == file.getLastModified();
491 }
492
493 CharBuffer getValue() {
494 return ref.get();
495 }
496 }
497 // </editor-fold>
498
499 public static Kind getKind(Path path) {
500 return getKind(path.getFileName().toString());
501 }
502
503 public static Kind getKind(String name) {
504 if (name.endsWith(Kind.CLASS.extension))
505 return Kind.CLASS;
506 else if (name.endsWith(Kind.SOURCE.extension))
507 return Kind.SOURCE;
508 else if (name.endsWith(Kind.HTML.extension))
509 return Kind.HTML;
510 else
511 return Kind.OTHER;
512 }
513
514 protected static <T> T nullCheck(T o) {
515 return Objects.requireNonNull(o);
516 }
517
518 protected static <T> Collection<T> nullCheck(Collection<T> it) {
519 for (T t : it)
520 Objects.requireNonNull(t);
521 return it;
522 }
523
524 // Output File Clash Detection
525
526 /** Record the fact that we have started writing to an output file.
527 */
528 // Note: individual files can be accessed concurrently, so we synchronize here
529 synchronized void newOutputToPath(Path path) throws IOException {
530
531 // Is output file clash detection enabled?
532 if (!lint.isEnabled(LintCategory.OUTPUT_FILE_CLASH))
533 return;
534
535 // Get the "canonical" version of the file's path; we are assuming
536 // here that two clashing files will resolve to the same real path.
537 Path realPath;
538 try {
539 realPath = path.toRealPath();
540 } catch (NoSuchFileException e) {
541 return; // should never happen except on broken filesystems
542 }
543
544 // Check whether we've already opened this file for output
545 if (!outputFilesWritten.add(realPath))
546 log.warning(LintWarnings.OutputFileClash(path));
547 }
548 }