1 /*
   2  * Copyright (c) 2017, 2019, 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  */
  23 
  24 
  25 
  26 package jdk.tools.jaotc.collect;
  27 
  28 import jdk.tools.jaotc.LoadedClass;
  29 
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 import java.util.function.BiConsumer;
  33 import java.io.File;
  34 
  35 public final class ClassSearch {
  36     private final List<SourceProvider> providers = new ArrayList<>();
  37 
  38     public void addProvider(SourceProvider provider) {
  39         providers.add(provider);
  40     }
  41 
  42     public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath) {
  43         return search(search, searchPath, (s, t) -> {
  44             throw new InternalError(s + " : " + t, t);
  45         });
  46     }
  47 
  48     public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath, BiConsumer<String, Throwable> classLoadingErrorsHandler) {
  49         List<LoadedClass> loaded = new ArrayList<>();
  50 
  51         List<ClassSource> sources = new ArrayList<>();
  52 
  53         for (SearchFor entry : search) {
  54             sources.add(findSource(entry, searchPath, classLoadingErrorsHandler));
  55         }
  56 
  57         for (ClassSource source : sources) {
  58             if (source != null) {
  59                 source.eachClass((name, loader) -> {
  60                     LoadedClass x = loadClass(name, loader, classLoadingErrorsHandler);
  61                     if (x != null) {
  62                         loaded.add(x);
  63                     }
  64                 });
  65             }
  66         }
  67 
  68         return loaded;
  69     }
  70 
  71     private static LoadedClass loadClass(String name, ClassLoader loader, BiConsumer<String, Throwable> classLoadingErrorsHandler) {
  72         try {
  73             Class<?> clzz = loader.loadClass(name);
  74             return new LoadedClass(name, clzz);
  75         } catch (Throwable e) {
  76             classLoadingErrorsHandler.accept(name, e);
  77             return null;
  78         }
  79     }
  80 
  81     private ClassSource findSource(SearchFor searchFor, SearchPath searchPath, BiConsumer<String, Throwable> classLoadingErrorsHandler) {
  82         ClassSource found = null;
  83 
  84         for (SourceProvider provider : providers) {
  85             if (!searchFor.isUnknown() && !provider.supports(searchFor.getType())) {
  86                 continue;
  87             }
  88             ClassSource source = null;
  89             try {
  90                 source = provider.findSource(searchFor.getName(), searchPath);
  91             } catch (Throwable e) {
  92                 classLoadingErrorsHandler.accept(searchFor.getName(), e);
  93             }
  94             if (source != null) {
  95                 if (found != null) {
  96                     throw new InternalError("Multiple possible sources: " + source + " and: " + found);
  97                 }
  98                 found = source;
  99             }
 100         }
 101 
 102         if (found == null) {
 103             classLoadingErrorsHandler.accept(searchFor.getName(), new InternalError("Failed to find " + searchFor.getType() + " file: " + searchFor.getName()));
 104         }
 105         return found;
 106     }
 107 
 108     public static List<SearchFor> makeList(String type, String argument) {
 109         List<SearchFor> list = new ArrayList<>();
 110         String[] elements = argument.split(File.pathSeparator);
 111         for (String element : elements) {
 112             list.add(new SearchFor(element, type));
 113         }
 114         return list;
 115     }
 116 }