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