jdk/test/java/lang/ClassLoader/findSystemClass/Loader.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5796 ce710cdec799
child 36511 9d0388c6b336
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5796
diff changeset
     2
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
5796
ce710cdec799 6961518: TEST_BUG: add @run main/othervm in tests that call setSecurityManager
mchung
parents: 5506
diff changeset
    24
/*
ce710cdec799 6961518: TEST_BUG: add @run main/othervm in tests that call setSecurityManager
mchung
parents: 5506
diff changeset
    25
 * This test runs in othervm mode as it tests ClassLoader.findSystemClass
ce710cdec799 6961518: TEST_BUG: add @run main/othervm in tests that call setSecurityManager
mchung
parents: 5506
diff changeset
    26
 * and getSystemResource methods.
ce710cdec799 6961518: TEST_BUG: add @run main/othervm in tests that call setSecurityManager
mchung
parents: 5506
diff changeset
    27
 */
ce710cdec799 6961518: TEST_BUG: add @run main/othervm in tests that call setSecurityManager
mchung
parents: 5506
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
   @bug 4147599 4478150
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
   @summary In 1.2beta4-I ClassLoader loaded classes can not link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
            against application classes.
5796
ce710cdec799 6961518: TEST_BUG: add @run main/othervm in tests that call setSecurityManager
mchung
parents: 5506
diff changeset
    33
   @run main/othervm Loader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * We are trying to test that certain methods of ClassLoader look at the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * paths as they did in 1.1.  To run this test on 1.1, you will have to pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * "-1.1" as option on the command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The required files are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      - Loader.java            (a 1.1 style class loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      - Loadee.java            (source for a class that refers to Loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      - Loadee.classfile       (to test findSystemClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      - Loadee.resource        (to test getSystemResource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      - java/lang/Object.class (to test getSystemResources)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The extension ".classfile" is so the class file is not seen by any loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * other than Loader.  If you need to make any changes you will have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * compile Loadee.java and rename Loadee.class to Loadee.classfile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * A 1.1-style ClassLoader.  The only class it can really load is "Loadee".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * For other classes it might be asked to load, it relies on loaders set up by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * the launcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class Loader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public Class loadClass(String name, boolean resolve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            c = findSystemClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (!name.equals("Loadee"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                throw new Error("java.lang.ClassLoader.findSystemClass() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                "did not find class " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            byte[] b = locateBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            c = defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (resolve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            resolveClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private byte[] locateBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            File f   = new File(System.getProperty("test.src", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                "Loadee.classfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            long l   = f.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            byte[] b = new byte[(int)l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            DataInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                new DataInputStream(new FileInputStream(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            in.readFully(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new Error("Test failed due to IOException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int FIND      = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final int RESOURCE  = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final int RESOURCES = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int tests = FIND | RESOURCE | RESOURCES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (args.length == 1 && args[0].equals("-1.1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            tests &= ~RESOURCES; /* Do not run getResources test. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if ((tests & FIND) == FIND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            report("findSystemClass()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            ClassLoader l = new Loader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Class       c = l.loadClass("Loadee");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            Object      o = c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if ((tests & RESOURCE) == RESOURCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            report("getSystemResource()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            URL u = getSystemResource("Loadee.resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    ("java.lang.ClassLoader.getSystemResource() test failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if ((tests & RESOURCES) == RESOURCES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            report("getSystemResources()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            java.util.Enumeration e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                getSystemResources("java/lang/Object.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            HashSet hs = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                URL u = (URL)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                System.out.println("url: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                hs.add(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (hs.size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    new Exception("java.lang.ClassLoader.getSystemResources()"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                  " did not find all resources");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static void report(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        System.out.println("Testing java.lang.ClassLoader." + s + " ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}