Summary
Correct Java source launcher to reject programs with a private no-arg constructor.
Problem
The behaviour of the java launcher for the following HelloWorld.java program defining a private no-arg constructor is different when run in source and class mode:
class HelloWorld {
private HelloWorld() {}
void main() { IO.println("Hello world"); }
}
$ java HelloWorld.java
Hello world
javac HelloWorld.java | java HelloWorld
Error: no non-private zero argument constructor found in class HelloWorld
remove private from existing constructor or define as:
public HelloWorld()
Solution
Perform same check as in class launch mode and fail with a similar error message.
Specification
The check in class mode, found in LauncherHelper.java, reads:
if (Modifier.isPrivate(constructor.getModifiers())) {
throw new Fault(Errors.CantAccessConstructor(mainClassName));
}
That check should be adopted and applied to SourceLauncher.java as:
if (Modifier.isPrivate(constructor.getModifiers())) {
throw new Fault(Errors.CantUsePrivateConstructor(mainClassName));
}
- csr of
-
JDK-8371470 Java Launcher does not fail when running compact java-file with private no-arg constructor
-
- In Progress
-