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.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                 if ("true".equals(value)) {
302                     previewMode = true;
303                 } else if ("false".equals(value)) {
304                     previewMode = false;
305                 } else {
306                     throw new IllegalArgumentException("Must be \"true\" or \"false\", got: " + value);
307                 }
308                 locations.setPreviewMode(previewMode);
309                 return true;
310 
311             default:
312                 return locations.handleOption(option, value);
313         }
314     }
315 
316     /**
317      * Call handleOption for collection of options and corresponding values.
318      * @param map a collection of options and corresponding values
319      * @return true if all the calls are successful
320      */
321     public boolean handleOptions(Map<Option, String> map) {
322         boolean ok = true;
323         for (Map.Entry<Option, String> e: map.entrySet()) {
324             try {
325                 ok = ok & handleOption(e.getKey(), e.getValue());
326             } catch (IllegalArgumentException ex) {
327                 log.error(Errors.IllegalArgumentForOption(e.getKey().getPrimaryName(), ex.getMessage()));
328                 ok = false;
329             }
330         }
331         return ok;
332     }
333 
334     // </editor-fold>
335 
336     // <editor-fold defaultstate="collapsed" desc="Encoding">
337     private String encodingName;
338     private String defaultEncodingName;
339     private String getDefaultEncodingName() {
340         if (defaultEncodingName == null) {
341             defaultEncodingName = Charset.defaultCharset().name();
342         }
343         return defaultEncodingName;
344     }
345 
346     public String getEncodingName() {
347         return (encodingName != null) ? encodingName : getDefaultEncodingName();
348     }
349 
350     public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
351         String encName = getEncodingName();
352         CharsetDecoder decoder;
353         try {
354             decoder = getDecoder(encName, ignoreEncodingErrors);
355         } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
356             log.error(Errors.UnsupportedEncoding(encName));
357             return CharBuffer.allocate(1).flip();
358         }
359 
360         // slightly overestimate the buffer size to avoid reallocation.
361         float factor =
362             decoder.averageCharsPerByte() * 0.8f +
363             decoder.maxCharsPerByte() * 0.2f;
364         CharBuffer dest = CharBuffer.
365             allocate(10 + (int)(inbuf.remaining()*factor));
366 
367         while (true) {
368             CoderResult result = decoder.decode(inbuf, dest, true);
369             dest.flip();
370 
371             if (result.isUnderflow()) { // done reading
372                 // make sure there is at least one extra character
373                 if (dest.limit() == dest.capacity()) {
374                     dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
375                     dest.flip();
376                 }
377                 return dest;
378             } else if (result.isOverflow()) { // buffer too small; expand
379                 int newCapacity =
380                     10 + dest.capacity() +
381                     (int)(inbuf.remaining()*decoder.maxCharsPerByte());
382                 dest = CharBuffer.allocate(newCapacity).put(dest);
383             } else if (result.isMalformed() || result.isUnmappable()) {
384                 // bad character in input
385                 StringBuilder unmappable = new StringBuilder();
386                 int len = result.length();
387 
388                 for (int i = 0; i < len; i++) {
389                     unmappable.append(String.format("%02X", inbuf.get()));
390                 }
391 
392                 String charsetName = charset == null ? encName : charset.name();
393 
394                 log.error(dest.limit(),
395                           Errors.IllegalCharForEncoding(unmappable.toString(), charsetName));
396 
397                 // undo the flip() to prepare the output buffer
398                 // for more translation
399                 dest.position(dest.limit());
400                 dest.limit(dest.capacity());
401                 dest.put((char)0xfffd); // backward compatible
402             } else {
403                 throw new AssertionError(result);
404             }
405         }
406         // unreached
407     }
408 
409     public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
410         Charset cs = (this.charset == null)
411             ? Charset.forName(encodingName)
412             : this.charset;
413         CharsetDecoder decoder = cs.newDecoder();
414 
415         CodingErrorAction action;
416         if (ignoreEncodingErrors)
417             action = CodingErrorAction.REPLACE;
418         else
419             action = CodingErrorAction.REPORT;
420 
421         return decoder
422             .onMalformedInput(action)
423             .onUnmappableCharacter(action);
424     }
425     // </editor-fold>
426 
427     // <editor-fold defaultstate="collapsed" desc="ByteBuffers">
428     /**
429      * Make a {@link ByteBuffer} from an input stream.
430      * @param in the stream
431      * @return a byte buffer containing the contents of the stream
432      * @throws IOException if an error occurred while reading the stream
433      */
434     public ByteBuffer makeByteBuffer(InputStream in) throws IOException {
435         byte[] array;
436         synchronized (this) {
437             if ((array = byteArrayCache) != null)
438                 byteArrayCache = null;
439             else
440                 array = EMPTY_ARRAY;
441         }
442         com.sun.tools.javac.util.ByteBuffer buf = new com.sun.tools.javac.util.ByteBuffer(array);
443         buf.appendStream(in);
444         return buf.asByteBuffer();
445     }
446 
447     public void recycleByteBuffer(ByteBuffer buf) {
448         if (buf.hasArray()) {
449             synchronized (this) {
450                 byteArrayCache = buf.array();
451             }
452         }
453     }
454 
455     private byte[] byteArrayCache;
456     // </editor-fold>
457 
458     // <editor-fold defaultstate="collapsed" desc="Content cache">
459     public CharBuffer getCachedContent(JavaFileObject file) {
460         ContentCacheEntry e = contentCache.get(file);
461         if (e == null)
462             return null;
463 
464         if (!e.isValid(file)) {
465             contentCache.remove(file);
466             return null;
467         }
468 
469         return e.getValue();
470     }
471 
472     public void cache(JavaFileObject file, CharBuffer cb) {
473         contentCache.put(file, new ContentCacheEntry(file, cb));
474     }
475 
476     public void flushCache(JavaFileObject file) {
477         contentCache.remove(file);
478     }
479 
480     public synchronized void resetOutputFilesWritten() {
481         outputFilesWritten.clear();
482     }
483 
484     protected final Map<JavaFileObject, ContentCacheEntry> contentCache = new HashMap<>();
485 
486     protected static class ContentCacheEntry {
487         final long timestamp;
488         final SoftReference<CharBuffer> ref;
489 
490         ContentCacheEntry(JavaFileObject file, CharBuffer cb) {
491             this.timestamp = file.getLastModified();
492             this.ref = new SoftReference<>(cb);
493         }
494 
495         boolean isValid(JavaFileObject file) {
496             return timestamp == file.getLastModified();
497         }
498 
499         CharBuffer getValue() {
500             return ref.get();
501         }
502     }
503     // </editor-fold>
504 
505     public static Kind getKind(Path path) {
506         return getKind(path.getFileName().toString());
507     }
508 
509     public static Kind getKind(String name) {
510         if (name.endsWith(Kind.CLASS.extension))
511             return Kind.CLASS;
512         else if (name.endsWith(Kind.SOURCE.extension))
513             return Kind.SOURCE;
514         else if (name.endsWith(Kind.HTML.extension))
515             return Kind.HTML;
516         else
517             return Kind.OTHER;
518     }
519 
520     protected static <T> T nullCheck(T o) {
521         return Objects.requireNonNull(o);
522     }
523 
524     protected static <T> Collection<T> nullCheck(Collection<T> it) {
525         for (T t : it)
526             Objects.requireNonNull(t);
527         return it;
528     }
529 
530 // Output File Clash Detection
531 
532     /** Record the fact that we have started writing to an output file.
533      */
534     // Note: individual files can be accessed concurrently, so we synchronize here
535     synchronized void newOutputToPath(Path path) throws IOException {
536 
537         // Is output file clash detection enabled?
538         if (!lint.isEnabled(LintCategory.OUTPUT_FILE_CLASH))
539             return;
540 
541         // Get the "canonical" version of the file's path; we are assuming
542         // here that two clashing files will resolve to the same real path.
543         Path realPath;
544         try {
545             realPath = path.toRealPath();
546         } catch (NoSuchFileException e) {
547             return;         // should never happen except on broken filesystems
548         }
549 
550         // Check whether we've already opened this file for output
551         if (!outputFilesWritten.add(realPath))
552             log.warning(LintWarnings.OutputFileClash(path));
553     }
554 }