Path: blob/master/test/jdk/java/beans/XMLDecoder/8028054/Task.java
41153 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.List;25import java.util.stream.Collectors;26import java.net.*;27import java.io.*;28import java.nio.file.FileSystem;29import java.nio.file.FileSystems;30import java.nio.file.FileSystemNotFoundException;31import java.nio.file.ProviderNotFoundException;32import java.nio.file.Files;33import java.nio.file.Path;34import java.util.function.Predicate;3536abstract class Task<T> implements Runnable {37private transient boolean working = true;38private final List<T> methods;39private final Thread thread;4041Task(List<T> methods) {42this.methods = methods;43this.thread = new Thread(this);44this.thread.start();45}4647boolean isAlive() {48return this.thread.isAlive();49}5051boolean isWorking() {52boolean working = this.working && this.thread.isAlive();53this.working = false;54return working;55}5657@Override58public void run() {59long time = -System.currentTimeMillis();60for (T method : this.methods) {61this.working = true;62try {63for (int i = 0; i < 100; i++) {64process(method);65}66} catch (NoSuchMethodException ignore) {67}68}69time += System.currentTimeMillis();70print("thread done in " + time / 1000 + " seconds");71}7273protected abstract void process(T method) throws NoSuchMethodException;7475static synchronized void print(Object message) {76System.out.println(message);77System.out.flush();78}7980static List<Class<?>> getClasses(int count) throws Exception {81List<Class<?>> classes = new ArrayList<>();82FileSystem fs = null;8384try {85fs = FileSystems.getFileSystem(URI.create("jrt:/"));86} catch (ProviderNotFoundException | FileSystemNotFoundException e) {87throw new RuntimeException("FAIL - JRT Filesystem not found");88}8990List<String> fileNames;91Path modules = fs.getPath("/modules");9293Predicate<String> startsWithJavaBase = path -> path.toString().startsWith("java.base/java");94Predicate<String> startsWithJavaDesktop = path -> path.toString().startsWith("java.desktop/java");95Predicate<String> startsWithJavaDataTransfer = path -> path.toString().startsWith("java.datatransfer/java");96Predicate<String> startsWithJavaRMI = path -> path.toString().startsWith("java.rmi/java");97Predicate<String> startsWithJavaSmartCardIO = path -> path.toString().startsWith("java.smartcardio/java");98Predicate<String> startsWithJavaManagement = path -> path.toString().startsWith("java.management/java");99Predicate<String> startsWithJavaXML = path -> path.toString().startsWith("java.xml/java");100Predicate<String> startsWithJavaScripting = path -> path.toString().startsWith("java.scripting/java");101Predicate<String> startsWithJavaNaming = path -> path.toString().startsWith("java.naming/java");102Predicate<String> startsWithJavaSQL = path -> path.toString().startsWith("java.sql/java");103Predicate<String> startsWithJavaCompiler = path -> path.toString().startsWith("java.compiler/java");104Predicate<String> startsWithJavaLogging = path -> path.toString().startsWith("java.logging/java");105Predicate<String> startsWithJavaPrefs = path -> path.toString().startsWith("java.prefs/java");106107fileNames = Files.walk(modules)108.map(Path::toString)109.filter(path -> path.toString().contains("java"))110.map(s -> s.substring(9)) // remove /modules/ from beginning111.filter(startsWithJavaBase112.or(startsWithJavaDesktop)113.or(startsWithJavaDataTransfer)114.or(startsWithJavaRMI)115.or(startsWithJavaSmartCardIO)116.or(startsWithJavaManagement)117.or(startsWithJavaXML)118.or(startsWithJavaScripting)119.or(startsWithJavaNaming)120.or(startsWithJavaSQL)121.or(startsWithJavaCompiler)122.or(startsWithJavaLogging)123.or(startsWithJavaPrefs))124.map(s -> s.replace('/', '.'))125.filter(path -> path.toString().endsWith(".class"))126.map(s -> s.substring(0, s.length() - 6)) // drop .class127.map(s -> s.substring(s.indexOf(".")))128.filter(path -> path.toString().contains("java"))129.map(s -> s.substring(s.indexOf("java")))130.collect(Collectors.toList());131132for (String name : fileNames) {133classes.add(Class.forName(name));134if (count == classes.size()) {135break;136}137}138139return classes;140}141}142143144