Path: blob/master/test/jdk/java/lang/ClassLoader/findSystemClass/Loader.java
41153 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*47* The extension ".classfile" is so the class file is not seen by any loader48* other than Loader. If you need to make any changes you will have to49* compile Loadee.java and rename Loadee.class to Loadee.classfile.50*/5152import java.io.File;53import java.io.DataInputStream;54import java.io.FileInputStream;55import java.io.IOException;56import java.net.URL;57import java.util.HashSet;585960/**61* A 1.1-style ClassLoader. The only class it can really load is "Loadee".62* For other classes it might be asked to load, it relies on loaders set up by63* the launcher.64*/65public class Loader extends ClassLoader {6667public Class loadClass(String name, boolean resolve)68throws ClassNotFoundException {69Class c = null;70try {71c = findSystemClass(name);72} catch (ClassNotFoundException cnfe) {73}74if (c == null) {75if (!name.equals("Loadee"))76throw new Error("java.lang.ClassLoader.findSystemClass() " +77"did not find class " + name);78byte[] b = locateBytes();79c = defineClass(name, b, 0, b.length);80}81if (resolve) {82resolveClass(c);83}84return c;85}8687private byte[] locateBytes() {88try {89File f = new File(System.getProperty("test.src", "."),90"Loadee.classfile");91long l = f.length();92byte[] b = new byte[(int)l];93DataInputStream in =94new DataInputStream(new FileInputStream(f));95in.readFully(b);96return b;97} catch (IOException ioe) {98ioe.printStackTrace();99throw new Error("Test failed due to IOException!");100}101}102103private static final int FIND = 0x1;104private static final int RESOURCE = 0x2;105private static final int RESOURCES = 0x4;106107public static void main(String[] args) throws Exception {108int tests = FIND | RESOURCE | RESOURCES;109110if (args.length == 1 && args[0].equals("-1.1")) {111tests &= ~RESOURCES; /* Do not run getResources test. */112}113114if ((tests & FIND) == FIND) {115report("findSystemClass()");116ClassLoader l = new Loader();117Class c = l.loadClass("Loadee");118Object o = c.newInstance();119}120121if ((tests & RESOURCE) == RESOURCE) {122report("getSystemResource()");123URL u = getSystemResource("Loadee.resource");124if (u == null)125throw new Exception126("java.lang.ClassLoader.getSystemResource() test failed!");127}128}129130private static void report(String s) {131System.out.println("Testing java.lang.ClassLoader." + s + " ...");132}133}134135136