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  */
  23 
  24 
  25 
  26 package jdk.tools.jaotc.collect;
  27 
  28 import java.nio.file.Path;
  29 import java.util.function.BiConsumer;
  30 
  31 public interface ClassSource {
  32     static boolean pathIsClassFile(Path entry) {
  33         String fileName = entry.getFileName().toString();
  34         return fileName.endsWith(".class") && !fileName.endsWith("module-info.class");
  35     }
  36 
  37     static String stripRoot(Path path) {
  38         if (path.getRoot() != null) {
  39             String root = path.getRoot().toString();
  40             String filename = path.toString().substring(root.length());
  41             String separator = path.getFileSystem().getSeparator();
  42             while (filename.startsWith(separator)) {
  43                 filename = filename.substring(separator.length());
  44             }
  45             return filename;
  46         }
  47 
  48         return path.toString();
  49     }
  50 
  51     static String makeClassName(Path path) {
  52         String fileName = path.toString();
  53 
  54         if (!fileName.endsWith(".class")) {
  55             throw new IllegalArgumentException("File doesn't end with .class: '" + fileName + "'");
  56         }
  57 
  58         fileName = stripRoot(path);
  59 
  60         String className = fileName.substring(0, fileName.length() - ".class".length());
  61         className = className.replace(path.getFileSystem().getSeparator(), ".");
  62         return className;
  63     }
  64 
  65     void eachClass(BiConsumer<String, ClassLoader> consumer);
  66 }