langtools/src/share/classes/com/sun/tools/javac/util/BaseFileManager.java
author briangoetz
Wed, 18 Dec 2013 10:29:25 -0500
changeset 22159 682da512ec17
parent 16796 53060909017b
child 22163 3651128c74eb
permissions -rw-r--r--
8030253: Update langtools to use strings-in-switch 8030262: Update langtools to use foreach loops 8030245: Update langtools to use try-with-resources and multi-catch Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
     1
/*
16796
53060909017b 7059170: Assume availablility of URLClassLoader.close
darcy
parents: 14801
diff changeset
     2
 * Copyright (c) 2009, 2013, 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
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.util;
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.Closeable;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    30
import java.io.IOException;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    31
import java.io.InputStream;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    32
import java.io.OutputStreamWriter;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    33
import java.lang.ref.SoftReference;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    34
import java.lang.reflect.Constructor;
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;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    45
import java.util.Collection;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    46
import java.util.HashMap;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    47
import java.util.Iterator;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    48
import java.util.Map;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    49
import java.util.Set;
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    50
import javax.tools.JavaFileObject;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    51
import javax.tools.JavaFileObject.Kind;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    52
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    53
import com.sun.tools.javac.code.Lint;
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    54
import com.sun.tools.javac.code.Source;
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    55
import com.sun.tools.javac.file.FSInfo;
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    56
import com.sun.tools.javac.file.Locations;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    57
import com.sun.tools.javac.main.Option;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    58
import com.sun.tools.javac.main.OptionHelper;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
    59
import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    60
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    61
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    62
/**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    63
 * Utility methods for building a filemanager.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    64
 * There are no references here to file-system specific objects such as
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    65
 * java.io.File or java.nio.file.Path.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    66
 */
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 5520
diff changeset
    67
public abstract class BaseFileManager {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    68
    protected BaseFileManager(Charset charset) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    69
        this.charset = charset;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    70
        byteBufferCache = new ByteBufferCache();
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    71
        locations = createLocations();
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
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    74
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    75
     * Set the context for JavacPathFileManager.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    76
     */
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    77
    public void setContext(Context context) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    78
        log = Log.instance(context);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    79
        options = Options.instance(context);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    80
        classLoaderClass = options.get("procloader");
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    81
        locations.update(log, options, Lint.instance(context), FSInfo.instance(context));
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    82
    }
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
    83
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    84
    protected Locations createLocations() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    85
        return new Locations();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    86
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    87
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    88
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    89
     * The log to be used for error reporting.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    90
     */
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    91
    public Log log;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    92
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    93
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    94
     * User provided charset (through javax.tools).
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    95
     */
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    96
    protected Charset charset;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    97
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    98
    protected Options options;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
    99
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   100
    protected String classLoaderClass;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   101
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   102
    protected Locations locations;
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 10461
diff changeset
   103
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   104
    protected Source getSource() {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   105
        String sourceName = options.get(Option.SOURCE);
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   106
        Source source = null;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   107
        if (sourceName != null)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   108
            source = Source.lookup(sourceName);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   109
        return (source != null ? source : Source.DEFAULT);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   110
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   111
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   112
    protected ClassLoader getClassLoader(URL[] urls) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   113
        ClassLoader thisClassLoader = getClass().getClassLoader();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   114
16796
53060909017b 7059170: Assume availablility of URLClassLoader.close
darcy
parents: 14801
diff changeset
   115
        // Allow the following to specify a closeable classloader
53060909017b 7059170: Assume availablility of URLClassLoader.close
darcy
parents: 14801
diff changeset
   116
        // other than URLClassLoader.
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   117
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   118
        // 1: Allow client to specify the class to use via hidden option
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   119
        if (classLoaderClass != null) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   120
            try {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   121
                Class<? extends ClassLoader> loader =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   122
                        Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   123
                Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   124
                Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   125
                return constr.newInstance(new Object[] { urls, thisClassLoader });
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   126
            } catch (Throwable t) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   127
                // ignore errors loading user-provided class loader, fall through
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   128
            }
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
        return new URLClassLoader(urls, thisClassLoader);
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
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   133
    // <editor-fold defaultstate="collapsed" desc="Option handling">
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   134
    public boolean handleOption(String current, Iterator<String> remaining) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   135
        OptionHelper helper = new GrumpyHelper(log) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   136
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   137
            public String get(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   138
                return options.get(option.getText());
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   139
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   140
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   141
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   142
            public void put(String name, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   143
                options.put(name, value);
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   144
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   145
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   146
            @Override
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   147
            public void remove(String name) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   148
                options.remove(name);
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   149
            }
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   150
        };
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   151
        for (Option o: javacFileManagerOptions) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   152
            if (o.matches(current))  {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   153
                if (o.hasArg()) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   154
                    if (remaining.hasNext()) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   155
                        if (!o.process(helper, current, remaining.next()))
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   156
                            return true;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   157
                    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   158
                } else {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   159
                    if (!o.process(helper, current))
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   160
                        return true;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   161
                }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   162
                // operand missing, or process returned false
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   163
                throw new IllegalArgumentException(current);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   164
            }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   165
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   166
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   167
        return false;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   168
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   169
    // where
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents: 11314
diff changeset
   170
        private static final Set<Option> javacFileManagerOptions =
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   171
            Option.getJavacFileManagerOptions();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   172
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   173
    public int isSupportedOption(String option) {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   174
        for (Option o : javacFileManagerOptions) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   175
            if (o.matches(option))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   176
                return o.hasArg() ? 1 : 0;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   177
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   178
        return -1;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   179
    }
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 5520
diff changeset
   180
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 5520
diff changeset
   181
    public abstract boolean isDefaultBootClassPath();
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 5520
diff changeset
   182
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   183
    // </editor-fold>
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
    // <editor-fold defaultstate="collapsed" desc="Encoding">
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   186
    private String defaultEncodingName;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   187
    private String getDefaultEncodingName() {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   188
        if (defaultEncodingName == null) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   189
            defaultEncodingName =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   190
                new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
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
        return defaultEncodingName;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   193
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   194
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   195
    public String getEncodingName() {
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11053
diff changeset
   196
        String encName = options.get(Option.ENCODING);
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   197
        if (encName == null)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   198
            return getDefaultEncodingName();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   199
        else
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   200
            return encName;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   201
    }
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
    public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   204
        String encodingName = getEncodingName();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   205
        CharsetDecoder decoder;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   206
        try {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   207
            decoder = getDecoder(encodingName, ignoreEncodingErrors);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   208
        } catch (IllegalCharsetNameException e) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   209
            log.error("unsupported.encoding", encodingName);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   210
            return (CharBuffer)CharBuffer.allocate(1).flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   211
        } catch (UnsupportedCharsetException e) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   212
            log.error("unsupported.encoding", encodingName);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   213
            return (CharBuffer)CharBuffer.allocate(1).flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   214
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   215
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   216
        // slightly overestimate the buffer size to avoid reallocation.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   217
        float factor =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   218
            decoder.averageCharsPerByte() * 0.8f +
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   219
            decoder.maxCharsPerByte() * 0.2f;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   220
        CharBuffer dest = CharBuffer.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   221
            allocate(10 + (int)(inbuf.remaining()*factor));
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   222
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   223
        while (true) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   224
            CoderResult result = decoder.decode(inbuf, dest, true);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   225
            dest.flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   226
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   227
            if (result.isUnderflow()) { // done reading
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   228
                // make sure there is at least one extra character
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   229
                if (dest.limit() == dest.capacity()) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   230
                    dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   231
                    dest.flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   232
                }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   233
                return dest;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   234
            } else if (result.isOverflow()) { // buffer too small; expand
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   235
                int newCapacity =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   236
                    10 + dest.capacity() +
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   237
                    (int)(inbuf.remaining()*decoder.maxCharsPerByte());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   238
                dest = CharBuffer.allocate(newCapacity).put(dest);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   239
            } else if (result.isMalformed() || result.isUnmappable()) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   240
                // bad character in input
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   241
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   242
                // report coding error (warn only pre 1.5)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   243
                if (!getSource().allowEncodingErrors()) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   244
                    log.error(new SimpleDiagnosticPosition(dest.limit()),
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   245
                              "illegal.char.for.encoding",
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   246
                              charset == null ? encodingName : charset.name());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   247
                } else {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   248
                    log.warning(new SimpleDiagnosticPosition(dest.limit()),
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   249
                                "illegal.char.for.encoding",
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   250
                                charset == null ? encodingName : charset.name());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   251
                }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   252
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   253
                // skip past the coding error
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   254
                inbuf.position(inbuf.position() + result.length());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   255
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   256
                // undo the flip() to prepare the output buffer
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   257
                // for more translation
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   258
                dest.position(dest.limit());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   259
                dest.limit(dest.capacity());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   260
                dest.put((char)0xfffd); // backward compatible
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   261
            } else {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   262
                throw new AssertionError(result);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   263
            }
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
        // unreached
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   266
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   267
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   268
    public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   269
        Charset cs = (this.charset == null)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   270
            ? Charset.forName(encodingName)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   271
            : this.charset;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   272
        CharsetDecoder decoder = cs.newDecoder();
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
        CodingErrorAction action;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   275
        if (ignoreEncodingErrors)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   276
            action = CodingErrorAction.REPLACE;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   277
        else
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   278
            action = CodingErrorAction.REPORT;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   279
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   280
        return decoder
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   281
            .onMalformedInput(action)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   282
            .onUnmappableCharacter(action);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   283
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   284
    // </editor-fold>
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
    // <editor-fold defaultstate="collapsed" desc="ByteBuffers">
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   287
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   288
     * Make a byte buffer from an input stream.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   289
     */
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   290
    public ByteBuffer makeByteBuffer(InputStream in)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   291
        throws IOException {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   292
        int limit = in.available();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   293
        if (limit < 1024) limit = 1024;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   294
        ByteBuffer result = byteBufferCache.get(limit);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   295
        int position = 0;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   296
        while (in.available() != 0) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   297
            if (position >= limit)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   298
                // expand buffer
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   299
                result = ByteBuffer.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   300
                    allocate(limit <<= 1).
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   301
                    put((ByteBuffer)result.flip());
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   302
            int count = in.read(result.array(),
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   303
                position,
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   304
                limit - position);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   305
            if (count < 0) break;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   306
            result.position(position += count);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   307
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   308
        return (ByteBuffer)result.flip();
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   309
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   310
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   311
    public void recycleByteBuffer(ByteBuffer bb) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   312
        byteBufferCache.put(bb);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   313
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   314
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   315
    /**
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   316
     * A single-element cache of direct byte buffers.
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   317
     */
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   318
    private static class ByteBufferCache {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   319
        private ByteBuffer cached;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   320
        ByteBuffer get(int capacity) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   321
            if (capacity < 20480) capacity = 20480;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   322
            ByteBuffer result =
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   323
                (cached != null && cached.capacity() >= capacity)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   324
                ? (ByteBuffer)cached.clear()
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   325
                : ByteBuffer.allocate(capacity + capacity>>1);
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   326
            cached = null;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   327
            return result;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   328
        }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   329
        void put(ByteBuffer x) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   330
            cached = x;
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
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   333
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   334
    private final ByteBufferCache byteBufferCache;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   335
    // </editor-fold>
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   336
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   337
    // <editor-fold defaultstate="collapsed" desc="Content cache">
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   338
    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
   339
        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
   340
        if (e == null)
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   341
            return null;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   342
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   343
        if (!e.isValid(file)) {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   344
            contentCache.remove(file);
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   345
            return null;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   346
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   347
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   348
        return e.getValue();
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   349
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   350
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   351
    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
   352
        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
   353
    }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   354
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   355
    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
   356
        contentCache.remove(file);
4548
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
10461
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   359
    protected final Map<JavaFileObject, ContentCacheEntry> contentCache
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   360
            = new HashMap<JavaFileObject, ContentCacheEntry>();
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   361
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   362
    protected static class ContentCacheEntry {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   363
        final long timestamp;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   364
        final SoftReference<CharBuffer> ref;
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   365
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   366
        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
   367
            this.timestamp = file.getLastModified();
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   368
            this.ref = new SoftReference<CharBuffer>(cb);
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   369
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   370
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   371
        boolean isValid(JavaFileObject file) {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   372
            return timestamp == file.getLastModified();
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   373
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   374
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   375
        CharBuffer getValue() {
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   376
            return ref.get();
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   377
        }
998e642f9811 7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents: 7681
diff changeset
   378
    }
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   379
    // </editor-fold>
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   380
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   381
    public static Kind getKind(String name) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   382
        if (name.endsWith(Kind.CLASS.extension))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   383
            return Kind.CLASS;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   384
        else if (name.endsWith(Kind.SOURCE.extension))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   385
            return Kind.SOURCE;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   386
        else if (name.endsWith(Kind.HTML.extension))
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   387
            return Kind.HTML;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   388
        else
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   389
            return Kind.OTHER;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   390
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   391
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   392
    protected static <T> T nullCheck(T o) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   393
        o.getClass(); // null check
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   394
        return o;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   395
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   396
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   397
    protected static <T> Collection<T> nullCheck(Collection<T> it) {
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   398
        for (T t : it)
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   399
            t.getClass(); // null check
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   400
        return it;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   401
    }
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents:
diff changeset
   402
}