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