< prev index next >

test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/ClassPathDirEntry.java

Print this page
rev 47464 : 8186618: [TESTBUG] Test applications/ctw/Modules.java doesn't have timeout and hang on windows
Reviewed-by: duke


   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     }


  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         }


   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 import java.io.IOException;
  27 import java.nio.file.FileVisitOption;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.util.stream.Stream;
  31 
  32 /**
  33  * Handler for dirs containing classes to compile.
  34  */
  35 public class ClassPathDirEntry extends PathHandler.PathEntry {
  36     private final int rootLength;
  37 
  38     public ClassPathDirEntry(Path root) {
  39         super(root);
  40         if (!Files.exists(root)) {
  41             throw new Error(root + " dir does not exist");
  42         }
  43         rootLength = root.toString()
  44                          .length();
  45     }


  49         try {
  50             return Files.walk(root, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
  51                         .filter(p -> Utils.isClassFile(p.toString()))
  52                         .map(this::pathToClassName);
  53         } catch (IOException e) {
  54             throw new Error("can not traverse " + root + " : " + e.getMessage(), e);
  55         }
  56     }
  57 
  58     @Override
  59     protected String description() {
  60         return "# dir: " + root;
  61     }
  62 
  63     @Override
  64     protected byte[] findByteCode(String classname) {
  65         Path path = root;
  66         for (String c : Utils.classNameToFileName(classname).split("/")) {
  67             path = path.resolve(c);
  68         }
  69         if (!Files.exists(path)) {

  70             return null;
  71         }
  72         try {
  73             return Files.readAllBytes(path);
  74         } catch (IOException e) {
  75             e.printStackTrace(CompileTheWorld.ERR);
  76             return null;
  77         }
  78     }
  79 
  80     private String pathToClassName(Path file) {
  81         String fileString;
  82         if (root == file) {
  83             fileString = file.normalize()
  84                              .toString();
  85         } else {
  86             fileString = file.normalize()
  87                              .toString()
  88                              .substring(rootLength + 1);
  89         }
< prev index next >