sundar@1213: #// Usage: jjs -cp jd.js -- [jdis|jdec] sundar@1213: sundar@1213: /* sundar@1213: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. sundar@1213: * sundar@1213: * Redistribution and use in source and binary forms, with or without sundar@1213: * modification, are permitted provided that the following conditions sundar@1213: * are met: sundar@1213: * sundar@1213: * - Redistributions of source code must retain the above copyright sundar@1213: * notice, this list of conditions and the following disclaimer. sundar@1213: * sundar@1213: * - Redistributions in binary form must reproduce the above copyright sundar@1213: * notice, this list of conditions and the following disclaimer in the sundar@1213: * documentation and/or other materials provided with the distribution. sundar@1213: * sundar@1213: * - Neither the name of Oracle nor the names of its sundar@1213: * contributors may be used to endorse or promote products derived sundar@1213: * from this software without specific prior written permission. sundar@1213: * sundar@1213: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS sundar@1213: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, sundar@1213: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sundar@1213: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR sundar@1213: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, sundar@1213: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, sundar@1213: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR sundar@1213: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF sundar@1213: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING sundar@1213: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS sundar@1213: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sundar@1213: */ sundar@1213: sundar@1213: // javap-like disassembler/decoder tool that disassembles/decodes sundar@1213: // java classes but with OpenJDK AsmTools disassembler/decoder syntax. sundar@1213: // You need to build asmtool.jar from OpenJDK codetools project sundar@1213: // specify it with -cp option. sundar@1213: sundar@1213: // See also https://wiki.openjdk.java.net/display/CodeTools/AsmTools sundar@1213: sundar@1213: function usage() { sundar@1213: print("Usage: jjs -cp jd.js -- [jdis|jdec]"); sundar@1213: exit(1); sundar@1213: } sundar@1213: sundar@1213: if (arguments.length == 0) { sundar@1213: usage(); sundar@1213: } sundar@1213: sundar@1213: // argument handling sundar@1213: // convert to internal class name sundar@1213: var className = arguments[0].replaceAll('\\.', '/'); sundar@1213: var tool; sundar@1213: if (arguments.length > 1) { sundar@1213: tool = arguments[1]; sundar@1213: switch (tool) { sundar@1213: case 'jdis': sundar@1213: case 'jdec': sundar@1213: break; sundar@1213: default: sundar@1213: usage(); sundar@1213: } sundar@1213: } else { sundar@1213: tool = "jdis"; // default tool sundar@1213: } sundar@1213: sundar@1213: // Java classes used sundar@1213: var AsmTools = Java.type("org.openjdk.asmtools.Main"); sundar@1213: var Files = Java.type("java.nio.file.Files"); sundar@1213: var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption"); sundar@1213: sundar@1213: // retrive input stream for .class bytes sundar@1213: var cl = AsmTools.class.classLoader; sundar@1213: var res = cl.getResource(className + ".class"); sundar@1213: sundar@1213: if (res) { sundar@1213: var is = res.openStream(); sundar@1213: var tmpPath; sundar@1213: try { sundar@1213: // copy the content of the .class to a temp file sundar@1213: tmpPath = Files.createTempFile("asmtools-", ".class"); sundar@1213: // mark as delete-on-exit sundar@1213: tmpPath.toFile().deleteOnExit(); sundar@1213: Files.copy(is, tmpPath, [ StandardCopyOption.REPLACE_EXISTING ]); sundar@1213: } finally { sundar@1213: is.close(); sundar@1213: } sundar@1213: sundar@1213: // invoke asmtools Main sundar@1213: AsmTools.main([ tool, tmpPath.toString() ]); sundar@1213: } else { sundar@1213: print("no such class: " + arguments[0]); sundar@1213: exit(1); sundar@1213: }