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