src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/PackagesHelper.java
author ihse
Sat, 03 Mar 2018 08:21:47 +0100
branchihse-warnings-cflags-branch
changeset 56230 489867818774
parent 47855 72b6d674bde2
permissions -rw-r--r--
No longer disable E_OLD_STYLE_FUNC_DEF.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     1
/*
42237
7ac7543bdd5e 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41426
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     4
 *
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    10
 *
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    15
 * accompanied this code).
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    16
 *
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    20
 *
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    23
 * questions.
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    24
 */
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    25
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    27
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
    28
import java.lang.reflect.Modifier;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    29
import java.io.IOException;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    30
import java.util.ArrayList;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    31
import java.util.Collections;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    32
import java.util.LinkedHashMap;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    33
import java.util.List;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    34
import java.util.Map;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    35
import java.util.Set;
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
    36
import jdk.nashorn.internal.runtime.Context;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    37
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    38
/**
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    39
 * Abstract helper class to compute properties of a Java package object. Properties of
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    40
 * package object are (simple) top level class names in that java package and
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    41
 * immediate subpackages of that package.
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    42
 */
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    43
abstract class PackagesHelper {
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
    44
    private final Context context;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    45
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    46
    /**
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    47
     * Construct a new PackagesHelper.
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    48
     *
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
    49
     * @param context the current Nashorn Context
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    50
     */
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
    51
    PackagesHelper(final Context context) throws IOException {
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
    52
        this.context = context;
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    53
    }
41426
aa60c8d89a92 8167018: Nashorn and jjs should support --module-path and --add-modules options
sundar
parents: 38897
diff changeset
    54
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    55
    static PackagesHelper create(final Context context) throws IOException {
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    56
        return isJavacHelperAvailable()? new JavacPackagesHelper(context) : new JrtPackagesHelper(context);
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    57
    }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    58
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    59
    // LRU cache for java package properties lists
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    60
    private final LinkedHashMap<String, List<String>> propsCache =
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
    61
        new LinkedHashMap<String, List<String>>(32, 0.75f, true) {
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    62
            private static final int CACHE_SIZE = 100;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    63
            private static final long serialVersionUID = 1;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    64
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    65
            @Override
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    66
            protected boolean removeEldestEntry(final Map.Entry<String, List<String>> eldest) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    67
                return size() > CACHE_SIZE;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    68
            }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    69
        };
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    70
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    71
    /**
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    72
     * Return the list of properties of the given Java package or package prefix
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    73
     *
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    74
     * @param pkg Java package name or package prefix name
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    75
     * @return the list of properties of the given Java package or package prefix
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    76
     */
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    77
    final List<String> getPackageProperties(final String pkg) {
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    78
        // check the cache first
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    79
        if (propsCache.containsKey(pkg)) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    80
            return propsCache.get(pkg);
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    81
        }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    82
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    83
        try {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    84
            // make sorted list of properties
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    85
            final List<String> props = new ArrayList<>(listPackage(pkg));
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    86
            Collections.sort(props);
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    87
            propsCache.put(pkg, props);
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    88
            return props;
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    89
        } catch (final IOException exp) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    90
            if (Main.DEBUG) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    91
                exp.printStackTrace();
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    92
            }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    93
            return Collections.<String>emptyList();
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    94
        }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    95
    }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
    96
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    97
    /**
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    98
     * Close resources (like file system) used, if any.
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
    99
     */
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   100
    abstract void close() throws IOException;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
   101
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   102
    /**
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   103
     * Return the set of properties of a given package object.
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   104
     *
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   105
     * @param pkg package start string
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   106
     * @return set of properties of the given Java package
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   107
     */
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   108
    abstract Set<String> listPackage(final String pkg) throws IOException;
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
   109
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   110
    final boolean isClassAccessible(final String className) {
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   111
        try {
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   112
            final Class<?> clz = context.findClass(className);
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   113
            return Modifier.isPublic(clz.getModifiers());
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   114
        } catch (final ClassNotFoundException cnfe) {
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   115
        }
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   116
        return false;
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   117
    }
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   118
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   119
    final boolean isPackageAccessible(final String pkgName) {
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   120
        try {
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   121
            Context.checkPackageAccess(pkgName);
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   122
            return true;
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   123
        } catch (final SecurityException se) {
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   124
            return false;
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   125
        }
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34551
diff changeset
   126
    }
47855
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   127
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   128
    private static boolean isJavacHelperAvailable() {
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   129
        try {
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   130
            boolean result = JavacPackagesHelper.isAvailable();
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   131
            if (Main.DEBUG && !result) {
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   132
                System.err.println("javac packages helper is not available");
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   133
            }
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   134
            return result;
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   135
        } catch (final LinkageError err) {
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   136
            if (Main.DEBUG) {
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   137
                System.err.println("javac packages helper is not available");
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   138
                err.printStackTrace();
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   139
            }
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   140
            return false;
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   141
        }
72b6d674bde2 8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents: 47216
diff changeset
   142
    }
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
diff changeset
   143
}