langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/CacheFSInfo.java
author mcimadamore
Fri, 16 Dec 2016 15:27:34 +0000
changeset 42827 36468b5fa7f4
parent 27852 2e6ad0e4fe20
permissions -rw-r--r--
8181370: Convert anonymous inner classes into lambdas/method references Reviewed-by: jjg, rfield, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     1
/*
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     4
 *
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5072b0dd3d52 6743107: clean up use of static caches in file manager
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: 3380
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    10
 *
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    15
 * accompanied this code).
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    16
 *
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3380
diff changeset
    23
 * questions.
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    24
 */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    25
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.file;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    27
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    28
import java.io.IOException;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    29
import java.nio.file.Path;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    30
import java.util.List;
8223
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 5847
diff changeset
    31
import java.util.Map;
638daa596494 6988106: javac report 'java.lang.IllegalMonitorStateException'
jjg
parents: 5847
diff changeset
    32
import java.util.concurrent.ConcurrentHashMap;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    33
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.util.Context;
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 27852
diff changeset
    35
import com.sun.tools.javac.util.Context.Factory;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    36
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    37
/**
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    38
 * Caching implementation of FSInfo.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    39
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    40
 * <p><b>This is NOT part of any supported API.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    41
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    42
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 1208
diff changeset
    43
 * deletion without notice.</b>
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    44
 */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    45
public class CacheFSInfo extends FSInfo {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    46
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    47
    /**
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8422
diff changeset
    48
     * Register a Context.Factory to create a CacheFSInfo.
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    49
     */
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8422
diff changeset
    50
    public static void preRegister(Context context) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 27852
diff changeset
    51
        context.put(FSInfo.class, (Factory<FSInfo>)c -> {
8422
b2ac1acf3937 7018098: CacheFSInfo persists too long
jjg
parents: 8223
diff changeset
    52
                FSInfo instance = new CacheFSInfo();
8614
06e42328ddab 7021650: fix Context issues
jjg
parents: 8422
diff changeset
    53
                c.put(FSInfo.class, instance);
8422
b2ac1acf3937 7018098: CacheFSInfo persists too long
jjg
parents: 8223
diff changeset
    54
                return instance;
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 27852
diff changeset
    55
            });
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    56
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    57
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    58
    public void clearCache() {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    59
        cache.clear();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    60
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    61
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    62
    @Override
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    63
    public Path getCanonicalFile(Path file) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    64
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    65
        return e.canonicalFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    66
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    67
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    68
    @Override
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    69
    public boolean exists(Path file) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    70
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    71
        return e.exists;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    72
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    73
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    74
    @Override
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    75
    public boolean isDirectory(Path file) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    76
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    77
        return e.isDirectory;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    78
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    79
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    80
    @Override
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    81
    public boolean isFile(Path file) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    82
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    83
        return e.isFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    84
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    85
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    86
    @Override
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    87
    public List<Path> getJarClassPath(Path file) throws IOException {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    88
        // don't bother to lock the cache, because it is thread-safe, and
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    89
        // because the worst that can happen would be to create two identical
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    90
        // jar class paths together and have one overwrite the other.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    91
        Entry e = getEntry(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    92
        if (e.jarClassPath == null)
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    93
            e.jarClassPath = super.getJarClassPath(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    94
        return e.jarClassPath;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    95
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    96
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
    97
    private Entry getEntry(Path file) {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    98
        // don't bother to lock the cache, because it is thread-safe, and
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
    99
        // because the worst that can happen would be to create two identical
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   100
        // entries together and have one overwrite the other.
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   101
        Entry e = cache.get(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   102
        if (e == null) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   103
            e = new Entry();
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   104
            e.canonicalFile = super.getCanonicalFile(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   105
            e.exists = super.exists(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   106
            e.isDirectory = super.isDirectory(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   107
            e.isFile = super.isFile(file);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   108
            cache.put(file, e);
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   109
        }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   110
        return e;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   111
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   112
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   113
    // could also be a Map<File,SoftReference<Entry>> ?
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   114
    private final Map<Path,Entry> cache = new ConcurrentHashMap<>();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   115
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   116
    private static class Entry {
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   117
        Path canonicalFile;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   118
        boolean exists;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   119
        boolean isFile;
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   120
        boolean isDirectory;
27852
2e6ad0e4fe20 8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents: 27226
diff changeset
   121
        List<Path> jarClassPath;
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   122
    }
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents:
diff changeset
   123
}