GNU header update.
[cacao.git] / tests / kaffe / ExceptionTestClassLoader.java
1 /*
2  * Test that classloader can properly supply exceptions.
3  *
4  * This test needs ExceptionTest.java
5  *
6  * @author Benjamin Reed <breed@almaden.ibm.com>
7  */
8 import java.io.*;
9 import java.util.*;
10 import java.lang.reflect.*;
11
12 public class ExceptionTestClassLoader extends ClassLoader {
13   private Class myLoadClass( String name ) throws ClassNotFoundException {
14     try {
15       if ( name.equals( "ExceptionTest" ) ) {
16         File file = new File( "ExceptionTest" );
17         FileInputStream fis = new FileInputStream( file );
18         byte buffer[] = new byte[(int)file.length()];
19         int size = 0;
20         while( size < file.length() ) {
21           int rc = fis.read( buffer, size, (int)file.length() - size );
22           size += rc;
23         }
24         return defineClass( null, buffer, 0, size );
25       }
26     } catch( Exception e ) {}
27     throw new ClassNotFoundException( name );
28   }
29
30   protected Class loadClass( String name, boolean resolve ) throws ClassNotFoundException {
31     if (name.equals("java.lang.Exception")) {
32        System.out.println("Success 2.");
33     }
34     Class theclass = findLoadedClass( name );
35     if ( theclass == null ) {
36       try {
37         theclass = findSystemClass( name );
38       } catch( Exception e ) { // Yes Exception is a bit too general
39         theclass = myLoadClass( name );
40       }
41     }
42     if ( theclass != null && resolve ) {
43       resolveClass( theclass );
44     }
45     return theclass;
46   }
47
48   public static void main( String args[] ) {
49     new File("ExceptionTest.class").renameTo(new File("ExceptionTest"));
50     Vector v = new Vector();
51     ClassLoader cl = new ExceptionTestClassLoader();
52     try {
53       Class classArgs[] = new Class[1];
54       classArgs[0] = String.class;
55       Constructor con = 
56         cl.loadClass( "ExceptionTest" ).getConstructor( classArgs );
57       Object instArgs[] = new Object[1];
58       instArgs[0] = "me";
59       con.newInstance( instArgs );
60     } catch ( Exception e ) {
61       e.printStackTrace();
62     }
63     System.out.println("Success 5.");
64   }
65 }
66
67
68
69
70
71 // Sort output
72 /* Expected Output:
73 Success 1.
74 Success 2.
75 Success 3.
76 Success 4.
77 Success 5.
78 */