J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | /* Testcase that does a defineClass with a NULL name on HelloWorld.class */ |
| 26 | |
| 27 | import java.io.*; |
| 28 | |
| 29 | public class DefineClass extends ClassLoader { |
| 30 | public static void main(String args[]) { |
| 31 | DefineClass t = new DefineClass(); |
| 32 | t.run(args); |
| 33 | } |
| 34 | public void run(String args[]) { |
| 35 | Class n; |
| 36 | byte b[] = new byte[10000]; |
| 37 | int len = 0; |
| 38 | String cdir; |
| 39 | String cfile; |
| 40 | |
| 41 | /* Class is found here: */ |
| 42 | cdir = System.getProperty("test.classes", "."); |
| 43 | cfile = cdir + java.io.File.separator + "HelloWorld.class"; |
| 44 | |
| 45 | try { |
| 46 | /* Construct byte array with complete class image in it. */ |
| 47 | FileInputStream fis = new FileInputStream(cfile); |
| 48 | int nbytes; |
| 49 | do { |
| 50 | nbytes = fis.read(b, len, b.length-len); |
| 51 | if ( nbytes > 0 ) { |
| 52 | len += nbytes; |
| 53 | } |
| 54 | } while ( nbytes > 0 ); |
| 55 | } catch ( Throwable x ) { |
| 56 | System.err.println("Cannot find " + cfile); |
| 57 | x.printStackTrace(); |
| 58 | } |
| 59 | |
| 60 | /* Define the class with null for the name */ |
| 61 | n = defineClass(null, b, 0, len); |
| 62 | |
| 63 | /* Try to create an instance of it */ |
| 64 | try { |
| 65 | n.newInstance(); |
| 66 | } catch ( Throwable x ) { |
| 67 | x.printStackTrace(); |
| 68 | } |
| 69 | } |
| 70 | } |