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