-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reviewed-by: jlahoda
- Loading branch information
Showing
6 changed files
with
167 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/jdk.jshell/share/classes/jdk/jshell/tool/resources/TOOLING.jsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
void jar(String... args) { run("jar", args); } | ||
void javac(String... args) { run("javac", args); } | ||
void javadoc(String... args) { run("javadoc", args); } | ||
void javap(String... args) { run("javap", args); } | ||
void jdeps(String... args) { run("jdeps", args); } | ||
void jlink(String... args) { run("jlink", args); } | ||
void jmod(String... args) { run("jmod", args); } | ||
void jpackage(String... args) { run("jpackage", args); } | ||
|
||
void javap(Class<?> type) throws Exception { | ||
try { | ||
var name = type.getCanonicalName(); | ||
if (name == null) throw new IllegalArgumentException("Type not supported: " + type); | ||
if (type == Class.forName(name, false, ClassLoader.getSystemClassLoader())) { | ||
run("javap", "-c", "-v", "-s", name); | ||
return; | ||
} | ||
} catch (ClassNotFoundException ignored) { | ||
// fall-through | ||
} | ||
var temp = java.nio.file.Files.createTempFile("TOOLING-", ".class"); | ||
try { | ||
var name = type.getName().replace('.', '/') + ".class"; | ||
try (var in = type.getClassLoader().getResourceAsStream(name); | ||
var out = java.nio.file.Files.newOutputStream(temp)) { | ||
if (in == null) throw new AssertionError("Resource not found: " + name); | ||
in.transferTo(out); | ||
} | ||
run("javap", "-c", "-v", "-s", temp.toString()); | ||
} finally { | ||
java.nio.file.Files.delete(temp); | ||
} | ||
} | ||
|
||
void run(String name, String... args) { | ||
var tool = java.util.spi.ToolProvider.findFirst(name); | ||
if (tool.isEmpty()) throw new RuntimeException("No such tool found: " + name); | ||
var code = tool.get().run(System.out, System.err, args); | ||
if (code == 0) return; | ||
System.err.println(name + " returned non-zero exit code: " + code); | ||
} | ||
|
||
void tools() { | ||
java.util.ServiceLoader.load(java.util.spi.ToolProvider.class).stream() | ||
.map(java.util.ServiceLoader.Provider::get) | ||
.map(java.util.spi.ToolProvider::name) | ||
.sorted() | ||
.forEach(System.out::println); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2023, 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 | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @bug 8306560 | ||
* @summary Tests for snippets and methods defined in TOOLING.jsh | ||
* @modules jdk.compiler/com.sun.tools.javac.api | ||
* jdk.compiler/com.sun.tools.javac.main | ||
* jdk.jdeps/com.sun.tools.javap | ||
* jdk.jshell/jdk.internal.jshell.tool | ||
* @build KullaTesting TestingInputStream | ||
* @run testng ToolingTest | ||
*/ | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class ToolingTest extends ReplToolTesting { | ||
@Test | ||
public void testListToolingSnippets() { | ||
test( | ||
a -> assertCommand(a, "/open TOOLING", | ||
""), | ||
a -> assertCommandOutputContains(a, "/list", | ||
// Tool methods | ||
"void jar(String... args)", | ||
// ... | ||
"void jpackage(String... args)", | ||
// Utility methods | ||
"void javap(Class<?> type) throws Exception", | ||
"void run(String name, String... args)", | ||
"void tools()") | ||
); | ||
} | ||
|
||
@Test | ||
public void testDisassembleJavaLangObjectClass() { | ||
test( | ||
a -> assertCommand(a, "/open TOOLING", | ||
""), | ||
a -> assertCommandUserOutputContains(a, "javap(Object.class)", | ||
"Classfile jrt:/java.base/java/lang/Object.class", | ||
"SourceFile: \"Object.java\"") | ||
); | ||
} | ||
|
||
@Test | ||
public void testDisassembleNewRecordClass() { | ||
test( | ||
a -> assertCommand(a, "record Point(int x, int y) {}", | ||
"| created record Point"), | ||
a -> assertCommand(a, "/open TOOLING", | ||
""), | ||
a -> assertCommandUserOutputContains(a, "javap(Point.class)", | ||
"Classfile ", // Classfile /.../TOOLING-13366652659767559204.class | ||
"Point extends java.lang.Record", // public final class REPL.$JShell$11$Point extends java.lang.Record | ||
"SourceFile: \"$JShell$" // SourceFile: "$JShell$11.java" | ||
) | ||
); | ||
} | ||
} |
547a8b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Issues