jdk/test/java/lang/ClassLoader/findSystemClass/Loader.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
parent 7668 d4a77089c587
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The extension ".classfile" is so the class file is not seen by any loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * other than Loader.  If you need to make any changes you will have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * compile Loadee.java and rename Loadee.class to Loadee.classfile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * A 1.1-style ClassLoader.  The only class it can really load is "Loadee".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * For other classes it might be asked to load, it relies on loaders set up by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * the launcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class Loader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public Class loadClass(String name, boolean resolve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            c = findSystemClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (!name.equals("Loadee"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw new Error("java.lang.ClassLoader.findSystemClass() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                "did not find class " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            byte[] b = locateBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            c = defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (resolve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            resolveClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private byte[] locateBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            File f   = new File(System.getProperty("test.src", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                "Loadee.classfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            long l   = f.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            byte[] b = new byte[(int)l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            DataInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                new DataInputStream(new FileInputStream(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            in.readFully(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new Error("Test failed due to IOException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final int FIND      = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int RESOURCE  = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final int RESOURCES = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int tests = FIND | RESOURCE | RESOURCES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (args.length == 1 && args[0].equals("-1.1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            tests &= ~RESOURCES; /* Do not run getResources test. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if ((tests & FIND) == FIND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            report("findSystemClass()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            ClassLoader l = new Loader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            Class       c = l.loadClass("Loadee");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Object      o = c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if ((tests & RESOURCE) == RESOURCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            report("getSystemResource()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            URL u = getSystemResource("Loadee.resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    ("java.lang.ClassLoader.getSystemResource() test failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static void report(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        System.out.println("Testing java.lang.ClassLoader." + s + " ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}