< prev index next >

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

Print this page




   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.Closeable;


  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 import java.nio.file.Paths;

  30 import java.util.Collections;
  31 import java.util.List;
  32 import java.util.Objects;
  33 import java.util.concurrent.Executor;
  34 import java.util.concurrent.atomic.AtomicLong;
  35 import java.util.function.Function;
  36 import java.util.regex.Matcher;
  37 import java.util.regex.Pattern;

  38 import java.util.stream.Stream;
  39 
  40 /**
  41  * Handler for a path, responsible for processing classes in the path.
  42  */
  43 public class PathHandler implements Closeable {
  44     public static abstract class PathEntry implements Closeable {
  45         private final ClassLoader loader = new PathEntryClassLoader(this::findByteCode);
  46 
  47         /**
  48          * returns bytecode for the class
  49          * @param name binary name of the class
  50          * @return bytecode of the class or null if handler does not have any
  51          * code for this name
  52          */
  53         protected abstract byte[] findByteCode(String name);
  54 
  55         protected final Path root;
  56 
  57         /**


 104 
 105     private static final AtomicLong CLASS_COUNT = new AtomicLong(0L);
 106     private static volatile boolean CLASSES_LIMIT_REACHED = false;
 107     private static final Pattern JAR_IN_DIR_PATTERN
 108             = Pattern.compile("^(.*[/\\\\])?\\*$");
 109 
 110     /**
 111      * Factory method. Constructs list of handlers for {@code path}.
 112      *
 113      * @param path     the path to process
 114      * @throws NullPointerException if {@code path} or {@code executor} is
 115      *                              {@code null}
 116      */
 117     public static List<PathHandler> create(String path) {
 118         Objects.requireNonNull(path);
 119         Matcher matcher = JAR_IN_DIR_PATTERN.matcher(path);
 120         if (matcher.matches()) {
 121             path = matcher.group(1);
 122             path = path.isEmpty() ? "." : path;
 123             return ClassPathJarInDirEntry.create(Paths.get(path));









 124         } else {
 125             path = path.isEmpty() ? "." : path;
 126             Path p = Paths.get(path);
 127             PathEntry entry;
 128             if (isJarFile(p)) {
 129                 entry = new ClassPathJarEntry(p);
 130             } else if (isListFile(p)) {
 131                 entry = new ClassesListInFile(p);
 132             } else if (isJimageFile(p)) {
 133                 entry = new ClassPathJimageEntry(p);
 134             } else {
 135                 entry = new ClassPathDirEntry(p);
 136             }
 137             return Collections.singletonList(new PathHandler(entry));
 138         }
 139     }
 140 
 141     private static boolean isJarFile(Path path) {
 142         if (Files.isRegularFile(path)) {
 143             String name = path.toString();


 163     }
 164 
 165     private final PathEntry entry;
 166     protected PathHandler(PathEntry entry) {
 167         Objects.requireNonNull(entry);
 168         this.entry = entry;
 169     }
 170 
 171 
 172     @Override
 173     public void close() {
 174         entry.close();
 175     }
 176 
 177     /**
 178      * Processes all classes in the specified path.
 179      * @param executor executor used for process task invocation
 180      */
 181     public final void process(Executor executor) {
 182         CompileTheWorld.OUT.println(entry.description());
 183         entry.classes().forEach(s -> processClass(s, executor));


 184     }
 185 
 186     /**
 187      * @return count of all classes in the specified path.
 188      */
 189     public long classCount() {
 190         return entry.classes().count();
 191     }
 192 
 193 
 194     /**
 195      * Processes specified class.
 196      * @param name fully qualified name of class to process
 197      */
 198     protected final void processClass(String name, Executor executor) {
 199         Objects.requireNonNull(name);
 200         if (isFinished()) {
 201             return;
 202         }
 203         long id = CLASS_COUNT.incrementAndGet();
 204         if (id > Utils.COMPILE_THE_WORLD_STOP_AT) {
 205             CLASSES_LIMIT_REACHED = true;
 206             return;
 207         }
 208         if (id >= Utils.COMPILE_THE_WORLD_START_AT) {
 209             Class<?> aClass;
 210             Thread.currentThread().setContextClassLoader(entry.loader());
 211             try {
 212                 CompileTheWorld.OUT.printf("[%d]\t%s%n", id, name);
 213                 aClass = entry.loader().loadClass(name);
 214                 Compiler.compileClass(aClass, id, executor);
 215             } catch (Throwable e) {
 216                 CompileTheWorld.OUT.printf("[%d]\t%s\tWARNING skipped: %s%n",
 217                         id, name, e);
 218                 e.printStackTrace(CompileTheWorld.ERR);
 219             }
 220         }
 221     }
 222 
 223     /**
 224      * @return count of processed classes
 225      */
 226     public static long getProcessedClassCount() {
 227         long id = CLASS_COUNT.get();
 228         if (id < Utils.COMPILE_THE_WORLD_START_AT) {
 229             return 0;
 230         }
 231         if (id > Utils.COMPILE_THE_WORLD_STOP_AT) {
 232             return Utils.COMPILE_THE_WORLD_STOP_AT - Utils.COMPILE_THE_WORLD_START_AT + 1;
 233         }
 234         return id - Utils.COMPILE_THE_WORLD_START_AT + 1;
 235     }
 236 
 237     /**


   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.Closeable;
  27 import java.net.URI;
  28 import java.nio.file.FileSystems;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Arrays;
  33 import java.util.Collections;
  34 import java.util.List;
  35 import java.util.Objects;
  36 import java.util.concurrent.Executor;
  37 import java.util.concurrent.atomic.AtomicLong;
  38 import java.util.function.Function;
  39 import java.util.regex.Matcher;
  40 import java.util.regex.Pattern;
  41 import java.util.stream.Collectors;
  42 import java.util.stream.Stream;
  43 
  44 /**
  45  * Handler for a path, responsible for processing classes in the path.
  46  */
  47 public class PathHandler implements Closeable {
  48     public static abstract class PathEntry implements Closeable {
  49         private final ClassLoader loader = new PathEntryClassLoader(this::findByteCode);
  50 
  51         /**
  52          * returns bytecode for the class
  53          * @param name binary name of the class
  54          * @return bytecode of the class or null if handler does not have any
  55          * code for this name
  56          */
  57         protected abstract byte[] findByteCode(String name);
  58 
  59         protected final Path root;
  60 
  61         /**


 108 
 109     private static final AtomicLong CLASS_COUNT = new AtomicLong(0L);
 110     private static volatile boolean CLASSES_LIMIT_REACHED = false;
 111     private static final Pattern JAR_IN_DIR_PATTERN
 112             = Pattern.compile("^(.*[/\\\\])?\\*$");
 113 
 114     /**
 115      * Factory method. Constructs list of handlers for {@code path}.
 116      *
 117      * @param path     the path to process
 118      * @throws NullPointerException if {@code path} or {@code executor} is
 119      *                              {@code null}
 120      */
 121     public static List<PathHandler> create(String path) {
 122         Objects.requireNonNull(path);
 123         Matcher matcher = JAR_IN_DIR_PATTERN.matcher(path);
 124         if (matcher.matches()) {
 125             path = matcher.group(1);
 126             path = path.isEmpty() ? "." : path;
 127             return ClassPathJarInDirEntry.create(Paths.get(path));
 128         } else if (path.startsWith("modules:")) {
 129             Path modules = FileSystems.getFileSystem(URI.create("jrt:/"))
 130                                       .getPath("modules");
 131             return Arrays.stream(path.substring("modules:".length())
 132                                      .split(","))
 133                          .map(modules::resolve)
 134                          .map(ClassPathDirEntry::new)
 135                          .map(PathHandler::new)
 136                          .collect(Collectors.toList());
 137         } else {
 138             path = path.isEmpty() ? "." : path;
 139             Path p = Paths.get(path);
 140             PathEntry entry;
 141             if (isJarFile(p)) {
 142                 entry = new ClassPathJarEntry(p);
 143             } else if (isListFile(p)) {
 144                 entry = new ClassesListInFile(p);
 145             } else if (isJimageFile(p)) {
 146                 entry = new ClassPathJimageEntry(p);
 147             } else {
 148                 entry = new ClassPathDirEntry(p);
 149             }
 150             return Collections.singletonList(new PathHandler(entry));
 151         }
 152     }
 153 
 154     private static boolean isJarFile(Path path) {
 155         if (Files.isRegularFile(path)) {
 156             String name = path.toString();


 176     }
 177 
 178     private final PathEntry entry;
 179     protected PathHandler(PathEntry entry) {
 180         Objects.requireNonNull(entry);
 181         this.entry = entry;
 182     }
 183 
 184 
 185     @Override
 186     public void close() {
 187         entry.close();
 188     }
 189 
 190     /**
 191      * Processes all classes in the specified path.
 192      * @param executor executor used for process task invocation
 193      */
 194     public final void process(Executor executor) {
 195         CompileTheWorld.OUT.println(entry.description());
 196         entry.classes()
 197              .distinct()
 198              .forEach(s -> processClass(s, executor));
 199     }
 200 
 201     /**
 202      * @return count of all classes in the specified path.
 203      */
 204     public long classCount() {
 205         return entry.classes().count();
 206     }
 207 
 208 
 209     /**
 210      * Processes specified class.
 211      * @param name fully qualified name of class to process
 212      */
 213     protected final void processClass(String name, Executor executor) {
 214         Objects.requireNonNull(name);
 215         if (isFinished()) {
 216             return;
 217         }
 218         long id = CLASS_COUNT.incrementAndGet();
 219         if (id > Utils.COMPILE_THE_WORLD_STOP_AT) {
 220             CLASSES_LIMIT_REACHED = true;
 221             return;
 222         }
 223         if (id >= Utils.COMPILE_THE_WORLD_START_AT) {
 224             Class<?> aClass;
 225             Thread.currentThread().setContextClassLoader(entry.loader());
 226             try {
 227                 CompileTheWorld.OUT.println(String.format("[%d]\t%s", id, name));
 228                 aClass = entry.loader().loadClass(name);
 229                 Compiler.compileClass(aClass, id, executor);
 230             } catch (Throwable e) {
 231                 CompileTheWorld.OUT.println(String.format("[%d]\t%s\tWARNING skipped: %s",
 232                         id, name, e));
 233                 e.printStackTrace(CompileTheWorld.ERR);
 234             }
 235         }
 236     }
 237 
 238     /**
 239      * @return count of processed classes
 240      */
 241     public static long getProcessedClassCount() {
 242         long id = CLASS_COUNT.get();
 243         if (id < Utils.COMPILE_THE_WORLD_START_AT) {
 244             return 0;
 245         }
 246         if (id > Utils.COMPILE_THE_WORLD_STOP_AT) {
 247             return Utils.COMPILE_THE_WORLD_STOP_AT - Utils.COMPILE_THE_WORLD_START_AT + 1;
 248         }
 249         return id - Utils.COMPILE_THE_WORLD_START_AT + 1;
 250     }
 251 
 252     /**
< prev index next >