8371470: Java Launcher does not fail when running compact java-file with private no-arg constructor

Reviewed-by: jpai
This commit is contained in:
Christian Stein 2025-12-02 13:32:22 +00:00
parent 6c01d3b088
commit c97d53a952
3 changed files with 36 additions and 11 deletions

View File

@ -226,6 +226,8 @@ public final class SourceLauncher {
Object instance = null;
// Similar to sun.launcher.LauncherHelper#checkAndLoadMain, including
// checks performed in LauncherHelper#validateMainMethod
if (!isStatic) {
if (Modifier.isAbstract(mainClass.getModifiers())) {
throw new Fault(Errors.CantInstantiate(mainClassName));
@ -238,6 +240,10 @@ public final class SourceLauncher {
throw new Fault(Errors.CantFindConstructor(mainClassName));
}
if (Modifier.isPrivate(constructor.getModifiers())) {
throw new Fault(Errors.CantUsePrivateConstructor(mainClassName));
}
try {
constructor.setAccessible(true);
instance = constructor.newInstance();

View File

@ -124,6 +124,12 @@ launcher.err.cant.access.main.method=\
launcher.err.cant.find.constructor=\
can''t find no argument constructor in class: {0}
# 0: string
launcher.err.cant.use.private.constructor=\
no non-private zero argument constructor found in class {0}\n\
remove private from existing constructor or define as:\n\
\ public {0}()
# 0: string
launcher.err.cant.access.constructor=\
can''t access no argument constructor in class: {0}

View File

@ -798,6 +798,22 @@ public class SourceLauncherTest extends TestRunner {
}
}
@Test
public void testPrivateConstructor(Path base) throws IOException {
tb.writeJavaFiles(base,
"""
class PrivateConstructor {
private PrivateConstructor() {}
void main() {}
}
""");
testError(base.resolve("PrivateConstructor.java"), "",
"""
error: no non-private zero argument constructor found in class PrivateConstructor
remove private from existing constructor or define as:
public PrivateConstructor()""");
}
@Test
public void testAbstractClassInstanceMain(Path base) throws IOException {
tb.writeJavaFiles(base,
@ -904,14 +920,6 @@ public class SourceLauncherTest extends TestRunner {
}
}
void checkContains(String name, String found, String expect) {
expect = expect.replace("\n", tb.lineSeparator);
out.println(name + ": " + found);
if (!found.contains(expect)) {
error("Expected output not found: " + expect);
}
}
void checkEqual(String name, List<String> found, List<String> expect) {
out.println(name + ": " + found);
tb.checkEqual(expect, found);
@ -939,7 +947,6 @@ public class SourceLauncherTest extends TestRunner {
}
void checkFault(String name, Throwable found, String expect) {
expect = expect.replace("\n", tb.lineSeparator);
out.println(name + ": " + found);
if (found == null) {
error("No exception thrown; expected Fault");
@ -947,8 +954,14 @@ public class SourceLauncherTest extends TestRunner {
if (!(found instanceof Fault)) {
error("Unexpected exception; expected Fault");
}
if (!(found.getMessage().equals(expect))) {
error("Unexpected detail message; expected: " + expect);
String actual = found.getMessage();
List<String> actualLines = actual.lines().toList();
List<String> expectLines = expect.lines().toList();
if (!(actualLines.equals(expectLines))) {
error("Unexpected detail message; expected: \n"
+ expect.indent(2)
+ "\nactual:\n"
+ actual.indent(2));
}
}
}