Name: skT88420 Date: 05/06/99
/* Compile and run
The first direct call of newInstance throws the proper exception
because obviously you cant create a class this way.
However the second indirect call via invoke crashes the vm when
the returned object is accessed.
This must hold the record for the shortest code to crash the VM
*/
import java.lang.Class;
import java.lang.reflect.*;
public class Newinstance {
public static void main(String[] args) {
Class[] methArgs = {};
Class c = null;
try {
c = Class.forName("java.lang.Class");
Object o = c.newInstance();
if (o != null)
// Throws IllegalAccessException which is ok
o.toString();
}
catch (Throwable e) {
System.out.println("Proper Exception");
e.printStackTrace();
}
try {
Method m = c.getMethod("newInstance", methArgs);
Object o = m.invoke(c,null);
if (o != null)
// Now the VM crashes jdk 1.1.8 rc1
System.out.println("Now the VM crashes");
System.out.println("Object = " + o.toString());
}
catch (Throwable e) {
System.out.println("Exception does not occur");
e.printStackTrace();
}
}
}
(Review ID: 57896)
======================================================================