Skip to content

Commit 044f4ed

Browse files
author
Mandy Chung
committed
8326979: (jdeps) improve the error message for FindException caused by InvalidModuleDescriptorException
Reviewed-by: jpai, alanb
1 parent 71f9c4e commit 044f4ed

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

src/jdk.jdeps/share/classes/com/sun/tools/jdeps/JdepsTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ int run(String... args) {
536536
}
537537
return EXIT_CMDERR;
538538
} catch (ResolutionException | FindException e) {
539-
reportError("err.exception.message", e.getMessage());
539+
Throwable cause = e.getCause();
540+
reportError("err.exception.message", cause != null ? cause.getMessage() : e.getMessage());
540541
return EXIT_CMDERR;
541542
} catch (IOException e) {
542543
e.printStackTrace();
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8326979
27+
* @run main InvalidModuleDescriptor
28+
* @summary jdeps should print the exception message of the cause of FindException
29+
* instead of FindException
30+
*/
31+
32+
import java.io.IOException;
33+
import java.io.PrintWriter;
34+
import java.io.StringWriter;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
37+
import java.nio.file.Paths;
38+
import java.util.spi.ToolProvider;
39+
40+
import static java.nio.file.StandardOpenOption.CREATE_NEW;
41+
42+
public class InvalidModuleDescriptor {
43+
private static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes"));
44+
private static final ToolProvider JDEPS = ToolProvider.findFirst("jdeps").orElseThrow();
45+
private static final ToolProvider JAR = ToolProvider.findFirst("jar").orElseThrow();
46+
47+
public static void main(String... args) throws IOException {
48+
// create an automatic module with an invalid module descriptor (containing unnamed package)
49+
Path jarFile = Paths.get("hi.jar");
50+
String moduleName = "hi";
51+
int rc = createAutomaticModule(jarFile, moduleName);
52+
if (rc != 0) {
53+
throw new RuntimeException("Fail to create automatic module");
54+
}
55+
56+
// jdeps should fail with an error without stack trace
57+
String expectedError = "Error: InvalidModuleDescriptor.class found in top-level directory (unnamed package not allowed in module)";
58+
rc = runJdeps(expectedError, "--module-path", jarFile.toString(), "-m", moduleName);
59+
if (rc == 0) {
60+
throw new RuntimeException("Expected jdeps to fail");
61+
}
62+
}
63+
64+
// create an automatic module with an invalid module descriptor
65+
static int createAutomaticModule(Path jarFile, String moduleName) throws IOException {
66+
Path manifest = Paths.get("manifest");
67+
Files.writeString(manifest, "Automatic-Module-Name: " + moduleName, CREATE_NEW);
68+
return JAR.run(System.out, System.out, "--create", "--file", jarFile.toString(),
69+
"-m", manifest.toString(),
70+
"-C", TEST_CLASSES.toString(), "InvalidModuleDescriptor.class");
71+
}
72+
73+
static int runJdeps(String expected, String... args) {
74+
StringWriter output = new StringWriter();
75+
StringWriter error = new StringWriter();
76+
try (PrintWriter pwout = new PrintWriter(output);
77+
PrintWriter pwerr = new PrintWriter(error)) {
78+
int rc = JDEPS.run(pwout, pwerr, args);
79+
if (!output.toString().contains(expected)) {
80+
System.out.println(output);
81+
System.out.println(error);
82+
throw new RuntimeException("Mismatched output");
83+
}
84+
return rc;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)