test/java/beans/XMLDecoder/8028054/Task.java

Print this page


   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             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> startsWithJavaXMLBind       = path -> path.toString().startsWith("java.xml.bind/java");
 102         Predicate<String> startsWithJavaScripting     = path -> path.toString().startsWith("java.scripting/java");
 103         Predicate<String> startsWithJavaNaming        = path -> path.toString().startsWith("java.naming/java");
 104         Predicate<String> startsWithJavaSQL           = path -> path.toString().startsWith("java.sql/java");
 105         Predicate<String> startsWithJavaActivation    = path -> path.toString().startsWith("java.activation/java");
 106         Predicate<String> startsWithJavaCompiler      = path -> path.toString().startsWith("java.compiler/java");
 107         Predicate<String> startsWithJavaAnnotations   = path -> path.toString().startsWith("java.annotations/java");
 108         Predicate<String> startsWithJavaTransaction   = path -> path.toString().startsWith("java.transaction/java");
 109         Predicate<String> startsWithJavaLogging       = path -> path.toString().startsWith("java.logging/java");
 110         Predicate<String> startsWithJavaCorba         = path -> path.toString().startsWith("java.corba/java");
 111         Predicate<String> startsWithJavaPrefs         = path -> path.toString().startsWith("java.prefs/java");
 112 
 113         fileNames = Files.walk(modules)
 114                 .map(Path::toString)
 115                 .filter(path -> path.toString().contains("java"))
 116                 .map(s -> s.substring(9))  // remove /modules/ from beginning
 117                 .filter(startsWithJavaBase
 118                     .or(startsWithJavaDesktop)
 119                     .or(startsWithJavaDataTransfer)
 120                     .or(startsWithJavaRMI)
 121                     .or(startsWithJavaSmartCardIO)
 122                     .or(startsWithJavaManagement)
 123                     .or(startsWithJavaXML)
 124                     .or(startsWithJavaXMLBind)
 125                     .or(startsWithJavaScripting)
 126                     .or(startsWithJavaNaming)
 127                     .or(startsWithJavaSQL)
 128                     .or(startsWithJavaActivation)
 129                     .or(startsWithJavaCompiler)
 130                     .or(startsWithJavaAnnotations)
 131                     .or(startsWithJavaTransaction)
 132                     .or(startsWithJavaLogging)
 133                     .or(startsWithJavaCorba)
 134                     .or(startsWithJavaPrefs))
 135                 .map(s -> s.replace('/', '.'))
 136                 .filter(path -> path.toString().endsWith(".class"))
 137                 .map(s -> s.substring(0, s.length() - 6))  // drop .class
 138                 .map(s -> s.substring(s.indexOf(".")))
 139                 .filter(path -> path.toString().contains("java"))
 140                 .map(s -> s.substring(s.indexOf("java")))
 141                 .collect(Collectors.toList());
 142 
 143         for (String name : fileNames) {
 144             classes.add(Class.forName(name));





 145             if (count == classes.size()) {
 146                 break;
 147             }
 148         }
 149 

 150         return classes;
 151     }
 152 }