8262944: Improve exception message when automatic module lists provider class not in JAR file

Reviewed-by: dfuchs, jvernee, alanb, lancea, mchung
This commit is contained in:
Christian Stein 2021-10-06 15:11:28 +00:00 committed by Alan Bateman
parent b8af6a9bfb
commit c10de3538b
2 changed files with 25 additions and 6 deletions

View File

@ -551,7 +551,7 @@ public class ModulePath implements ModuleFinder {
if (!cn.isEmpty()) {
String pn = packageName(cn);
if (!packages.contains(pn)) {
String msg = "Provider class " + cn + " not in module";
String msg = "Provider class " + cn + " not in JAR file " + fn;
throw new InvalidModuleDescriptorException(msg);
}
providerClasses.add(cn);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/**
* @test
* @bug 8142968 8253751
* @bug 8142968 8253751 8262944
* @library /test/lib
* @build AutomaticModulesTest
* jdk.test.lib.util.JarUtils
@ -35,6 +35,7 @@
import java.io.IOException;
import java.lang.module.Configuration;
import java.lang.module.FindException;
import java.lang.module.InvalidModuleDescriptorException;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Requires.Modifier;
import java.lang.module.ModuleFinder;
@ -434,10 +435,28 @@ public class AutomaticModulesTest {
Files.write(services.resolve("p.S"), Set.of("q.P"));
Path dir = Files.createTempDirectory(USER_DIR, "mods");
JarUtils.createJarFile(dir.resolve("m.jar"), tmpdir);
Path jarfile = dir.resolve("m.jar");
JarUtils.createJarFile(jarfile, tmpdir);
// should throw FindException
ModuleFinder.of(dir).findAll();
// catch FindException, inspect its cause's type and details, and rethrow
var expectedMessage = "Provider class q.P not in JAR file " + jarfile.getFileName();
try {
ModuleFinder.of(dir).findAll();
} catch (FindException exception) {
if (exception.getCause() instanceof InvalidModuleDescriptorException imde) {
var actualMessage = imde.getMessage();
if (actualMessage.equals(expectedMessage)) {
throw exception; // rethrow as expected
}
throw new AssertionError(
"""
Unexpected detail message in InvalidModuleDescriptorException:
Expected message -> '%s'
Actual message -> '%s'
""".formatted(expectedMessage, actualMessage));
}
throw new AssertionError("Unexpected exception cause: " + exception.getCause());
}
}
/**