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 import java.util.ArrayList; 25 import java.util.Enumeration; 26 import java.util.List; 27 import java.util.jar.JarEntry; 28 import java.util.jar.JarFile; 29 import java.util.regex.Matcher; 30 import java.util.regex.Pattern; 31 32 abstract class Task<T> implements Runnable { 33 private transient boolean working = true; 34 private final List<T> methods; 35 private final Thread thread; 36 37 Task(List<T> methods) { 38 this.methods = methods; 39 this.thread = new Thread(this); 40 this.thread.start(); 41 } 42 43 boolean isAlive() { 44 return this.thread.isAlive(); 45 } 46 47 boolean isWorking() { 48 boolean working = this.working && this.thread.isAlive(); 49 this.working = false; 50 return working; 57 this.working = true; 58 try { 59 for (int i = 0; i < 100; i++) { 60 process(method); 61 } 62 } catch (NoSuchMethodException ignore) { 63 } 64 } 65 time += System.currentTimeMillis(); 66 print("thread done in " + time / 1000 + " seconds"); 67 } 68 69 protected abstract void process(T method) throws NoSuchMethodException; 70 71 static synchronized void print(Object message) { 72 System.out.println(message); 73 System.out.flush(); 74 } 75 76 static List<Class<?>> getClasses(int count) throws Exception { 77 String resource = ClassLoader.getSystemClassLoader().getResource("java/lang/Object.class").toString(); 78 79 Pattern pattern = Pattern.compile("jar:file:(.*)!.*"); 80 Matcher matcher = pattern.matcher(resource); 81 matcher.matches(); 82 resource = matcher.group(1); 83 84 List<Class<?>> classes = new ArrayList<>(); 85 try (JarFile jarFile = new JarFile(resource)) { 86 Enumeration<JarEntry> entries = jarFile.entries(); 87 while (entries.hasMoreElements()) { 88 String name = entries.nextElement().getName(); 89 if (name.startsWith("java") && name.endsWith(".class")) { 90 classes.add(Class.forName(name.substring(0, name.indexOf(".")).replace('/', '.'))); 91 if (count == classes.size()) { 92 break; 93 } 94 } 95 } 96 } 97 return classes; 98 } 99 } | 1 /* 2 * Copyright (c) 2013, 2015, 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; 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 print("JRT Filesystem Not Found"); 89 System.exit(100); 90 } 91 92 List<String> fileNames; 93 Path modules = fs.getPath("/modules"); 94 95 Predicate<String> startsWithJavaBase = path -> path.toString().startsWith("java.base/java"); 96 Predicate<String> startsWithJavaDesktop = path -> path.toString().startsWith("java.desktop/java"); 97 Predicate<String> startsWithJavaDataTransfer = path -> path.toString().startsWith("java.datatransfer/java"); 98 Predicate<String> startsWithJavaRMI = path -> path.toString().startsWith("java.rmi/java"); 99 Predicate<String> startsWithJavaSmartCardIO = path -> path.toString().startsWith("java.smartcardio/java"); 100 Predicate<String> startsWithJavaManagement = path -> path.toString().startsWith("java.management/java"); 101 Predicate<String> startsWithJavaXML = path -> path.toString().startsWith("java.xml/java"); 102 Predicate<String> startsWithJavaXMLBind = path -> path.toString().startsWith("java.xml.bind/java"); 103 Predicate<String> startsWithJavaScripting = path -> path.toString().startsWith("java.scripting/java"); 104 Predicate<String> startsWithJavaNaming = path -> path.toString().startsWith("java.naming/java"); 105 Predicate<String> startsWithJavaSQL = path -> path.toString().startsWith("java.sql/java"); 106 Predicate<String> startsWithJavaActivation = path -> path.toString().startsWith("java.activation/java"); 107 Predicate<String> startsWithJavaCompiler = path -> path.toString().startsWith("java.compiler/java"); 108 Predicate<String> startsWithJavaAnnotations = path -> path.toString().startsWith("java.annotations/java"); 109 Predicate<String> startsWithJavaTransaction = path -> path.toString().startsWith("java.transaction/java"); 110 Predicate<String> startsWithJavaLogging = path -> path.toString().startsWith("java.logging/java"); 111 Predicate<String> startsWithJavaCorba = path -> path.toString().startsWith("java.corba/java"); 112 Predicate<String> startsWithJavaPrefs = path -> path.toString().startsWith("java.prefs/java"); 113 114 fileNames = Files.walk(modules) 115 .map(Path::toString) 116 .filter(path -> path.toString().contains("java")) 117 .map(s -> s.substring(9)) // remove /modules/ from beginning 118 .filter(startsWithJavaBase 119 .or(startsWithJavaDesktop) 120 .or(startsWithJavaDataTransfer) 121 .or(startsWithJavaRMI) 122 .or(startsWithJavaSmartCardIO) 123 .or(startsWithJavaManagement) 124 .or(startsWithJavaXML) 125 .or(startsWithJavaXMLBind) 126 .or(startsWithJavaScripting) 127 .or(startsWithJavaNaming) 128 .or(startsWithJavaSQL) 129 .or(startsWithJavaActivation) 130 .or(startsWithJavaCompiler) 131 .or(startsWithJavaAnnotations) 132 .or(startsWithJavaTransaction) 133 .or(startsWithJavaLogging) 134 .or(startsWithJavaCorba) 135 .or(startsWithJavaPrefs)) 136 .map(s -> s.replace('/', '.')) 137 .filter(path -> path.toString().endsWith(".class")) 138 .map(s -> s.substring(0, s.length() - 6)) // drop .class 139 .map(s -> s.substring(s.indexOf("."))) 140 .filter(path -> path.toString().contains("java")) 141 .map(s -> s.substring(s.indexOf("java"))) 142 .collect(Collectors.toList()); 143 144 for (String name : fileNames) { 145 classes.add(Class.forName(name)); 146 if (count == classes.size()) { 147 break; 148 } 149 } 150 151 return classes; 152 } 153 } |