< prev index next >

src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/collect/ClassSearch.java

Print this page


   1 /*
   2  * Copyright (c) 2017, 2018, 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 
  34 public final class ClassSearch {
  35     private final List<SourceProvider> providers = new ArrayList<>();
  36 
  37     public void addProvider(SourceProvider provider) {
  38         providers.add(provider);
  39     }
  40 
  41     public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath) {
  42         return search(search, searchPath, (s, t) -> {
  43             throw new InternalError(s + " : " + t, t);
  44         });
  45     }
  46 
  47     public List<LoadedClass> search(List<SearchFor> search, SearchPath searchPath, BiConsumer<String, Throwable> classLoadingErrorsHandler) {
  48         List<LoadedClass> loaded = new ArrayList<>();
  49 
  50         List<ClassSource> sources = new ArrayList<>();
  51 
  52         for (SearchFor entry : search) {


  89                 source = provider.findSource(searchFor.getName(), searchPath);
  90             } catch (Throwable e) {
  91                 classLoadingErrorsHandler.accept(searchFor.getName(), e);
  92             }
  93             if (source != null) {
  94                 if (found != null) {
  95                     throw new InternalError("Multiple possible sources: " + source + " and: " + found);
  96                 }
  97                 found = source;
  98             }
  99         }
 100 
 101         if (found == null) {
 102             classLoadingErrorsHandler.accept(searchFor.getName(), new InternalError("Failed to find " + searchFor.getType() + " file: " + searchFor.getName()));
 103         }
 104         return found;
 105     }
 106 
 107     public static List<SearchFor> makeList(String type, String argument) {
 108         List<SearchFor> list = new ArrayList<>();
 109         String[] elements = argument.split(":");
 110         for (String element : elements) {
 111             list.add(new SearchFor(element, type));
 112         }
 113         return list;
 114     }
 115 }
   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) {


  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 }
< prev index next >