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