1 /*
   2  * Copyright (c) 2013, 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 package sun.hotspot.tools.ctw;
  25 
  26 
  27 import java.io.IOException;
  28 import java.nio.file.FileVisitOption;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.util.stream.Stream;
  32 
  33 /**
  34  * Handler for dirs containing classes to compile.
  35  */
  36 public class ClassPathDirEntry extends PathHandler.PathEntry {
  37     private final int rootLength;
  38 
  39     public ClassPathDirEntry(Path root) {
  40         super(root);
  41         if (!Files.exists(root)) {
  42             throw new Error(root + " dir does not exist");
  43         }
  44         rootLength = root.toString()
  45                          .length();
  46     }
  47 
  48     @Override
  49     protected Stream<String> classes() {
  50         try {
  51             return Files.walk(root, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
  52                         .filter(p -> Utils.isClassFile(p.toString()))
  53                         .map(this::pathToClassName);
  54         } catch (IOException e) {
  55             throw new Error("can not traverse " + root + " : " + e.getMessage(), e);
  56         }
  57     }
  58 
  59     @Override
  60     protected String description() {
  61         return "# dir: " + root;
  62     }
  63 
  64     @Override
  65     protected byte[] findByteCode(String classname) {
  66         Path path = root;
  67         for (String c : Utils.classNameToFileName(classname).split("/")) {
  68             path = path.resolve(c);
  69         }
  70         if (!path.toFile()
  71                  .exists()) {
  72             return null;
  73         }
  74         try {
  75             return Files.readAllBytes(path);
  76         } catch (IOException e) {
  77             e.printStackTrace(CompileTheWorld.ERR);
  78             return null;
  79         }
  80     }
  81 
  82     private String pathToClassName(Path file) {
  83         String fileString;
  84         if (root == file) {
  85             fileString = file.normalize()
  86                              .toString();
  87         } else {
  88             fileString = file.normalize()
  89                              .toString()
  90                              .substring(rootLength + 1);
  91         }
  92         return Utils.fileNameToClassName(fileString);
  93     }
  94 }
  95