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