hotspot/src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/collect/ClassSearch.java
author rbackman
Thu, 05 Jan 2017 08:37:10 +0100
changeset 43464 f38fde4a6b52
child 43974 309b569feebb
permissions -rw-r--r--
8169588: [AOT] jaotc --classpath option is confusing Reviewed-by: kvn, dlong
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43464
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     1
/*
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     4
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     8
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    13
 * accompanied this code).
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    14
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    18
 *
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    21
 * questions.
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    22
 */
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    23
package jdk.tools.jaotc.collect;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    24
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    25
import jdk.tools.jaotc.LoadedClass;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    26
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    27
import java.util.ArrayList;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    28
import java.util.List;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    29
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    30
public class ClassSearch {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    31
    private List<SourceProvider> providers = new ArrayList<>();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    32
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    33
    public void addProvider(SourceProvider provider) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    34
        providers.add(provider);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    35
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    36
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    37
    public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    38
        List<LoadedClass> loaded = new ArrayList<>();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    39
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    40
        List<ClassSource> sources = new ArrayList<>();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    41
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    42
        for (SearchFor entry : search) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    43
            sources.add(findSource(entry, searchPath));
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    44
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    45
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    46
        for (ClassSource source : sources) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    47
            source.eachClass((name, loader) -> loaded.add(loadClass(name, loader)));
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    48
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    49
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    50
        return loaded;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    51
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    52
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    53
    private LoadedClass loadClass(String name, ClassLoader loader) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    54
        try {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    55
            Class<?> clzz = loader.loadClass(name);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    56
            return new LoadedClass(name, clzz);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    57
        } catch (ClassNotFoundException e) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    58
            throw new InternalError("Failed to load with: " + loader, e);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    59
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    60
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    61
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    62
    private ClassSource findSource(SearchFor searchFor, SearchPath searchPath) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    63
        ClassSource found = null;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    64
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    65
        for (SourceProvider provider : providers) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    66
            if (!searchFor.isUnknown() && !provider.supports(searchFor.getType())) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    67
                continue;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    68
            }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    69
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    70
            ClassSource source = provider.findSource(searchFor.getName(), searchPath);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    71
            if (source != null) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    72
                if (found != null) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    73
                    throw new InternalError("Multiple possible sources: " + source + " and: " + found);
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    74
                }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    75
                found = source;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    76
            }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    77
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    78
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    79
        if (found == null) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    80
            throw new InternalError("Failed to find: " + searchFor.toString());
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    81
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    82
        return found;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    83
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    84
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    85
    public static List<SearchFor> makeList(String type, String argument) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    86
        List<SearchFor> list = new ArrayList<>();
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    87
        String[] elements = argument.split(":");
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    88
        for (String element : elements) {
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    89
            list.add(new SearchFor(element, type));
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    90
        }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    91
        return list;
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    92
    }
f38fde4a6b52 8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
diff changeset
    93
}