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