/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; import com.oracle.java.testlibrary.*; /* * @test ClassListExerciser * @summary Exercise loading of classes from the classlist, make sure * no crashes or exceptions occur * @library /testlibrary * @run main/othervm ClassListExerciser createSharedArchive * @run main/othervm ClassListExerciser $JAVA_HOME/jre/lib/classlist * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=./test.jsa -Xshare:auto ClassListExerciser $JAVA_HOME/jre/lib/classlist */ /* What this class does: * - read the boot classlist * - for each class in a classlist: * - load it * - instantiate it (if default constructor exists) */ public class ClassListExerciser { private final static boolean DIAGNOSTIC_MODE = false; public static void main(String[] args) throws Exception { String firstArg = args[0]; if (args[0].equals("createSharedArchive")) { createSharedArchive(); } else { String classListPath = args[0].replace("$JAVA_HOME", System.getProperty("test.jdk")); ClassListExerciser cle = new ClassListExerciser(); ClassLoader classLoader = cle.getClassLoader(); cle.exercise(classListPath, classLoader); } } private static void createSharedArchive() throws Exception { ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( "-XX:+UnlockDiagnosticVMOptions", "-XX:SharedArchiveFile=./test.jsa", "-Xshare:dump"); OutputAnalyzer output = new OutputAnalyzer(pb.start()); output.shouldContain("Loading classes to share"); output.shouldHaveExitValue(0); } // This method is used to get the class loader for use in this test // Ideally, the bootstrap class loader should be used. However, it is // not accessible via Java API. Using system or application class loader is // fine in this case, since since by design such class loaders will // delegate loading of bootstrap classes to the bootstrap class loader. private ClassLoader getClassLoader() { return ClassLoader.getSystemClassLoader(); } private String getClassName(String lineFromClassList) { return lineFromClassList.split("\\.class")[0].replace('/', '.'); } // A filter to skip a given class, which has known problems loading // Returns true for skipping private boolean skipClass(String className) { if ( className.startsWith("sun.awt.X11.XErrorHandler") || (className.charAt(0) == '#') ) { return true; } // java.lang.ClassNotFoundException: javax.swing.text.AbstractDocument$InsertStringResult // on Windows only if (Platform.isWindows() && className.equals("javax.swing.text.AbstractDocument$InsertStringResult") ) { return true; } // java.lang.ClassNotFoundException: java.lang.invoke.MagicLambdaImpl // on Mac OSX only if (Platform.isOSX() && (className.equals("java.lang.invoke.MagicLambdaImpl") || className.equals("sun.lwawt.macosx.LWCToolkit$5") ) ) { return true; } return false; } public void exercise(String classListName, ClassLoader classLoader) throws RuntimeException, FileNotFoundException, IOException, ClassNotFoundException { BufferedReader reader = new BufferedReader(new FileReader(classListName)); String line = null; int nrOfLoadedClasses = 0; try { while ((line = reader.readLine()) != null) { String className = getClassName(line); if (DIAGNOSTIC_MODE) System.out.println(className); if (skipClass(className)) continue; Class c = classLoader.loadClass(className); nrOfLoadedClasses++; } } finally { reader.close(); } System.out.println("Number of loaded classes is " + nrOfLoadedClasses); } }