Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8262944: Improve exception message when automatic module lists provid…
…er class not in JAR file

Reviewed-by: dfuchs, jvernee, alanb, lancea, mchung
  • Loading branch information
sormuras authored and Alan Bateman committed Oct 6, 2021
1 parent b8af6a9 commit c10de35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Expand Up @@ -551,7 +551,7 @@ private ModuleDescriptor deriveModuleDescriptor(JarFile jf)
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);
Expand Down
31 changes: 25 additions & 6 deletions test/jdk/java/lang/module/AutomaticModulesTest.java
@@ -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
Expand All @@ -23,7 +23,7 @@

/**
* @test
* @bug 8142968 8253751
* @bug 8142968 8253751 8262944
* @library /test/lib
* @build AutomaticModulesTest
* jdk.test.lib.util.JarUtils
Expand All @@ -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;
Expand Down Expand Up @@ -434,10 +435,28 @@ public void testMissingProviderPackage() throws IOException {
Files.write(services.resolve("p.S"), Set.of("q.P"));

Path dir = Files.createTempDirectory(USER_DIR, "mods");
JarUtils.createJarFile(dir.resolve("m.jar"), tmpdir);

// should throw FindException
ModuleFinder.of(dir).findAll();
Path jarfile = dir.resolve("m.jar");
JarUtils.createJarFile(jarfile, tmpdir);

// 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());
}
}

/**
Expand Down

1 comment on commit c10de35

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.