langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java
author jlahoda
Mon, 16 May 2016 15:41:57 +0200
changeset 38511 d934635453ad
parent 37394 c36230ee15d9
child 39362 5ac5af2acb57
permissions -rw-r--r--
8078559: Update error message to indicate illegal character when encoding set to ascii Summary: When an input character cannot be decoded, include it in the produced error Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
     1
/*
35807
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
     2
 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
     4
 *
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    10
 *
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    15
 * accompanied this code).
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    16
 *
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
    23
 * questions.
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    24
 */
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    25
29780
8f8e54a1fa20 8076420: Consolidate javac file handling in javac.file package
jjg
parents: 29291
diff changeset
    26
package com.sun.tools.javac.file;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    27
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    29
import java.io.IOException;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    30
import java.io.InputStream;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    31
import java.io.OutputStreamWriter;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    32
import java.lang.ref.SoftReference;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
    34
import java.lang.reflect.Method;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    35
import java.net.URL;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    36
import java.net.URLClassLoader;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    37
import java.nio.ByteBuffer;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    38
import java.nio.CharBuffer;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    39
import java.nio.charset.Charset;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    40
import java.nio.charset.CharsetDecoder;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    41
import java.nio.charset.CoderResult;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    42
import java.nio.charset.CodingErrorAction;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    43
import java.nio.charset.IllegalCharsetNameException;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    44
import java.nio.charset.UnsupportedCharsetException;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27379
diff changeset
    45
import java.nio.file.Path;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
    46
import java.nio.file.Paths;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    47
import java.util.Collection;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    48
import java.util.HashMap;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    49
import java.util.Iterator;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    50
import java.util.Map;
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 27579
diff changeset
    51
import java.util.Objects;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    52
import java.util.Set;
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    53
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    54
import javax.tools.JavaFileManager;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    55
import javax.tools.JavaFileObject;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    56
import javax.tools.JavaFileObject.Kind;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    57
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    58
import com.sun.tools.javac.code.Lint;
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    59
import com.sun.tools.javac.code.Source;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    60
import com.sun.tools.javac.main.Option;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    61
import com.sun.tools.javac.main.OptionHelper;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    62
import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
    63
import com.sun.tools.javac.resources.CompilerProperties.Errors;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
    64
import com.sun.tools.javac.util.Abort;
29780
8f8e54a1fa20 8076420: Consolidate javac file handling in javac.file package
jjg
parents: 29291
diff changeset
    65
import com.sun.tools.javac.util.Context;
8f8e54a1fa20 8076420: Consolidate javac file handling in javac.file package
jjg
parents: 29291
diff changeset
    66
import com.sun.tools.javac.util.DefinedBy;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    67
import com.sun.tools.javac.util.DefinedBy.Api;
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    68
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
29780
8f8e54a1fa20 8076420: Consolidate javac file handling in javac.file package
jjg
parents: 29291
diff changeset
    69
import com.sun.tools.javac.util.Log;
8f8e54a1fa20 8076420: Consolidate javac file handling in javac.file package
jjg
parents: 29291
diff changeset
    70
import com.sun.tools.javac.util.Options;
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    71
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    72
/**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    73
 * Utility methods for building a filemanager.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    74
 * There are no references here to file-system specific objects such as
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    75
 * java.io.File or java.nio.file.Path.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    76
 */
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    77
public abstract class BaseFileManager implements JavaFileManager {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    78
    protected BaseFileManager(Charset charset) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    79
        this.charset = charset;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    80
        byteBufferCache = new ByteBufferCache();
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    81
        locations = createLocations();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    82
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    83
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    84
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    85
     * Set the context for JavacPathFileManager.
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    86
     * @param context the context containing items to be associated with the file manager
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    87
     */
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    88
    public void setContext(Context context) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    89
        log = Log.instance(context);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    90
        options = Options.instance(context);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    91
        classLoaderClass = options.get("procloader");
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
    92
        locations.update(log, Lint.instance(context), FSInfo.instance(context));
35807
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
    93
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
    94
        // Setting this option is an indication that close() should defer actually closing
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
    95
        // the file manager until after a specified period of inactivity.
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
    96
        // This is to accomodate clients which save references to Symbols created for use
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
    97
        // within doclets or annotation processors, and which then attempt to use those
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
    98
        // references after the tool exits, having closed any internally managed file manager.
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
    99
        // Ideally, such clients should run the tool via the javax.tools API, providing their
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   100
        // own file manager, which can be closed by the client when all use of that file
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   101
        // manager is complete.
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   102
        // If the option has a numeric value, it will be interpreted as the duration,
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   103
        // in seconds, of the period of inactivity to wait for, before the file manager
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   104
        // is actually closed.
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   105
        // See also deferredClose().
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   106
        String s = options.get("fileManager.deferClose");
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   107
        if (s != null) {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   108
            try {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   109
                deferredCloseTimeout = (int) (Float.parseFloat(s) * 1000);
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   110
            } catch (NumberFormatException e) {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   111
                deferredCloseTimeout = 60 * 1000;  // default: one minute, in millis
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   112
            }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   113
        }
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
   114
    }
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
   115
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   116
    protected Locations createLocations() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   117
        return new Locations();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   118
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   119
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   120
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   121
     * The log to be used for error reporting.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   122
     */
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   123
    public Log log;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   124
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   125
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   126
     * User provided charset (through javax.tools).
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   127
     */
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   128
    protected Charset charset;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   129
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   130
    protected Options options;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   131
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   132
    protected String classLoaderClass;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   133
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   134
    protected Locations locations;
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
   135
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 27318
diff changeset
   136
    /**
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 27318
diff changeset
   137
     * A flag for clients to use to indicate that this file manager should
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 27318
diff changeset
   138
     * be closed when it is no longer required.
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 27318
diff changeset
   139
     */
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 27318
diff changeset
   140
    public boolean autoClose;
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 27318
diff changeset
   141
35807
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   142
    /**
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   143
     * Wait for a period of inactivity before calling close().
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   144
     * The length of the period of inactivity is given by {@code deferredCloseTimeout}
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   145
     */
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   146
    protected void deferredClose() {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   147
        Thread t = new Thread(getClass().getName() + " DeferredClose") {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   148
            @Override
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   149
            public void run() {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   150
                try {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   151
                    synchronized (BaseFileManager.this) {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   152
                        long now = System.currentTimeMillis();
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   153
                        while (now < lastUsedTime + deferredCloseTimeout) {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   154
                            BaseFileManager.this.wait(lastUsedTime + deferredCloseTimeout - now);
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   155
                            now = System.currentTimeMillis();
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   156
                        }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   157
                        deferredCloseTimeout = 0;
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   158
                        close();
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   159
                    }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   160
                } catch (InterruptedException e) {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   161
                } catch (IOException e) {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   162
                }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   163
            }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   164
        };
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   165
        t.setDaemon(true);
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   166
        t.start();
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   167
    }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   168
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   169
    synchronized void updateLastUsedTime() {
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   170
        if (deferredCloseTimeout > 0) { // avoid updating the time unnecessarily
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   171
            lastUsedTime = System.currentTimeMillis();
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   172
        }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   173
    }
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   174
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   175
    private long lastUsedTime = System.currentTimeMillis();
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   176
    protected long deferredCloseTimeout = 0;
2eb1d877da0f 8147801: java.nio.file.ClosedFileSystemException when using Javadoc API's in JDK9
jjg
parents: 29780
diff changeset
   177
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   178
    protected Source getSource() {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   179
        String sourceName = options.get(Option.SOURCE);
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   180
        Source source = null;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   181
        if (sourceName != null)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   182
            source = Source.lookup(sourceName);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   183
        return (source != null ? source : Source.DEFAULT);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   184
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   185
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   186
    protected ClassLoader getClassLoader(URL[] urls) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   187
        ClassLoader thisClassLoader = getClass().getClassLoader();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   188
16796
53060909017b 7059170: Assume availablility of URLClassLoader.close
darcy
parents: 14801
diff changeset
   189
        // Allow the following to specify a closeable classloader
53060909017b 7059170: Assume availablility of URLClassLoader.close
darcy
parents: 14801
diff changeset
   190
        // other than URLClassLoader.
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   191
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   192
        // 1: Allow client to specify the class to use via hidden option
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   193
        if (classLoaderClass != null) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   194
            try {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   195
                Class<? extends ClassLoader> loader =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   196
                        Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   197
                Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   198
                Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   199
                return ensureReadable(constr.newInstance(urls, thisClassLoader));
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   200
            } catch (ReflectiveOperationException t) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   201
                // ignore errors loading user-provided class loader, fall through
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   202
            }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   203
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   204
        return ensureReadable(new URLClassLoader(urls, thisClassLoader));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   205
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   206
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   207
    /**
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   208
     * Ensures that the unnamed module of the given classloader is readable to this
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   209
     * module.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   210
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   211
    private ClassLoader ensureReadable(ClassLoader targetLoader) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   212
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   213
            Method getModuleMethod = Class.class.getMethod("getModule");
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   214
            Object thisModule = getModuleMethod.invoke(this.getClass());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   215
            Method getUnnamedModuleMethod = ClassLoader.class.getMethod("getUnnamedModule");
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   216
            Object targetModule = getUnnamedModuleMethod.invoke(targetLoader);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   217
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   218
            Class<?> moduleClass = getModuleMethod.getReturnType();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   219
            Method addReadsMethod = moduleClass.getMethod("addReads", moduleClass);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   220
            addReadsMethod.invoke(thisModule, targetModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   221
        } catch (NoSuchMethodException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   222
            // ignore
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   223
        } catch (Exception e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   224
            throw new Abort(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   225
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   226
        return targetLoader;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   227
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   228
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   229
    public boolean isDefaultBootClassPath() {
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   230
        return locations.isDefaultBootClassPath();
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   231
    }
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   232
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   233
    // <editor-fold defaultstate="collapsed" desc="Option handling">
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   234
    @Override @DefinedBy(Api.COMPILER)
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   235
    public boolean handleOption(String current, Iterator<String> remaining) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   236
        OptionHelper helper = new GrumpyHelper(log) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   237
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   238
            public String get(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   239
                return options.get(option.getText());
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   240
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   241
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   242
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   243
            public void put(String name, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   244
                options.put(name, value);
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   245
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   246
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   247
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   248
            public void remove(String name) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   249
                options.remove(name);
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   250
            }
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   251
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   252
            @Override
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   253
            public boolean handleFileManagerOption(Option option, String value) {
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   254
                return handleOption(option, value);
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   255
            }
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   256
        };
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   257
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   258
        for (Option o: javacFileManagerOptions) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   259
            if (o.matches(current))  {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   260
                if (o.hasArg()) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   261
                    if (remaining.hasNext()) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   262
                        if (!o.process(helper, current, remaining.next()))
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   263
                            return true;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   264
                    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   265
                } else {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   266
                    if (!o.process(helper, current))
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   267
                        return true;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   268
                }
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   269
                // operand missing, or process returned true
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   270
                throw new IllegalArgumentException(current);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   271
            }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   272
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   273
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   274
        return false;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   275
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   276
    // where
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 11314
diff changeset
   277
        private static final Set<Option> javacFileManagerOptions =
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   278
            Option.getJavacFileManagerOptions();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   279
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   280
    @Override @DefinedBy(Api.COMPILER)
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   281
    public int isSupportedOption(String option) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   282
        for (Option o : javacFileManagerOptions) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   283
            if (o.matches(option))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   284
                return o.hasArg() ? 1 : 0;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   285
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   286
        return -1;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   287
    }
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 5520
diff changeset
   288
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   289
    protected String multiReleaseValue;
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   290
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   291
    /**
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   292
     * Common back end for OptionHelper handleFileManagerOption.
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   293
     * @param option the option whose value to be set
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   294
     * @param value the value for the option
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   295
     * @return true if successful, and false otherwise
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   296
     */
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   297
    public boolean handleOption(Option option, String value) {
36155
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   298
        switch (option) {
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   299
            case ENCODING:
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   300
                encodingName = value;
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   301
                return true;
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   302
37394
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   303
            case MULTIRELEASE:
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   304
                multiReleaseValue = value;
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   305
                return true;
c36230ee15d9 8149757: Implement Multi-Release JAR aware JavacFileManager for javac
jjg
parents: 36526
diff changeset
   306
36155
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   307
            default:
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   308
                return locations.handleOption(option, value);
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   309
        }
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   310
    }
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   311
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   312
    /**
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   313
     * Call handleOption for collection of options and corresponding values.
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   314
     * @param map a collection of options and corresponding values
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   315
     * @return true if all the calls are successful
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   316
     */
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   317
    public boolean handleOptions(Map<Option, String> map) {
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   318
        boolean ok = true;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   319
        for (Map.Entry<Option, String> e: map.entrySet()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   320
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   321
                ok = ok & handleOption(e.getKey(), e.getValue());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   322
            } catch (IllegalArgumentException ex) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   323
                log.error(Errors.IllegalArgumentForOption(e.getKey().getText(), ex.getMessage()));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   324
                ok = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   325
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36155
diff changeset
   326
        }
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   327
        return ok;
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   328
    }
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 5520
diff changeset
   329
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   330
    // </editor-fold>
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   331
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   332
    // <editor-fold defaultstate="collapsed" desc="Encoding">
36155
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   333
    private String encodingName;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   334
    private String defaultEncodingName;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   335
    private String getDefaultEncodingName() {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   336
        if (defaultEncodingName == null) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   337
            defaultEncodingName =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   338
                new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   339
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   340
        return defaultEncodingName;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   341
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   342
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   343
    public String getEncodingName() {
36155
1555ce1fcb4f 8149772: cleanup handling of -encoding in JavacFileManager
jjg
parents: 35807
diff changeset
   344
        return (encodingName != null) ? encodingName : getDefaultEncodingName();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   345
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   346
27318
4660a5da7d90 8062376: Suppress cast warnings when using NIO buffers
rwarburton
parents: 26266
diff changeset
   347
    @SuppressWarnings("cast")
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   348
    public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   349
        String encodingName = getEncodingName();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   350
        CharsetDecoder decoder;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   351
        try {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   352
            decoder = getDecoder(encodingName, ignoreEncodingErrors);
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   353
        } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   354
            log.error("unsupported.encoding", encodingName);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   355
            return (CharBuffer)CharBuffer.allocate(1).flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   356
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   357
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   358
        // slightly overestimate the buffer size to avoid reallocation.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   359
        float factor =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   360
            decoder.averageCharsPerByte() * 0.8f +
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   361
            decoder.maxCharsPerByte() * 0.2f;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   362
        CharBuffer dest = CharBuffer.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   363
            allocate(10 + (int)(inbuf.remaining()*factor));
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   364
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   365
        while (true) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   366
            CoderResult result = decoder.decode(inbuf, dest, true);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   367
            dest.flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   368
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   369
            if (result.isUnderflow()) { // done reading
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   370
                // make sure there is at least one extra character
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   371
                if (dest.limit() == dest.capacity()) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   372
                    dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   373
                    dest.flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   374
                }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   375
                return dest;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   376
            } else if (result.isOverflow()) { // buffer too small; expand
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   377
                int newCapacity =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   378
                    10 + dest.capacity() +
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   379
                    (int)(inbuf.remaining()*decoder.maxCharsPerByte());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   380
                dest = CharBuffer.allocate(newCapacity).put(dest);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   381
            } else if (result.isMalformed() || result.isUnmappable()) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   382
                // bad character in input
38511
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   383
                StringBuilder unmappable = new StringBuilder();
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   384
                int len = result.length();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   385
38511
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   386
                for (int i = 0; i < len; i++) {
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   387
                    unmappable.append(String.format("%02X", inbuf.get()));
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   388
                }
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   389
38511
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   390
                String charsetName = charset == null ? encodingName : charset.name();
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   391
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   392
                log.error(dest.limit(),
d934635453ad 8078559: Update error message to indicate illegal character when encoding set to ascii
jlahoda
parents: 37394
diff changeset
   393
                          Errors.IllegalCharForEncoding(unmappable.toString(), charsetName));
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   394
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   395
                // undo the flip() to prepare the output buffer
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   396
                // for more translation
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   397
                dest.position(dest.limit());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   398
                dest.limit(dest.capacity());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   399
                dest.put((char)0xfffd); // backward compatible
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   400
            } else {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   401
                throw new AssertionError(result);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   402
            }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   403
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   404
        // unreached
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   405
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   406
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   407
    public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   408
        Charset cs = (this.charset == null)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   409
            ? Charset.forName(encodingName)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   410
            : this.charset;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   411
        CharsetDecoder decoder = cs.newDecoder();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   412
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   413
        CodingErrorAction action;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   414
        if (ignoreEncodingErrors)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   415
            action = CodingErrorAction.REPLACE;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   416
        else
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   417
            action = CodingErrorAction.REPORT;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   418
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   419
        return decoder
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   420
            .onMalformedInput(action)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   421
            .onUnmappableCharacter(action);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   422
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   423
    // </editor-fold>
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   424
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   425
    // <editor-fold defaultstate="collapsed" desc="ByteBuffers">
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   426
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   427
     * Make a byte buffer from an input stream.
24897
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   428
     * @param in the stream
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   429
     * @return a byte buffer containing the contents of the stream
655b72d7b96e 7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents: 22163
diff changeset
   430
     * @throws IOException if an error occurred while reading the stream
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   431
     */
27318
4660a5da7d90 8062376: Suppress cast warnings when using NIO buffers
rwarburton
parents: 26266
diff changeset
   432
    @SuppressWarnings("cast")
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   433
    public ByteBuffer makeByteBuffer(InputStream in)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   434
        throws IOException {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   435
        int limit = in.available();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   436
        if (limit < 1024) limit = 1024;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   437
        ByteBuffer result = byteBufferCache.get(limit);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   438
        int position = 0;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   439
        while (in.available() != 0) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   440
            if (position >= limit)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   441
                // expand buffer
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   442
                result = ByteBuffer.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   443
                    allocate(limit <<= 1).
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   444
                    put((ByteBuffer)result.flip());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   445
            int count = in.read(result.array(),
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   446
                position,
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   447
                limit - position);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   448
            if (count < 0) break;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   449
            result.position(position += count);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   450
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   451
        return (ByteBuffer)result.flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   452
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   453
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   454
    public void recycleByteBuffer(ByteBuffer bb) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   455
        byteBufferCache.put(bb);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   456
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   457
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   458
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   459
     * A single-element cache of direct byte buffers.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   460
     */
27318
4660a5da7d90 8062376: Suppress cast warnings when using NIO buffers
rwarburton
parents: 26266
diff changeset
   461
    @SuppressWarnings("cast")
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   462
    private static class ByteBufferCache {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   463
        private ByteBuffer cached;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   464
        ByteBuffer get(int capacity) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   465
            if (capacity < 20480) capacity = 20480;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   466
            ByteBuffer result =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   467
                (cached != null && cached.capacity() >= capacity)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   468
                ? (ByteBuffer)cached.clear()
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   469
                : ByteBuffer.allocate(capacity + capacity>>1);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   470
            cached = null;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   471
            return result;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   472
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   473
        void put(ByteBuffer x) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   474
            cached = x;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   475
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   476
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   477
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   478
    private final ByteBufferCache byteBufferCache;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   479
    // </editor-fold>
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   480
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   481
    // <editor-fold defaultstate="collapsed" desc="Content cache">
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   482
    public CharBuffer getCachedContent(JavaFileObject file) {
10461
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   483
        ContentCacheEntry e = contentCache.get(file);
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   484
        if (e == null)
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   485
            return null;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   486
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   487
        if (!e.isValid(file)) {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   488
            contentCache.remove(file);
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   489
            return null;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   490
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   491
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   492
        return e.getValue();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   493
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   494
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   495
    public void cache(JavaFileObject file, CharBuffer cb) {
10461
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   496
        contentCache.put(file, new ContentCacheEntry(file, cb));
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   497
    }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   498
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   499
    public void flushCache(JavaFileObject file) {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   500
        contentCache.remove(file);
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   501
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   502
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 16796
diff changeset
   503
    protected final Map<JavaFileObject, ContentCacheEntry> contentCache = new HashMap<>();
10461
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   504
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   505
    protected static class ContentCacheEntry {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   506
        final long timestamp;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   507
        final SoftReference<CharBuffer> ref;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   508
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   509
        ContentCacheEntry(JavaFileObject file, CharBuffer cb) {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   510
            this.timestamp = file.getLastModified();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 16796
diff changeset
   511
            this.ref = new SoftReference<>(cb);
10461
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   512
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   513
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   514
        boolean isValid(JavaFileObject file) {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   515
            return timestamp == file.getLastModified();
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   516
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   517
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   518
        CharBuffer getValue() {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   519
            return ref.get();
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   520
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   521
    }
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   522
    // </editor-fold>
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   523
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27379
diff changeset
   524
    public static Kind getKind(Path path) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27379
diff changeset
   525
        return getKind(path.getFileName().toString());
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27379
diff changeset
   526
    }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27379
diff changeset
   527
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   528
    public static Kind getKind(String name) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   529
        if (name.endsWith(Kind.CLASS.extension))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   530
            return Kind.CLASS;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   531
        else if (name.endsWith(Kind.SOURCE.extension))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   532
            return Kind.SOURCE;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   533
        else if (name.endsWith(Kind.HTML.extension))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   534
            return Kind.HTML;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   535
        else
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   536
            return Kind.OTHER;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   537
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   538
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   539
    protected static <T> T nullCheck(T o) {
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 27579
diff changeset
   540
        return Objects.requireNonNull(o);
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   541
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   542
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   543
    protected static <T> Collection<T> nullCheck(Collection<T> it) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   544
        for (T t : it)
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 27579
diff changeset
   545
            Objects.requireNonNull(t);
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   546
        return it;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   547
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   548
}