src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/collect/ClassCollector.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File hotspot Sdiff src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/collect

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

Print this page


   1 /*
   2  * Copyright (c) 2016, 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  */


 189         // Combine class path and file name and see what it is.
 190         Path combinedPath = Paths.get(options.classpath + File.separator + fileName);
 191         if (combinedPath.endsWith(".class")) {
 192             throw new InternalError("unimplemented");
 193         } else if (Files.isDirectory(combinedPath)) {
 194             return scanDirectory(classPath, combinedPath);
 195         } else {
 196             throw new InternalError("Expecting a .class, .jar or directory: " + fileName);
 197         }
 198     }
 199 
 200     private FileSystem makeFileSystem(String fileName) {
 201         try {
 202             return FileSystems.newFileSystem(makeJarFileURI(fileName), new HashMap<>());
 203         } catch (IOException e) {
 204             throw new InternalError(e);
 205         }
 206     }
 207 
 208     private URI makeJarFileURI(String fileName) {


 209         try {
 210             return new URI("jar:file:" + Paths.get(fileName).toAbsolutePath() + "!/");
 211         } catch (URISyntaxException e) {
 212             throw new InternalError(e);
 213         }
 214     }
 215 
 216     private PathMatcher combine(PathMatcher m1, PathMatcher m2) {
 217         return path -> m1.matches(path) && m2.matches(path);
 218     }
 219 
 220     private Set<Class<?>> scanDirectory(Path classPath, Path combinedPath) {
 221         String dir = options.classpath;
 222 
 223         FileSystem fileSystem = FileSystems.getDefault();
 224         PathMatcher matcher = fileSystem.getPathMatcher("glob:" + "*.class");
 225         FileSystemFinder finder = new FileSystemFinder(combinedPath,
 226             combine(matcher, pathname -> entryIsClassFile(pathname.toString())));
 227 
 228         File file = new File(dir);
 229         try (URLClassLoader loader = URLClassLoader.newInstance(buildUrls(file))) {
 230             return loadClassFiles(classPath, finder, loader);


 235 
 236     private Set<Class<?>> loadClassFiles(Path root, FileSystemFinder finder, ClassLoader loader) {
 237         Set<Class<?>> classes = new HashSet<>();
 238         for (Path name : finder.done()) {
 239             // Now relativize to the class path so we get the actual class names.
 240             String entry = root.relativize(name).normalize().toString();
 241             Class<?> c = loadClassFile(loader, entry);
 242             addClass(classes, entry, c);
 243         }
 244         return classes;
 245     }
 246 
 247     private void addClass(Set<Class<?>> classes, String name, Class<?> c) {
 248         if (c != null) {
 249             classes.add(c);
 250             log.printlnVerbose(" loaded " + name);
 251         }
 252     }
 253 
 254     private URL[] buildUrls(String fileName) throws MalformedURLException {
 255         return new URL[]{ new URL("jar:file:" + Paths.get(fileName).toAbsolutePath() + "!/") };


 256     }
 257 
 258     private URL[] buildUrls(File file) throws MalformedURLException {
 259         return new URL[] {file.toURI().toURL() };
 260     }
 261 
 262     private Path getModuleDirectory(String modulepath, String module) {
 263         FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
 264         return fs.getPath(modulepath, module);
 265     }
 266 
 267     /**
 268      * Loads a class with the given file name from the specified {@link URLClassLoader}.
 269      */
 270     private Class<?> loadClassFile(final ClassLoader loader, final String fileName) {
 271         int start = 0;
 272         if (fileName.startsWith("/")) {
 273             start = 1;
 274         }
 275         String className = fileName.substring(start, fileName.length() - ".class".length());
 276         className = className.replace('/', '.');

 277         try {
 278             return loader.loadClass(className);
 279         } catch (Throwable e) {
 280             // If we are running in JCK mode we ignore all exceptions.
 281             if (options.ignoreClassLoadingErrors) {
 282                 log.printError(className + ": " + e);
 283                 return null;
 284             }
 285             throw new InternalError(e);
 286         }
 287     }
 288 
 289     /**
 290      * {@link FileVisitor} implementation to find class files recursively.
 291      */
 292     private static class FileSystemFinder extends SimpleFileVisitor<Path> {
 293         private final ArrayList<Path> fileNames = new ArrayList<>();
 294         private final PathMatcher filter;
 295 
 296         FileSystemFinder(Path combinedPath, PathMatcher filter) {


   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  */


 189         // Combine class path and file name and see what it is.
 190         Path combinedPath = Paths.get(options.classpath + File.separator + fileName);
 191         if (combinedPath.endsWith(".class")) {
 192             throw new InternalError("unimplemented");
 193         } else if (Files.isDirectory(combinedPath)) {
 194             return scanDirectory(classPath, combinedPath);
 195         } else {
 196             throw new InternalError("Expecting a .class, .jar or directory: " + fileName);
 197         }
 198     }
 199 
 200     private FileSystem makeFileSystem(String fileName) {
 201         try {
 202             return FileSystems.newFileSystem(makeJarFileURI(fileName), new HashMap<>());
 203         } catch (IOException e) {
 204             throw new InternalError(e);
 205         }
 206     }
 207 
 208     private URI makeJarFileURI(String fileName) {
 209         String name = Paths.get(fileName).toAbsolutePath().toString();
 210         name = name.replace('\\', '/');
 211         try {
 212             return new URI("jar:file:///" + name + "!/");
 213         } catch (URISyntaxException e) {
 214             throw new InternalError(e);
 215         }
 216     }
 217 
 218     private PathMatcher combine(PathMatcher m1, PathMatcher m2) {
 219         return path -> m1.matches(path) && m2.matches(path);
 220     }
 221 
 222     private Set<Class<?>> scanDirectory(Path classPath, Path combinedPath) {
 223         String dir = options.classpath;
 224 
 225         FileSystem fileSystem = FileSystems.getDefault();
 226         PathMatcher matcher = fileSystem.getPathMatcher("glob:" + "*.class");
 227         FileSystemFinder finder = new FileSystemFinder(combinedPath,
 228             combine(matcher, pathname -> entryIsClassFile(pathname.toString())));
 229 
 230         File file = new File(dir);
 231         try (URLClassLoader loader = URLClassLoader.newInstance(buildUrls(file))) {
 232             return loadClassFiles(classPath, finder, loader);


 237 
 238     private Set<Class<?>> loadClassFiles(Path root, FileSystemFinder finder, ClassLoader loader) {
 239         Set<Class<?>> classes = new HashSet<>();
 240         for (Path name : finder.done()) {
 241             // Now relativize to the class path so we get the actual class names.
 242             String entry = root.relativize(name).normalize().toString();
 243             Class<?> c = loadClassFile(loader, entry);
 244             addClass(classes, entry, c);
 245         }
 246         return classes;
 247     }
 248 
 249     private void addClass(Set<Class<?>> classes, String name, Class<?> c) {
 250         if (c != null) {
 251             classes.add(c);
 252             log.printlnVerbose(" loaded " + name);
 253         }
 254     }
 255 
 256     private URL[] buildUrls(String fileName) throws MalformedURLException {
 257         String name = Paths.get(fileName).toAbsolutePath().toString();
 258         name = name.replace('\\', '/');
 259         return new URL[]{ new URL("jar:file:///" + name + "!/") };
 260     }
 261 
 262     private URL[] buildUrls(File file) throws MalformedURLException {
 263         return new URL[] {file.toURI().toURL() };
 264     }
 265 
 266     private Path getModuleDirectory(String modulepath, String module) {
 267         FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
 268         return fs.getPath(modulepath, module);
 269     }
 270 
 271     /**
 272      * Loads a class with the given file name from the specified {@link URLClassLoader}.
 273      */
 274     private Class<?> loadClassFile(final ClassLoader loader, final String fileName) {
 275         int start = 0;
 276         if (fileName.startsWith("/")) {
 277             start = 1;
 278         }
 279         String className = fileName.substring(start, fileName.length() - ".class".length());
 280         className = className.replace('/', '.');
 281         className = className.replace('\\', '.');
 282         try {
 283             return loader.loadClass(className);
 284         } catch (Throwable e) {
 285             // If we are running in JCK mode we ignore all exceptions.
 286             if (options.ignoreClassLoadingErrors) {
 287                 log.printError(className + ": " + e);
 288                 return null;
 289             }
 290             throw new InternalError(e);
 291         }
 292     }
 293 
 294     /**
 295      * {@link FileVisitor} implementation to find class files recursively.
 296      */
 297     private static class FileSystemFinder extends SimpleFileVisitor<Path> {
 298         private final ArrayList<Path> fileNames = new ArrayList<>();
 299         private final PathMatcher filter;
 300 
 301         FileSystemFinder(Path combinedPath, PathMatcher filter) {


src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/collect/ClassCollector.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File