1 /* 2 * Copyright (c) 2013, 2018, 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 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.stream.Collectors; 27 import java.net.*; 28 import java.io.*; 29 import java.nio.file.FileSystem; 30 import java.nio.file.FileSystems; 31 import java.nio.file.FileSystemNotFoundException; 32 import java.nio.file.ProviderNotFoundException; 33 import java.nio.file.Files; 34 import java.nio.file.Path; 35 import java.util.function.Predicate; 36 37 abstract class Task<T> implements Runnable { 38 private transient boolean working = true; 39 private final List<T> methods; 40 private final Thread thread; 41 42 Task(List<T> methods) { 43 this.methods = methods; 44 this.thread = new Thread(this); 45 this.thread.start(); 46 } 47 48 boolean isAlive() { 49 return this.thread.isAlive(); 50 } 51 52 boolean isWorking() { 53 boolean working = this.working && this.thread.isAlive(); 54 this.working = false; 55 return working; 56 } 57 58 @Override 59 public void run() { 60 long time = -System.currentTimeMillis(); 61 for (T method : this.methods) { 62 this.working = true; 63 try { 64 for (int i = 0; i < 100; i++) { 65 process(method); 66 } 67 } catch (NoSuchMethodException ignore) { 68 } 69 } 70 time += System.currentTimeMillis(); 71 print("thread done in " + time / 1000 + " seconds"); 72 } 73 74 protected abstract void process(T method) throws NoSuchMethodException; 75 76 static synchronized void print(Object message) { 77 System.out.println(message); 78 System.out.flush(); 79 } 80 81 static List<Class<?>> getClasses(int count) throws Exception { 82 List<Class<?>> classes = new ArrayList<>(); 83 FileSystem fs = null; 84 85 try { 86 fs = FileSystems.getFileSystem(URI.create("jrt:/")); 87 } catch (ProviderNotFoundException | FileSystemNotFoundException e) { 88 throw new RuntimeException("FAIL - JRT Filesystem not found"); 89 } 90 91 List<String> fileNames; 92 Path modules = fs.getPath("/modules"); 93 94 Predicate<String> startsWithJavaBase = path -> path.toString().startsWith("java.base/java"); 95 Predicate<String> startsWithJavaDesktop = path -> path.toString().startsWith("java.desktop/java"); 96 Predicate<String> startsWithJavaDataTransfer = path -> path.toString().startsWith("java.datatransfer/java"); 97 Predicate<String> startsWithJavaRMI = path -> path.toString().startsWith("java.rmi/java"); 98 Predicate<String> startsWithJavaSmartCardIO = path -> path.toString().startsWith("java.smartcardio/java"); 99 Predicate<String> startsWithJavaManagement = path -> path.toString().startsWith("java.management/java"); 100 Predicate<String> startsWithJavaXML = path -> path.toString().startsWith("java.xml/java"); 101 Predicate<String> startsWithJavaScripting = path -> path.toString().startsWith("java.scripting/java"); 102 Predicate<String> startsWithJavaNaming = path -> path.toString().startsWith("java.naming/java"); 103 Predicate<String> startsWithJavaSQL = path -> path.toString().startsWith("java.sql/java"); 104 Predicate<String> startsWithJavaCompiler = path -> path.toString().startsWith("java.compiler/java"); 105 Predicate<String> startsWithJavaLogging = path -> path.toString().startsWith("java.logging/java"); 106 Predicate<String> startsWithJavaPrefs = path -> path.toString().startsWith("java.prefs/java"); 107 108 fileNames = Files.walk(modules) 109 .map(Path::toString) 110 .filter(path -> path.toString().contains("java")) 111 .map(s -> s.substring(9)) // remove /modules/ from beginning 112 .filter(startsWithJavaBase 113 .or(startsWithJavaDesktop) 114 .or(startsWithJavaDataTransfer) 115 .or(startsWithJavaRMI) 116 .or(startsWithJavaSmartCardIO) 117 .or(startsWithJavaManagement) 118 .or(startsWithJavaXML) 119 .or(startsWithJavaScripting) 120 .or(startsWithJavaNaming) 121 .or(startsWithJavaSQL) 122 .or(startsWithJavaCompiler) 123 .or(startsWithJavaLogging) 124 .or(startsWithJavaPrefs)) 125 .map(s -> s.replace('/', '.')) 126 .filter(path -> path.toString().endsWith(".class")) 127 .map(s -> s.substring(0, s.length() - 6)) // drop .class 128 .map(s -> s.substring(s.indexOf("."))) 129 .filter(path -> path.toString().contains("java")) 130 .map(s -> s.substring(s.indexOf("java"))) 131 .collect(Collectors.toList()); 132 133 for (String name : fileNames) { 134 classes.add(Class.forName(name)); 135 if (count == classes.size()) { 136 break; 137 } 138 } 139 140 return classes; 141 } 142 }