samples/jd.js

Wed, 23 Mar 2016 14:27:50 -0700

author
asaha
date
Wed, 23 Mar 2016 14:27:50 -0700
changeset 1861
6a43a158f561
parent 1213
98f6e6355a67
permissions
-rw-r--r--

Added tag jdk8u101-b00 for changeset 16d657d6cb22

sundar@1213 1 #// Usage: jjs -cp <asmtools.jar> jd.js -- <classname> [jdis|jdec]
sundar@1213 2
sundar@1213 3 /*
sundar@1213 4 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
sundar@1213 5 *
sundar@1213 6 * Redistribution and use in source and binary forms, with or without
sundar@1213 7 * modification, are permitted provided that the following conditions
sundar@1213 8 * are met:
sundar@1213 9 *
sundar@1213 10 * - Redistributions of source code must retain the above copyright
sundar@1213 11 * notice, this list of conditions and the following disclaimer.
sundar@1213 12 *
sundar@1213 13 * - Redistributions in binary form must reproduce the above copyright
sundar@1213 14 * notice, this list of conditions and the following disclaimer in the
sundar@1213 15 * documentation and/or other materials provided with the distribution.
sundar@1213 16 *
sundar@1213 17 * - Neither the name of Oracle nor the names of its
sundar@1213 18 * contributors may be used to endorse or promote products derived
sundar@1213 19 * from this software without specific prior written permission.
sundar@1213 20 *
sundar@1213 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
sundar@1213 22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
sundar@1213 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sundar@1213 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
sundar@1213 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
sundar@1213 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
sundar@1213 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
sundar@1213 28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
sundar@1213 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
sundar@1213 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sundar@1213 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sundar@1213 32 */
sundar@1213 33
sundar@1213 34 // javap-like disassembler/decoder tool that disassembles/decodes
sundar@1213 35 // java classes but with OpenJDK AsmTools disassembler/decoder syntax.
sundar@1213 36 // You need to build asmtool.jar from OpenJDK codetools project
sundar@1213 37 // specify it with -cp option.
sundar@1213 38
sundar@1213 39 // See also https://wiki.openjdk.java.net/display/CodeTools/AsmTools
sundar@1213 40
sundar@1213 41 function usage() {
sundar@1213 42 print("Usage: jjs -cp <asmtools.jar> jd.js -- <classname> [jdis|jdec]");
sundar@1213 43 exit(1);
sundar@1213 44 }
sundar@1213 45
sundar@1213 46 if (arguments.length == 0) {
sundar@1213 47 usage();
sundar@1213 48 }
sundar@1213 49
sundar@1213 50 // argument handling
sundar@1213 51 // convert to internal class name
sundar@1213 52 var className = arguments[0].replaceAll('\\.', '/');
sundar@1213 53 var tool;
sundar@1213 54 if (arguments.length > 1) {
sundar@1213 55 tool = arguments[1];
sundar@1213 56 switch (tool) {
sundar@1213 57 case 'jdis':
sundar@1213 58 case 'jdec':
sundar@1213 59 break;
sundar@1213 60 default:
sundar@1213 61 usage();
sundar@1213 62 }
sundar@1213 63 } else {
sundar@1213 64 tool = "jdis"; // default tool
sundar@1213 65 }
sundar@1213 66
sundar@1213 67 // Java classes used
sundar@1213 68 var AsmTools = Java.type("org.openjdk.asmtools.Main");
sundar@1213 69 var Files = Java.type("java.nio.file.Files");
sundar@1213 70 var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
sundar@1213 71
sundar@1213 72 // retrive input stream for .class bytes
sundar@1213 73 var cl = AsmTools.class.classLoader;
sundar@1213 74 var res = cl.getResource(className + ".class");
sundar@1213 75
sundar@1213 76 if (res) {
sundar@1213 77 var is = res.openStream();
sundar@1213 78 var tmpPath;
sundar@1213 79 try {
sundar@1213 80 // copy the content of the .class to a temp file
sundar@1213 81 tmpPath = Files.createTempFile("asmtools-", ".class");
sundar@1213 82 // mark as delete-on-exit
sundar@1213 83 tmpPath.toFile().deleteOnExit();
sundar@1213 84 Files.copy(is, tmpPath, [ StandardCopyOption.REPLACE_EXISTING ]);
sundar@1213 85 } finally {
sundar@1213 86 is.close();
sundar@1213 87 }
sundar@1213 88
sundar@1213 89 // invoke asmtools Main
sundar@1213 90 AsmTools.main([ tool, tmpPath.toString() ]);
sundar@1213 91 } else {
sundar@1213 92 print("no such class: " + arguments[0]);
sundar@1213 93 exit(1);
sundar@1213 94 }

mercurial