Loader.java revision 13901:b2a69d66dc65
1/* 2 * Copyright (c) 1998, 2010, 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/* 25 * This test runs in othervm mode as it tests ClassLoader.findSystemClass 26 * and getSystemResource methods. 27 */ 28 29/* @test 30 @bug 4147599 4478150 31 @summary In 1.2beta4-I ClassLoader loaded classes can not link 32 against application classes. 33 @run main/othervm Loader 34*/ 35 36/* 37 * We are trying to test that certain methods of ClassLoader look at the same 38 * paths as they did in 1.1. To run this test on 1.1, you will have to pass 39 * "-1.1" as option on the command line. 40 * 41 * The required files are: 42 * 43 * - Loader.java (a 1.1 style class loader) 44 * - Loadee.java (source for a class that refers to Loader) 45 * - Loadee.classfile (to test findSystemClass) 46 * - Loadee.resource (to test getSystemResource) 47 * 48 * The extension ".classfile" is so the class file is not seen by any loader 49 * other than Loader. If you need to make any changes you will have to 50 * compile Loadee.java and rename Loadee.class to Loadee.classfile. 51 */ 52 53import java.io.File; 54import java.io.DataInputStream; 55import java.io.FileInputStream; 56import java.io.IOException; 57import java.net.URL; 58import java.util.HashSet; 59 60 61/** 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 by 64 * the launcher. 65 */ 66public class Loader extends ClassLoader { 67 68 public Class loadClass(String name, boolean resolve) 69 throws ClassNotFoundException { 70 Class c = null; 71 try { 72 c = findSystemClass(name); 73 } catch (ClassNotFoundException cnfe) { 74 } 75 if (c == null) { 76 if (!name.equals("Loadee")) 77 throw new Error("java.lang.ClassLoader.findSystemClass() " + 78 "did not find class " + name); 79 byte[] b = locateBytes(); 80 c = defineClass(name, b, 0, b.length); 81 } 82 if (resolve) { 83 resolveClass(c); 84 } 85 return c; 86 } 87 88 private byte[] locateBytes() { 89 try { 90 File f = new File(System.getProperty("test.src", "."), 91 "Loadee.classfile"); 92 long l = f.length(); 93 byte[] b = new byte[(int)l]; 94 DataInputStream in = 95 new DataInputStream(new FileInputStream(f)); 96 in.readFully(b); 97 return b; 98 } catch (IOException ioe) { 99 ioe.printStackTrace(); 100 throw new Error("Test failed due to IOException!"); 101 } 102 } 103 104 private static final int FIND = 0x1; 105 private static final int RESOURCE = 0x2; 106 private static final int RESOURCES = 0x4; 107 108 public static void main(String[] args) throws Exception { 109 int tests = FIND | RESOURCE | RESOURCES; 110 111 if (args.length == 1 && args[0].equals("-1.1")) { 112 tests &= ~RESOURCES; /* Do not run getResources test. */ 113 } 114 115 if ((tests & FIND) == FIND) { 116 report("findSystemClass()"); 117 ClassLoader l = new Loader(); 118 Class c = l.loadClass("Loadee"); 119 Object o = c.newInstance(); 120 } 121 122 if ((tests & RESOURCE) == RESOURCE) { 123 report("getSystemResource()"); 124 URL u = getSystemResource("Loadee.resource"); 125 if (u == null) 126 throw new Exception 127 ("java.lang.ClassLoader.getSystemResource() test failed!"); 128 } 129 } 130 131 private static void report(String s) { 132 System.out.println("Testing java.lang.ClassLoader." + s + " ..."); 133 } 134} 135