java.lang.Class.newInstance() is supposed to throw an IllegalAccessException
if the class that invokes it has an inaccessible constructor. For instance,
the constructor Class() in java.lang.Class is private. The following code
should set state to 3.
Instead, the kestrel-solaris JDK is throwing an InstantiationException (an
exception that should be thrown if the class one wishes to instantiate is
abstract or an interface).
public class X {
public static void main(String[] args) {
Class cl=null;
Object o;
int state=0;
try {
cl = Class.forName("java.lang.Class");
}catch (ClassNotFoundException e) {
state = 1;
}
// If we find the class try and create a new instance
if (state == 0) {
try {
// we should not be able to instantiate Class
// as it has a private constructor
o = cl.newInstance();
}catch (InstantiationException e) {
System.out.println(e.getMessage());
state = 2;
}catch (IllegalAccessException e) {
System.out.println(e.getMessage());
state = 3;
}
}
System.out.println("state="+state);
}
}
The problem was discovered with Plum Hall test
plumhall-99a/jvs/lang/Class/newInstance_01_x
allan.jacobs@Eng 2000-04-06
if the class that invokes it has an inaccessible constructor. For instance,
the constructor Class() in java.lang.Class is private. The following code
should set state to 3.
Instead, the kestrel-solaris JDK is throwing an InstantiationException (an
exception that should be thrown if the class one wishes to instantiate is
abstract or an interface).
public class X {
public static void main(String[] args) {
Class cl=null;
Object o;
int state=0;
try {
cl = Class.forName("java.lang.Class");
}catch (ClassNotFoundException e) {
state = 1;
}
// If we find the class try and create a new instance
if (state == 0) {
try {
// we should not be able to instantiate Class
// as it has a private constructor
o = cl.newInstance();
}catch (InstantiationException e) {
System.out.println(e.getMessage());
state = 2;
}catch (IllegalAccessException e) {
System.out.println(e.getMessage());
state = 3;
}
}
System.out.println("state="+state);
}
}
The problem was discovered with Plum Hall test
plumhall-99a/jvs/lang/Class/newInstance_01_x
allan.jacobs@Eng 2000-04-06
- relates to
-
JDK-8364937 IllegalAccessError vs NoSuchMethodError for Nonexistent Class Constructor
-
- In Progress
-