File: findLoadedClass.java

package info (click to toggle)
mauve 20080616-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,856 kB
  • ctags: 21,952
  • sloc: java: 234,107; sh: 2,834; xml: 208; makefile: 59
file content (248 lines) | stat: -rw-r--r-- 8,604 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Tags: JDK1.1

// Copyright (C) 2005 Free Software Foundation, Inc.
// Written by Jeroen Frijters  <jeroen@frijters.net>

// This file is part of Mauve.

// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.

// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.  */

package gnu.testlet.java.lang.ClassLoader;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

public class findLoadedClass extends ClassLoader implements Testlet
{
  // This represents the class:
  //   class Triv extends java.util.Hashtable {}
  private static byte[] trivialClassDef = {
    (byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE, (byte)0x00, (byte)0x03,
    (byte)0x00, (byte)0x2D, (byte)0x00, (byte)0x0F, (byte)0x07, (byte)0x00,
    (byte)0x0C, (byte)0x07, (byte)0x00, (byte)0x0E, (byte)0x0A, (byte)0x00,
    (byte)0x02, (byte)0x00, (byte)0x04, (byte)0x0C, (byte)0x00, (byte)0x06,
    (byte)0x00, (byte)0x05, (byte)0x01, (byte)0x00, (byte)0x03, (byte)0x28,
    (byte)0x29, (byte)0x56, (byte)0x01, (byte)0x00, (byte)0x06, (byte)0x3C,
    (byte)0x69, (byte)0x6E, (byte)0x69, (byte)0x74, (byte)0x3E, (byte)0x01,
    (byte)0x00, (byte)0x04, (byte)0x43, (byte)0x6F, (byte)0x64, (byte)0x65,
    (byte)0x01, (byte)0x00, (byte)0x0D, (byte)0x43, (byte)0x6F, (byte)0x6E,
    (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6E, (byte)0x74, (byte)0x56,
    (byte)0x61, (byte)0x6C, (byte)0x75, (byte)0x65, (byte)0x01, (byte)0x00,
    (byte)0x0A, (byte)0x45, (byte)0x78, (byte)0x63, (byte)0x65, (byte)0x70,
    (byte)0x74, (byte)0x69, (byte)0x6F, (byte)0x6E, (byte)0x73, (byte)0x01,
    (byte)0x00, (byte)0x0E, (byte)0x4C, (byte)0x6F, (byte)0x63, (byte)0x61,
    (byte)0x6C, (byte)0x56, (byte)0x61, (byte)0x72, (byte)0x69, (byte)0x61,
    (byte)0x62, (byte)0x6C, (byte)0x65, (byte)0x73, (byte)0x01, (byte)0x00,
    (byte)0x0A, (byte)0x53, (byte)0x6F, (byte)0x75, (byte)0x72, (byte)0x63,
    (byte)0x65, (byte)0x46, (byte)0x69, (byte)0x6C, (byte)0x65, (byte)0x01,
    (byte)0x00, (byte)0x04, (byte)0x54, (byte)0x72, (byte)0x69, (byte)0x76,
    (byte)0x01, (byte)0x00, (byte)0x09, (byte)0x54, (byte)0x72, (byte)0x69,
    (byte)0x76, (byte)0x2E, (byte)0x6A, (byte)0x61, (byte)0x76, (byte)0x61,
    (byte)0x01, (byte)0x00, (byte)0x13, (byte)0x6A, (byte)0x61, (byte)0x76,
    (byte)0x61, (byte)0x2F, (byte)0x75, (byte)0x74, (byte)0x69, (byte)0x6C,
    (byte)0x2F, (byte)0x48, (byte)0x61, (byte)0x73, (byte)0x68, (byte)0x74,
    (byte)0x61, (byte)0x62, (byte)0x6C, (byte)0x65, (byte)0x00, (byte)0x00,
    (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x00,
    (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00,
    (byte)0x00, (byte)0x06, (byte)0x00, (byte)0x05, (byte)0x00, (byte)0x01,
    (byte)0x00, (byte)0x07, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x11,
    (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00,
    (byte)0x00, (byte)0x05, (byte)0x2A, (byte)0xB7, (byte)0x00, (byte)0x03,
    (byte)0xB1, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
    (byte)0x01, (byte)0x00, (byte)0x0B, (byte)0x00, (byte)0x00, (byte)0x00,
    (byte)0x02, (byte)0x00, (byte)0x0D
  };

  private boolean broken;

  public findLoadedClass()
  {
  }

  protected synchronized Class loadClass(String name, boolean resolve)
    throws ClassNotFoundException
  {
    if (broken)
        throw new ClassNotFoundException();
    else
        return super.loadClass(name, resolve);
  }

  private findLoadedClass(ClassLoader parent)
  {
    super(parent);
  }

  public void test(TestHarness harness)
  {
    defineClass("Triv", trivialClassDef, 0, trivialClassDef.length);

    // defineClass should have registered the class
    harness.checkPoint("defineClass should register");
    checkLoaded(harness, this, "Triv");

    // make sure that the VM registers the initiating class loader
    harness.checkPoint("VM should register");
    checkLoaded(harness, this, "java.util.Hashtable");

    // types that weren't loaded shouldn't be visible
    harness.checkPoint("premature");
    harness.check(findLoadedClass("java.lang.Object") == null);

    // Class.forName() should register the initiating loader.
    harness.checkPoint("Class.forName");
    try
    {
        Class.forName("java.lang.Object", false, this);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
    }
    checkLoaded(harness, this, "java.lang.Object");

    // The above should also apply to arrays
    // Note that on Sun JDK 1.4 (not on 1.5), loading the component type
    // also make the array type visible, so we don't test that the array
    // is not visible at this point.
    try
    {
        Class.forName("[Ljava.lang.Object;", false, this);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
    }
    checkLoaded(harness, this, "[Ljava.lang.Object;");

    // Loading an array type, makes available the ultimate component type
    harness.checkPoint("array implies component type");
    harness.check(findLoadedClass("java.util.Vector") == null);    
    try
    {
        Class.forName("[[Ljava.util.Vector;", false, this);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
    }
    checkLoaded(harness, this, "java.util.Vector");

    // After loading a class thru a parent, we shouldn't be able to define it.
    harness.checkPoint("no redefine");
    findLoadedClass cl = new findLoadedClass(this);
    harness.check(cl.findLoadedClass("Triv") == null);
    try
    {
        Class.forName("Triv", false, cl);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
        throw new Error(x);
    }
    checkLoaded(harness, cl, "Triv");
    try
    {
        cl.defineClass("Triv", trivialClassDef, 0, trivialClassDef.length);
        harness.check(false);
    }
    catch(LinkageError _)
    {
        harness.check(true);
    }

    // Check multi level trickery
    harness.checkPoint("multi level");
    findLoadedClass grandParent = new findLoadedClass();
    grandParent.defineClass("Triv", trivialClassDef, 0, trivialClassDef.length);
    findLoadedClass parent = new findLoadedClass(grandParent);
    findLoadedClass child = new findLoadedClass(parent);
    try
    {
        Class.forName("Triv", false, child);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
        throw new Error(x);
    }
    try
    {
        parent.defineClass("Triv", trivialClassDef, 0, trivialClassDef.length);
        harness.check(true);
    }
    catch(LinkageError x)
    {
        harness.debug(x);
        harness.check(false);
    }
    try
    {
        Class c = Class.forName("Triv", false, child);
        harness.check(c.getClassLoader() == grandParent);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
        harness.check(false);
    }
    catch(LinkageError x)
    {
        harness.debug(x);
        harness.check(false);
    }
    // Even if a class loader is broken, Class.forName() should continue
    // to work.
    child.broken = true;
    try
    {
        Class c = Class.forName("Triv", false, child);
        harness.check(c.getClassLoader() == grandParent);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
        harness.check(false);
    }
    catch(LinkageError x)
    {
        harness.debug(x);
        harness.check(false);
    }

    // The VM should also look in the loaded classes cache, before calling loadClass()
    harness.checkPoint("VM consults cache");
    findLoadedClass newLoader = new findLoadedClass();
    try
    {
        Class.forName("java.util.Hashtable", false, newLoader);
    }
    catch(ClassNotFoundException x)
    {
        harness.debug(x);
        throw new Error(x);
    }
    newLoader.broken = true;
    newLoader.defineClass("Triv", trivialClassDef, 0, trivialClassDef.length);
  }

  private void checkLoaded(TestHarness harness, findLoadedClass cl, String name)
  {
    Class c = cl.findLoadedClass(name);
    harness.check(c != null && c.getName().equals(name));
  }
}