Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ClassLoader/findSystemClass/Loader.java
38828 views
/*1* Copyright (c) 1998, 2010, 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*/2223/*24* This test runs in othervm mode as it tests ClassLoader.findSystemClass25* and getSystemResource methods.26*/2728/* @test29@bug 4147599 447815030@summary In 1.2beta4-I ClassLoader loaded classes can not link31against application classes.32@run main/othervm Loader33*/3435/*36* We are trying to test that certain methods of ClassLoader look at the same37* paths as they did in 1.1. To run this test on 1.1, you will have to pass38* "-1.1" as option on the command line.39*40* The required files are:41*42* - Loader.java (a 1.1 style class loader)43* - Loadee.java (source for a class that refers to Loader)44* - Loadee.classfile (to test findSystemClass)45* - Loadee.resource (to test getSystemResource)46* - java/lang/Object.class (to test getSystemResources)47*48* The extension ".classfile" is so the class file is not seen by any loader49* other than Loader. If you need to make any changes you will have to50* compile Loadee.java and rename Loadee.class to Loadee.classfile.51*/5253import java.io.File;54import java.io.DataInputStream;55import java.io.FileInputStream;56import java.io.IOException;57import java.net.URL;58import java.util.HashSet;596061/**62* A 1.1-style ClassLoader. The only class it can really load is "Loadee".63* For other classes it might be asked to load, it relies on loaders set up by64* the launcher.65*/66public class Loader extends ClassLoader {6768public Class loadClass(String name, boolean resolve)69throws ClassNotFoundException {70Class c = null;71try {72c = findSystemClass(name);73} catch (ClassNotFoundException cnfe) {74}75if (c == null) {76if (!name.equals("Loadee"))77throw new Error("java.lang.ClassLoader.findSystemClass() " +78"did not find class " + name);79byte[] b = locateBytes();80c = defineClass(name, b, 0, b.length);81}82if (resolve) {83resolveClass(c);84}85return c;86}8788private byte[] locateBytes() {89try {90File f = new File(System.getProperty("test.src", "."),91"Loadee.classfile");92long l = f.length();93byte[] b = new byte[(int)l];94DataInputStream in =95new DataInputStream(new FileInputStream(f));96in.readFully(b);97return b;98} catch (IOException ioe) {99ioe.printStackTrace();100throw new Error("Test failed due to IOException!");101}102}103104private static final int FIND = 0x1;105private static final int RESOURCE = 0x2;106private static final int RESOURCES = 0x4;107108public static void main(String[] args) throws Exception {109int tests = FIND | RESOURCE | RESOURCES;110111if (args.length == 1 && args[0].equals("-1.1")) {112tests &= ~RESOURCES; /* Do not run getResources test. */113}114115if ((tests & FIND) == FIND) {116report("findSystemClass()");117ClassLoader l = new Loader();118Class c = l.loadClass("Loadee");119Object o = c.newInstance();120}121122if ((tests & RESOURCE) == RESOURCE) {123report("getSystemResource()");124URL u = getSystemResource("Loadee.resource");125if (u == null)126throw new Exception127("java.lang.ClassLoader.getSystemResource() test failed!");128}129130if ((tests & RESOURCES) == RESOURCES) {131report("getSystemResources()");132java.util.Enumeration e =133getSystemResources("java/lang/Object.class");134HashSet hs = new HashSet();135while (e.hasMoreElements()) {136URL u = (URL)e.nextElement();137if (u == null)138break;139System.out.println("url: " + u);140hs.add(u);141}142if (hs.size() != 2) {143throw144new Exception("java.lang.ClassLoader.getSystemResources()"+145" did not find all resources");146}147}148}149150private static void report(String s) {151System.out.println("Testing java.lang.ClassLoader." + s + " ...");152}153}154155156