jdk/test/java/util/ResourceBundle/TestBug4179766.java
author rupashka
Mon, 17 May 2010 17:23:18 +0400
changeset 5581 9c2282c6f080
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6938481: 4906607 is not fixed for NIMBUS L&F Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
    @test  1.3 99/02/15
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
    @summary test Resource Bundle for bug 4179766
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
    @build Bug4179766Class Bug4179766Resource Bug4179766Getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
    @run main TestBug4179766
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
    @bug 4179766
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Portions copyright (c) 2007 Sun Microsystems, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Permission to use, copy, modify, and distribute this software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * and its documentation for NON-COMMERCIAL purposes and without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * fee is hereby granted provided that this copyright notice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * appears in all copies. Please refer to the file "copyright.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * for further important copyright and licensing information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
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
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * This class tests the behavior of the ResourceBundle cache with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * respect to ClassLoaders.  The same resource loaded by different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * loaders should be cached as separate objects, one for each loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * In order to test this behavior, this test constructs a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * class loader to load its resources.  It does not delegate resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * loading to the system loader to load the class files, but loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * them from the current directory instead.  This is so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * defining class loader for the resources is different.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * delegated to the system loader to load the resources, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * defining ClassLoader would be the same even though the initiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * loader differered, and the resource would only be cached once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
public class TestBug4179766 extends RBTestFmwk {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    //hash code used by class loaders when sameHash is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final int SAME_HASH_CODE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    //the next unique hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static int nextHashCode = SAME_HASH_CODE + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    //suffix on class files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final String CLASS_SUFFIX = ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    //generate a unique hashcode for a class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static synchronized int getNextHashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return nextHashCode++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        //static links so all needed classes get compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Object o1 = new Bug4179766Class();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Object o2 = new Bug4179766Resource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        new TestBug4179766().run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    * Ensure the resource cache is working correctly for a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    * resource from a single loader.  If we get the same resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    * from the same loader twice, we should get the same resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void testCache() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Loader loader = new Loader(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        ResourceBundle b1 = getResourceBundle(loader, "Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (b1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            errln("Resource not found: Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ResourceBundle b2 = getResourceBundle(loader, "Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (b2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            errln("Resource not found: Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        printIDInfo("[bundle1]",b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        printIDInfo("[bundle2]",b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (b1 != b2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            errln("Different objects returned by same ClassLoader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    * Test that loaders with the same hash key still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    * cache resources seperately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void testSameHash() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        doTest(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    * Test that loaders with different hash keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    * cache resources seperately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void testDifferentHash() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        doTest(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Ensure that cached resources for different ClassLoaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * are cached seperately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private void doTest(boolean sameHash) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ResourceBundle b1 = getResourceBundle(new Loader(sameHash), "Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (b1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
           errln("Resource not found: Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ResourceBundle b2 = getResourceBundle(new Loader(sameHash), "Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (b2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
           errln("Resource not found: Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        printIDInfo("[bundle1]",b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        printIDInfo("[bundle2]",b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (b1 == b2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
           errln("Same object returned by different ClassLoaders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Get a resource using a specified class loader to load the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private ResourceBundle getResourceBundle(Loader loader, String name) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            Class c = loader.loadClass("Bug4179766Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            Bug4179766Getter test = (Bug4179766Getter)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return test.getResourceBundle(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            errln("Class not found by custom class loader: "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            errln("Error instantiating: "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            errln("IllegalAccessException instantiating: "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Print information about an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * [message][object's identity][object's class][object's loader][loaders hash][loaders identity]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private void printIDInfo(String message, Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Class c = o.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ClassLoader l = c.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int hash = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            hash = l.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        logln(message + System.identityHashCode(o) + "  Class: " + c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                + "  ClassLoader: " + l + "  loaderHash: " + hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                + "  loaderPrimHash: " + System.identityHashCode(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * A simple class loader that loads classes from the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * working directory.  The hash code of the loader can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * set to be either the loaders identity or 0, allowing several
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * loaders to have the same hashCode value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public class Loader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        private int thisHashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         * Create a new loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public Loader(boolean sameHash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (sameHash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                thisHashCode = SAME_HASH_CODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                thisHashCode = getNextHashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * Return the hash code for this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return thisHashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * Get the data from the class file for the specified class.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * the file can't be found, or the class is not one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * special ones listed below, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         *    Bug4179766Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         *    Bug4179766Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        private byte[] getClassData(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            boolean shouldLoad = className.equals("Bug4179766Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            shouldLoad = shouldLoad || className.equals("Bug4179766Resource");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (shouldLoad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    File file = new File(System.getProperty("test.classes", "."), className+CLASS_SUFFIX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    FileInputStream fi = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    byte[] result = new byte[fi.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    fi.read(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * Load a class.  Files we can load take preference over ones the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * can load.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        public synchronized Class loadClass(String className, boolean resolveIt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            Class result = findLoadedClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                printInfo("        ***Returning cached class: "+className, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            byte[] classData = getClassData(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (classData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                //we don't have a local copy of this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                return loadFromSystem(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            result = defineClass(classData, 0, classData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                //there was an error defining the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return loadFromSystem(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (resolveIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                resolveClass(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            printInfo("        ***Loaded local class: "+className, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * Delegate loading to the system loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        private Class loadFromSystem(String className) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                Class result = super.findSystemClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                printInfo("        ***Returning system class: "+className, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                printInfo("        ***Class not found: "+className, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * Print information about a class that was loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * [loader identity][message][class identity]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        private void printInfo(String message, Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                logln(""+System.identityHashCode(this)+"  "+message+"  "+System.identityHashCode(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                logln(""+System.identityHashCode(this)+"  "+message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}