# HG changeset patch # User Jaroslav Tulach # Date 1352528829 -3600 # Node ID 525b0a571518b8e3460c8dfa19b8e27a119b0b5d # Parent b06660b614db1cd0a4450bad6c1df04da8c16efb Removing code not directly related to parsing class files diff -r b06660b614db -r 525b0a571518 javap/src/main/java/sun/tools/javap/JavapEnvironment.java --- a/javap/src/main/java/sun/tools/javap/JavapEnvironment.java Fri Nov 09 21:33:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,355 +0,0 @@ -/* - * Copyright (c) 2002, 2006, 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. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * 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. - */ - - -package sun.tools.javap; - -import java.util.*; -import java.io.*; -import java.util.jar.*; - - -/** - * Strores flag values according to command line options - * and sets path where to find classes. - * - * @author Sucheta Dambalkar - */ -public class JavapEnvironment { - - //Access flags - public static final int PRIVATE = 0; - public static final int PROTECTED = 1; - public static final int PACKAGE = 2; - public static final int PUBLIC = 3; - - //search path flags. - private static final int start = 0; - private static final int cmdboot= 1; - private static final int sunboot = 2; - private static final int javaclass= 3; - private static final int cmdextdir= 4; - private static final int javaext= 5; - private static final int cmdclasspath= 6; - private static final int envclasspath= 7; - private static final int javaclasspath= 8; - private static final int currentdir = 9; - - - // JavapEnvironment flag settings - boolean showLineAndLocal = false; - int showAccess = PACKAGE; - boolean showDisassembled = false; - boolean showVerbose = false; - boolean showInternalSigs = false; - String classPathString = null; - String bootClassPathString = null; - String extDirsString = null; - boolean extDirflag = false; - boolean nothingToDo = true; - boolean showallAttr = false; - String classpath = null; - int searchpath = start; - - /** - * According to which flags are set, - * returns file input stream for classfile to disassemble. - */ - - public InputStream getFileInputStream(String Name){ - InputStream fileInStream = null; - searchpath = cmdboot; - try{ - if(searchpath == cmdboot){ - if(bootClassPathString != null){ - //search in specified bootclasspath. - classpath = bootClassPathString; - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path. - else searchpath = cmdextdir; - } - else searchpath = sunboot; - } - - if(searchpath == sunboot){ - if(System.getProperty("sun.boot.class.path") != null){ - //search in sun.boot.class.path - classpath = System.getProperty("sun.boot.class.path"); - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path - else searchpath = cmdextdir; - } - else searchpath = javaclass; - } - - if(searchpath == javaclass){ - if(System.getProperty("java.class.path") != null){ - //search in java.class.path - classpath =System.getProperty("java.class.path"); - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path - else searchpath = cmdextdir; - } - else searchpath = cmdextdir; - } - - if(searchpath == cmdextdir){ - if(extDirsString != null){ - //search in specified extdir. - classpath = extDirsString; - extDirflag = true; - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path - else { - searchpath = cmdclasspath; - extDirflag = false; - } - } - else searchpath = javaext; - } - - if(searchpath == javaext){ - if(System.getProperty("java.ext.dirs") != null){ - //search in java.ext.dirs - classpath = System.getProperty("java.ext.dirs"); - extDirflag = true; - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path - else { - searchpath = cmdclasspath; - extDirflag = false; - } - } - else searchpath = cmdclasspath; - } - if(searchpath == cmdclasspath){ - if(classPathString != null){ - //search in specified classpath. - classpath = classPathString; - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path - else searchpath = 8; - } - else searchpath = envclasspath; - } - - if(searchpath == envclasspath){ - if(System.getProperty("env.class.path")!= null){ - //search in env.class.path - classpath = System.getProperty("env.class.path"); - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path. - else searchpath = javaclasspath; - } - else searchpath = javaclasspath; - } - - if(searchpath == javaclasspath){ - if(("application.home") == null){ - //search in java.class.path - classpath = System.getProperty("java.class.path"); - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - //no classes found in search path. - else searchpath = currentdir; - } - else searchpath = currentdir; - } - - if(searchpath == currentdir){ - classpath = "."; - //search in current dir. - if((fileInStream = resolvefilename(Name)) != null) return fileInStream; - else { - //no classes found in search path. - error("Could not find "+ Name); - System.exit(1); - } - } - }catch(SecurityException excsec){ - excsec.printStackTrace(); - error("fatal exception"); - }catch(NullPointerException excnull){ - excnull.printStackTrace(); - error("fatal exception"); - }catch(IllegalArgumentException excill){ - excill.printStackTrace(); - error("fatal exception"); - } - - return null; - } - - - public void error(String msg) { - System.err.println("ERROR:" +msg); - } - - /** - * Resolves file name for classfile to disassemble. - */ - public InputStream resolvefilename(String name){ - String classname = name.replace('.', '/') + ".class"; - while (true) { - InputStream instream = extDirflag - ? resolveExdirFilename(classname) - : resolveclasspath(classname); - if (instream != null) - return instream; - int lastindex = classname.lastIndexOf('/'); - if (lastindex == -1) return null; - classname = classname.substring(0, lastindex) + "$" + - classname.substring(lastindex + 1); - } - } - - /** - * Resolves file name for classfile to disassemble if flag exdir is set. - */ - public InputStream resolveExdirFilename(String classname){ - if(classpath.indexOf(File.pathSeparator) != -1){ - //separates path - StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator); - while(st.hasMoreTokens()){ - String path = st.nextToken(); - InputStream in = resolveExdirFilenamehelper(path, classname); - if (in != null) - return in; - } - }else return (resolveExdirFilenamehelper(classpath, classname)); - - return null; - } - - /** - * Resolves file name for classfile to disassemble. - */ - public InputStream resolveclasspath(String classname){ - if(classpath.indexOf(File.pathSeparator) != -1){ - StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator); - //separates path. - while(st.hasMoreTokens()){ - String path = (st.nextToken()).trim(); - InputStream in = resolveclasspathhelper(path, classname); - if(in != null) return in; - - } - return null; - } - else return (resolveclasspathhelper(classpath, classname)); - } - - - /** - * Returns file input stream for classfile to disassemble if exdir is set. - */ - public InputStream resolveExdirFilenamehelper(String path, String classname){ - File fileobj = new File(path); - if(fileobj.isDirectory()){ - // gets list of files in that directory. - File[] filelist = fileobj.listFiles(); - for(int i = 0; i < filelist.length; i++){ - try{ - //file is a jar file. - if(filelist[i].toString().endsWith(".jar")){ - JarFile jfile = new JarFile(filelist[i]); - if((jfile.getEntry(classname)) != null){ - - InputStream filein = jfile.getInputStream(jfile.getEntry(classname)); - int bytearraysize = filein.available(); - byte []b = new byte[bytearraysize]; - int totalread = 0; - while(totalread < bytearraysize){ - totalread += filein.read(b, totalread, bytearraysize-totalread); - } - InputStream inbyte = new ByteArrayInputStream(b); - filein.close(); - return inbyte; - } - } else { - //not a jar file. - String filename = path+"/"+ classname; - File file = new File(filename); - if(file.isFile()){ - return (new FileInputStream(file)); - } - } - }catch(FileNotFoundException fnexce){ - fnexce.printStackTrace(); - error("cant read file"); - error("fatal exception"); - }catch(IOException ioexc){ - ioexc.printStackTrace(); - error("fatal exception"); - } - } - } - - return null; - } - - - /** - * Returns file input stream for classfile to disassemble. - */ - public InputStream resolveclasspathhelper(String path, String classname){ - File fileobj = new File(path); - try{ - if(fileobj.isDirectory()){ - //is a directory. - String filename = path+"/"+ classname; - File file = new File(filename); - if(file.isFile()){ - return (new FileInputStream(file)); - } - - }else if(fileobj.isFile()){ - if(fileobj.toString().endsWith(".jar")){ - //is a jar file. - JarFile jfile = new JarFile(fileobj); - if((jfile.getEntry(classname)) != null){ - InputStream filein = jfile.getInputStream(jfile.getEntry(classname)); - int bytearraysize = filein.available(); - byte []b = new byte[bytearraysize]; - int totalread = 0; - while(totalread < bytearraysize){ - totalread += filein.read(b, totalread, bytearraysize-totalread); - } - InputStream inbyte = new ByteArrayInputStream(b); - filein.close(); - return inbyte; - } - } - } - }catch(FileNotFoundException fnexce){ - fnexce.printStackTrace(); - error("cant read file"); - error("fatal exception"); - }catch(IOException ioexce){ - ioexce.printStackTrace(); - error("fatal exception"); - } - return null; - } -} diff -r b06660b614db -r 525b0a571518 javap/src/main/java/sun/tools/javap/JavapPrinter.java --- a/javap/src/main/java/sun/tools/javap/JavapPrinter.java Fri Nov 09 21:33:22 2012 +0100 +++ b/javap/src/main/java/sun/tools/javap/JavapPrinter.java Sat Nov 10 07:27:09 2012 +0100 @@ -37,874 +37,4 @@ * @author Sucheta Dambalkar */ public class JavapPrinter { - JavapEnvironment env; - ClassData cls; - byte[] code; - String lP= ""; - PrintWriter out; - - public JavapPrinter(InputStream cname, PrintWriter out, JavapEnvironment env){ - this.out = out; - this.cls = new ClassData(cname); - this.env = env; - } - - /** - * Entry point to print class file information. - */ - public void print(){ - printclassHeader(); - printfields(); - printMethods(); - printend(); - } - - /** - * Print a description of the class (not members). - */ - public void printclassHeader(){ - String srcName=""; - if ((srcName = cls.getSourceName()) != "null") // requires debug info - out.println("Compiled from " + javaclassname(srcName)); - - if(cls.isInterface()) { - // The only useful access modifier of an interface is - // public; interfaces are always marked as abstract and - // cannot be final. - out.print((cls.isPublic()?"public ":"") + - "interface "+ javaclassname(cls.getClassName())); - } - else if(cls.isClass()) { - String []accflags = cls.getAccess(); - printAccess(accflags); - out.print("class "+ javaclassname(cls.getClassName())); - - if(cls.getSuperClassName() != null){ - out.print(" extends " + javaclassname(cls.getSuperClassName())); - } - } - - String []interfacelist = cls.getSuperInterfaces(); - if(interfacelist.length > 0){ - if(cls.isClass()) { - out.print(" implements "); - } - else if(cls.isInterface()){ - out.print(" extends "); - } - - for(int j = 0; j < interfacelist.length; j++){ - out.print(javaclassname(interfacelist[j])); - - if((j+1) < interfacelist.length) { - out.print(","); - } - } - } - - // Print class attribute information. - if((env.showallAttr) || (env.showVerbose)){ - printClassAttributes(); - } - // Print verbose output. - if(env.showVerbose){ - printverbosecls(); - } - out.println("{"); - } - - /** - * Print verbose output. - */ - public void printverbosecls(){ - out.println(" minor version: "+cls.getMinor_version()); - out.println(" major version: "+cls.getMajor_version()); - out.println(" Constant pool:"); - printcp(); - env.showallAttr = true; - } - - /** - * Print class attribute information. - */ - public void printClassAttributes(){ - out.println(); - AttrData[] clsattrs = cls.getAttributes(); - for(int i = 0; i < clsattrs.length; i++){ - String clsattrname = clsattrs[i].getAttrName(); - if(clsattrname.equals("SourceFile")){ - out.println(" SourceFile: "+ cls.getSourceName()); - }else if(clsattrname.equals("InnerClasses")){ - printInnerClasses(); - }else { - printAttrData(clsattrs[i]); - } - } - } - - /** - * Print the fields - */ - public void printfields(){ - FieldData[] fields = cls.getFields(); - for(int f = 0; f < fields.length; f++){ - String[] accflags = fields[f].getAccess(); - if(checkAccess(accflags)){ - if(!(env. showLineAndLocal || env.showDisassembled || env.showVerbose - || env.showInternalSigs || env.showallAttr)){ - out.print(" "); - } - printAccess(accflags); - out.println(fields[f].getType()+" " +fields[f].getName()+";"); - if (env.showInternalSigs) { - out.println(" Signature: " + (fields[f].getInternalSig())); - } - - // print field attribute information. - if (env.showallAttr){ - printFieldAttributes(fields[f]); - - } - if((env.showDisassembled) || (env.showLineAndLocal)){ - out.println(); - } - } - } - } - - - /* print field attribute information. */ - public void printFieldAttributes(FieldData field){ - Vector fieldattrs = field.getAttributes(); - for(int j = 0; j < fieldattrs.size(); j++){ - String fieldattrname = ((AttrData)fieldattrs.elementAt(j)).getAttrName(); - if(fieldattrname.equals("ConstantValue")){ - printConstantValue(field); - }else if (fieldattrname.equals("Deprecated")){ - out.println("Deprecated: "+ field.isDeprecated()); - }else if (fieldattrname.equals("Synthetic")){ - out.println(" Synthetic: "+ field.isSynthetic()); - }else { - printAttrData((AttrData)fieldattrs.elementAt(j)); - } - } - out.println(); - } - - /** - * Print the methods - */ - public void printMethods(){ - MethodData[] methods = cls.getMethods(); - for(int m = 0; m < methods.length; m++){ - String[] accflags = methods[m].getAccess(); - if(checkAccess(accflags)){ - if(!(env. showLineAndLocal || env.showDisassembled || env.showVerbose - || env.showInternalSigs || env.showallAttr)){ - out.print(" "); - } - printMethodSignature(methods[m], accflags); - printExceptions(methods[m]); - out.println(";"); - - // Print internal signature of method. - if (env.showInternalSigs){ - out.println(" Signature: " + (methods[m].getInternalSig())); - } - - //Print disassembled code. - if(env.showDisassembled && ! env.showallAttr) { - printcodeSequence(methods[m]); - printExceptionTable(methods[m]); - out.println(); - } - - // Print line and local variable attribute information. - if (env.showLineAndLocal) { - printLineNumTable(methods[m]); - printLocVarTable(methods[m]); - out.println(); - } - - // Print method attribute information. - if (env.showallAttr){ - printMethodAttributes(methods[m]); - } - } - } - } - - /** - * Print method signature. - */ - public void printMethodSignature(MethodData method, String[] accflags){ - printAccess(accflags); - - if((method.getName()).equals("")){ - out.print(javaclassname(cls.getClassName())); - out.print(method.getParameters()); - }else if((method.getName()).equals("")){ - out.print("{}"); - }else{ - out.print(method.getReturnType()+" "); - out.print(method.getName()); - out.print(method.getParameters()); - } - } - - /** - * print method attribute information. - */ - public void printMethodAttributes(MethodData method){ - Vector methodattrs = method.getAttributes(); - Vector codeattrs = method.getCodeAttributes(); - for(int k = 0; k < methodattrs.size(); k++){ - String methodattrname = ((AttrData)methodattrs.elementAt(k)).getAttrName(); - if(methodattrname.equals("Code")){ - printcodeSequence(method); - printExceptionTable(method); - for(int c = 0; c < codeattrs.size(); c++){ - String codeattrname = ((AttrData)codeattrs.elementAt(c)).getAttrName(); - if(codeattrname.equals("LineNumberTable")){ - printLineNumTable(method); - }else if(codeattrname.equals("LocalVariableTable")){ - printLocVarTable(method); - }else if(codeattrname.equals("StackMapTable")) { - // Java SE JSR 202 stack map tables - printStackMapTable(method); - }else if(codeattrname.equals("StackMap")) { - // Java ME CLDC stack maps - printStackMap(method); - } else { - printAttrData((AttrData)codeattrs.elementAt(c)); - } - } - }else if(methodattrname.equals("Exceptions")){ - out.println(" Exceptions: "); - printExceptions(method); - }else if (methodattrname.equals("Deprecated")){ - out.println(" Deprecated: "+ method.isDeprecated()); - }else if (methodattrname.equals("Synthetic")){ - out.println(" Synthetic: "+ method.isSynthetic()); - }else { - printAttrData((AttrData)methodattrs.elementAt(k)); - } - } - out.println(); - } - - /** - * Print exceptions. - */ - public void printExceptions(MethodData method){ - int []exc_index_table = method.get_exc_index_table(); - if (exc_index_table != null) { - if(!(env. showLineAndLocal || env.showDisassembled || env.showVerbose - || env.showInternalSigs || env.showallAttr)){ - out.print(" "); - } - out.print(" throws "); - int k; - int l = exc_index_table.length; - - for (k=0; kopc_jsr_w) { - // pseudo opcodes should be printed as bytecodes - out.print("bytecode "+opcode); - return 1; - } - out.print(Tables.opcName(opcode)); - switch (opcode) { - case opc_aload: case opc_astore: - case opc_fload: case opc_fstore: - case opc_iload: case opc_istore: - case opc_lload: case opc_lstore: - case opc_dload: case opc_dstore: - case opc_ret: - out.print("\t"+getUbyte(pc+1)); - return 2; - case opc_iinc: - out.print("\t"+getUbyte(pc+1)+", "+getbyte(pc+2)); - return 3; - case opc_tableswitch:{ - int tb=align(pc+1); - int default_skip = getInt(tb); /* default skip pamount */ - int low = getInt(tb+4); - int high = getInt(tb+8); - int count = high - low; - out.print("{ //"+low+" to "+high); - for (int i = 0; i <= count; i++) - out.print( "\n\t\t" + (i+low) + ": "+lP+(pc+getInt(tb+12+4*i))+";"); - out.print("\n\t\tdefault: "+lP+(default_skip + pc) + " }"); - return tb-pc+16+count*4; - } - - case opc_lookupswitch:{ - int tb=align(pc+1); - int default_skip = getInt(tb); - int npairs = getInt(tb+4); - out.print("{ //"+npairs); - for (int i = 1; i <= npairs; i++) - out.print("\n\t\t"+getInt(tb+i*8) - +": "+lP+(pc+getInt(tb+4+i*8))+";" - ); - out.print("\n\t\tdefault: "+lP+(default_skip + pc) + " }"); - return tb-pc+(npairs+1)*8; - } - case opc_newarray: - int type=getUbyte(pc+1); - switch (type) { - case T_BOOLEAN:out.print(" boolean");break; - case T_BYTE: out.print(" byte"); break; - case T_CHAR: out.print(" char"); break; - case T_SHORT: out.print(" short"); break; - case T_INT: out.print(" int"); break; - case T_LONG: out.print(" long"); break; - case T_FLOAT: out.print(" float"); break; - case T_DOUBLE: out.print(" double"); break; - case T_CLASS: out.print(" class"); break; - default: out.print(" BOGUS TYPE:"+type); - } - return 2; - - case opc_anewarray: { - int index = getUShort(pc+1); - out.print("\t#"+index+"; //"); - PrintConstant(index); - return 3; - } - - case opc_sipush: - out.print("\t"+getShort(pc+1)); - return 3; - - case opc_bipush: - out.print("\t"+getbyte(pc+1)); - return 2; - - case opc_ldc: { - int index = getUbyte(pc+1); - out.print("\t#"+index+"; //"); - PrintConstant(index); - return 2; - } - - case opc_ldc_w: case opc_ldc2_w: - case opc_instanceof: case opc_checkcast: - case opc_new: - case opc_putstatic: case opc_getstatic: - case opc_putfield: case opc_getfield: - case opc_invokevirtual: - case opc_invokespecial: - case opc_invokestatic: { - int index = getUShort(pc+1); - out.print("\t#"+index+"; //"); - PrintConstant(index); - return 3; - } - - case opc_invokeinterface: { - int index = getUShort(pc+1), nargs=getUbyte(pc+3); - out.print("\t#"+index+", "+nargs+"; //"); - PrintConstant(index); - return 5; - } - - case opc_multianewarray: { - int index = getUShort(pc+1), dimensions=getUbyte(pc+3); - out.print("\t#"+index+", "+dimensions+"; //"); - PrintConstant(index); - return 4; - } - case opc_jsr: case opc_goto: - case opc_ifeq: case opc_ifge: case opc_ifgt: - case opc_ifle: case opc_iflt: case opc_ifne: - case opc_if_icmpeq: case opc_if_icmpne: case opc_if_icmpge: - case opc_if_icmpgt: case opc_if_icmple: case opc_if_icmplt: - case opc_if_acmpeq: case opc_if_acmpne: - case opc_ifnull: case opc_ifnonnull: - out.print("\t"+lP+(pc + getShort(pc+1)) ); - return 3; - - case opc_jsr_w: - case opc_goto_w: - out.print("\t"+lP+(pc + getInt(pc+1))); - return 5; - - default: - return 1; - } - } - /** - * Print code attribute details. - */ - public void printVerboseHeader(MethodData method) { - int argCount = method.getArgumentlength(); - if (!method.isStatic()) - ++argCount; // for 'this' - - out.println(" Stack=" + method.getMaxStack() - + ", Locals=" + method.getMaxLocals() - + ", Args_size=" + argCount); - - } - - - /** - * Print the exception table for this method code - */ - void printExceptionTable(MethodData method){//throws IOException - Vector exception_table = method.getexception_table(); - if (exception_table.size() > 0) { - out.println(" Exception table:"); - out.println(" from to target type"); - for (int idx = 0; idx < exception_table.size(); ++idx) { - TrapData handler = (TrapData)exception_table.elementAt(idx); - printFixedWidthInt(handler.start_pc, 6); - printFixedWidthInt(handler.end_pc, 6); - printFixedWidthInt(handler.handler_pc, 6); - out.print(" "); - int catch_cpx = handler.catch_cpx; - if (catch_cpx == 0) { - out.println("any"); - }else { - out.print("Class "); - out.println(cls.getClassName(catch_cpx)); - out.println(""); - } - } - } - } - - /** - * Print LineNumberTable attribute information. - */ - public void printLineNumTable(MethodData method) { - int numlines = method.getnumlines(); - Vector lin_num_tb = method.getlin_num_tb(); - if( lin_num_tb.size() > 0){ - out.println(" LineNumberTable: "); - for (int i=0; i 0){ - out.println(" LocalVariableTable: "); - out.print(" "); - out.println("Start Length Slot Name Signature"); - } - Vector loc_var_tb = method.getloc_var_tb(); - - for (int i=0; i 0) { - out.println(" StackMap: number_of_entries = " + number_of_entries); - - for (StackMapData frame : stack_map_tb) { - frame.print(this); - } - } - out.println(); - } - - /** - * Print StackMapTable attribute information. - */ - public void printStackMapTable(MethodData method) { - StackMapTableData[] stack_map_tb = method.getStackMapTable(); - int number_of_entries = stack_map_tb.length; - if (number_of_entries > 0) { - out.println(" StackMapTable: number_of_entries = " + number_of_entries); - - for (StackMapTableData frame : stack_map_tb) { - frame.print(this); - } - } - out.println(); - } - - void printMap(String name, int[] map) { - out.print(name); - for (int i=0; i> 8; - switch (type) { - case ITEM_Object: - out.print(" "); - PrintConstant(argument); - break; - case ITEM_NewObject: - out.print(" " + Tables.mapTypeName(type)); - out.print(" " + argument); - break; - default: - out.print(" " + Tables.mapTypeName(type)); - } - out.print( (i==(map.length-1)? ' ' : ',')); - } - out.println("]"); - } - - /** - * Print ConstantValue attribute information. - */ - public void printConstantValue(FieldData field){ - out.print(" Constant value: "); - int cpx = (field.getConstantValueIndex()); - byte tag=0; - try { - tag=cls.getTag(cpx); - - } catch (IndexOutOfBoundsException e) { - out.print("Error:"); - return; - } - switch (tag) { - case CONSTANT_METHOD: - case CONSTANT_INTERFACEMETHOD: - case CONSTANT_FIELD: { - CPX2 x = (CPX2)(cls.getCpoolEntry(cpx)); - if (x.cpx1 == cls.getthis_cpx()) { - // don't print class part for local references - cpx=x.cpx2; - } - } - } - out.print(cls.TagString(tag)+" "+ cls.StringValue(cpx)); - } - - /** - * Print InnerClass attribute information. - */ - public void printInnerClasses(){//throws ioexception - - InnerClassData[] innerClasses = cls.getInnerClasses(); - if(innerClasses != null){ - if(innerClasses.length > 0){ - out.print(" "); - out.println("InnerClass: "); - for(int i = 0 ; i < innerClasses.length; i++){ - out.print(" "); - //access - String[] accflags = innerClasses[i].getAccess(); - if(checkAccess(accflags)){ - printAccess(accflags); - if (innerClasses[i].inner_name_index!=0) { - out.print("#"+innerClasses[i].inner_name_index+"= "); - } - out.print("#"+innerClasses[i].inner_class_info_index); - if (innerClasses[i].outer_class_info_index!=0) { - out.print(" of #"+innerClasses[i].outer_class_info_index); - } - out.print("; //"); - if (innerClasses[i].inner_name_index!=0) { - out.print(cls.getName(innerClasses[i].inner_name_index)+"="); - } - PrintConstant(innerClasses[i].inner_class_info_index); - if (innerClasses[i].outer_class_info_index!=0) { - out.print(" of "); - PrintConstant(innerClasses[i].outer_class_info_index); - } - out.println(); - } - } - - } - } - } - - /** - * Print constant pool information. - */ - public void printcp(){ - int cpx = 1 ; - - while (cpx < cls.getCpoolCount()) { - out.print("const #"+cpx+" = "); - cpx+=PrintlnConstantEntry(cpx); - } - out.println(); - } - - /** - * Print constant pool entry information. - */ - public int PrintlnConstantEntry(int cpx) { - int size=1; - byte tag=0; - try { - tag=cls.getTag(cpx); - } catch (IndexOutOfBoundsException e) { - out.println(" "); - return 1; - } - out.print(cls.StringTag(cpx)+"\t"); - Object x=cls.getCpoolEntryobj(cpx); - if (x==null) { - switch (tag) { - case CONSTANT_LONG: - case CONSTANT_DOUBLE: - size=2; - } - out.println("null;"); - return size; - } - String str=cls.StringValue(cpx); - - switch (tag) { - case CONSTANT_CLASS: - case CONSTANT_STRING: - out.println("#"+(((CPX)x).cpx)+";\t// "+str); - break; - case CONSTANT_FIELD: - case CONSTANT_METHOD: - case CONSTANT_INTERFACEMETHOD: - out.println("#"+((CPX2)x).cpx1+".#"+((CPX2)x).cpx2+";\t// "+str); - break; - case CONSTANT_NAMEANDTYPE: - out.println("#"+((CPX2)x).cpx1+":#"+((CPX2)x).cpx2+";// "+str); - break; - case CONSTANT_LONG: - case CONSTANT_DOUBLE: - size=2; - default: - out.println(str+";"); - } - return size; - } - - /** - * Checks access of class, field or method. - */ - public boolean checkAccess(String accflags[]){ - - boolean ispublic = false; - boolean isprotected = false; - boolean isprivate = false; - boolean ispackage = false; - - for(int i= 0; i < accflags.length; i++){ - if(accflags[i].equals("public")) ispublic = true; - else if (accflags[i].equals("protected")) isprotected = true; - else if (accflags[i].equals("private")) isprivate = true; - } - - if(!(ispublic || isprotected || isprivate)) ispackage = true; - - if((env.showAccess == env.PUBLIC) && (isprotected || isprivate || ispackage)) return false; - else if((env.showAccess == env.PROTECTED) && (isprivate || ispackage)) return false; - else if((env.showAccess == env.PACKAGE) && (isprivate)) return false; - else return true; - } - - /** - * Prints access of class, field or method. - */ - public void printAccess(String []accflags){ - for(int j = 0; j < accflags.length; j++){ - out.print(accflags[j]+" "); - } - } - - /** - * Print an integer so that it takes 'length' characters in - * the output. Temporary until formatting code is stable. - */ - public void printFixedWidthInt(long x, int length) { - CharArrayWriter baStream = new CharArrayWriter(); - PrintWriter pStream = new PrintWriter(baStream); - pStream.print(x); - String str = baStream.toString(); - for (int cnt = length - str.length(); cnt > 0; --cnt) - out.print(' '); - out.print(str); - } - - protected int getbyte (int pc) { - return code[pc]; - } - - protected int getUbyte (int pc) { - return code[pc]&0xFF; - } - - int getShort (int pc) { - return (code[pc]<<8) | (code[pc+1]&0xFF); - } - - int getUShort (int pc) { - return ((code[pc]<<8) | (code[pc+1]&0xFF))&0xFFFF; - } - - protected int getInt (int pc) { - return (getShort(pc)<<16) | (getShort(pc+2)&0xFFFF); - } - - /** - * Print constant value at that index. - */ - void PrintConstant(int cpx) { - if (cpx==0) { - out.print("#0"); - return; - } - byte tag=0; - try { - tag=cls.getTag(cpx); - - } catch (IndexOutOfBoundsException e) { - out.print("#"+cpx); - return; - } - switch (tag) { - case CONSTANT_METHOD: - case CONSTANT_INTERFACEMETHOD: - case CONSTANT_FIELD: { - // CPX2 x=(CPX2)(cpool[cpx]); - CPX2 x = (CPX2)(cls.getCpoolEntry(cpx)); - if (x.cpx1 == cls.getthis_cpx()) { - // don't print class part for local references - cpx=x.cpx2; - } - } - } - out.print(cls.TagString(tag)+" "+ cls.StringValue(cpx)); - } - - protected static int align (int n) { - return (n+3) & ~3 ; - } - - public void printend(){ - out.println("}"); - out.println(); - } - - public String javaclassname(String name){ - return name.replace('/','.'); - } - - /** - * Print attribute data in hex. - */ - public void printAttrData(AttrData attr){ - byte []data = attr.getData(); - int i = 0; - int j = 0; - out.print(" "+attr.getAttrName()+": "); - out.println("length = " + cls.toHex(attr.datalen)); - - out.print(" "); - - - while (i < data.length){ - String databytestring = cls.toHex(data[i]); - if(databytestring.equals("0x")) out.print("00"); - else if(databytestring.substring(2).length() == 1){ - out.print("0"+databytestring.substring(2)); - } else{ - out.print(databytestring.substring(2)); - } - - j++; - if(j == 16) { - out.println(); - out.print(" "); - j = 0; - } - else out.print(" "); - i++; - } - out.println(); - } } diff -r b06660b614db -r 525b0a571518 javap/src/main/java/sun/tools/javap/Main.java --- a/javap/src/main/java/sun/tools/javap/Main.java Fri Nov 09 21:33:22 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,216 +0,0 @@ -/* - * Copyright (c) 2002, 2003, 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. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * 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. - */ - - - -package sun.tools.javap; - -import java.util.*; -import java.io.*; - -/** - * Entry point for javap, class file disassembler. - * - * @author Sucheta Dambalkar (Adopted code from old javap) - */ -public class Main{ - - private Vector classList = new Vector(); - private PrintWriter out; - JavapEnvironment env = new JavapEnvironment(); - private static boolean errorOccurred = false; - private static final String progname = "javap"; - - - public Main(PrintWriter out){ - this.out = out; - } - - public static void main(String argv[]) { - entry(argv); - if (errorOccurred) { - System.exit(1); - } - } - - - /** - * Entry point for tool if you don't want System.exit() called. - */ - public static void entry(String argv[]) { - PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); - try { - - Main jpmain = new Main(out); - jpmain.perform(argv); - - } finally { - out.close(); - } - } - - /** - * Process the arguments and perform the desired action - */ - private void perform(String argv[]) { - if (parseArguments(argv)) { - displayResults(); - - } - } - - private void error(String msg) { - errorOccurred = true; - System.err.println(msg); - System.err.flush(); - } - - /** - * Print usage information - */ - private void usage() { - java.io.PrintStream out = System.out; - out.println("Usage: " + progname + " ..."); - out.println(); - out.println("where options include:"); - out.println(" -c Disassemble the code"); - out.println(" -classpath Specify where to find user class files"); - out.println(" -extdirs Override location of installed extensions"); - out.println(" -help Print this usage message"); - out.println(" -J Pass directly to the runtime system"); - out.println(" -l Print line number and local variable tables"); - out.println(" -public Show only public classes and members"); - out.println(" -protected Show protected/public classes and members"); - out.println(" -package Show package/protected/public classes"); - out.println(" and members (default)"); - out.println(" -private Show all classes and members"); - out.println(" -s Print internal type signatures"); - out.println(" -bootclasspath Override location of class files loaded"); - out.println(" by the bootstrap class loader"); - out.println(" -verbose Print stack size, number of locals and args for methods"); - out.println(" If verifying, print reasons for failure"); - out.println(); - } - - /** - * Parse the command line arguments. - * Set flags, construct the class list and create environment. - */ - private boolean parseArguments(String argv[]) { - for (int i = 0 ; i < argv.length ; i++) { - String arg = argv[i]; - if (arg.startsWith("-")) { - if (arg.equals("-l")) { - env.showLineAndLocal = true; - } else if (arg.equals("-private") || arg.equals("-p")) { - env.showAccess = env.PRIVATE; - } else if (arg.equals("-package")) { - env.showAccess = env.PACKAGE; - } else if (arg.equals("-protected")) { - env.showAccess = env.PROTECTED; - } else if (arg.equals("-public")) { - env.showAccess = env.PUBLIC; - } else if (arg.equals("-c")) { - env.showDisassembled = true; - } else if (arg.equals("-s")) { - env.showInternalSigs = true; - } else if (arg.equals("-verbose")) { - env.showVerbose = true; - } else if (arg.equals("-v")) { - env.showVerbose = true; - } else if (arg.equals("-h")) { - error("-h is no longer available - use the 'javah' program"); - return false; - } else if (arg.equals("-verify")) { - error("-verify is no longer available - use 'java -verify'"); - return false; - } else if (arg.equals("-verify-verbose")) { - error("-verify is no longer available - use 'java -verify'"); - return false; - } else if (arg.equals("-help")) { - usage(); - return false; - } else if (arg.equals("-classpath")) { - if ((i + 1) < argv.length) { - env.classPathString = argv[++i]; - } else { - error("-classpath requires argument"); - usage(); - return false; - } - } else if (arg.equals("-bootclasspath")) { - if ((i + 1) < argv.length) { - env.bootClassPathString = argv[++i]; - } else { - error("-bootclasspath requires argument"); - usage(); - return false; - } - } else if (arg.equals("-extdirs")) { - if ((i + 1) < argv.length) { - env.extDirsString = argv[++i]; - } else { - error("-extdirs requires argument"); - usage(); - return false; - } - } else if (arg.equals("-all")) { - env.showallAttr = true; - } else { - error("invalid flag: " + arg); - usage(); - return false; - } - } else { - classList.addElement(arg); - env.nothingToDo = false; - } - } - if (env.nothingToDo) { - System.out.println("No classes were specified on the command line. Try -help."); - errorOccurred = true; - return false; - } - return true; - } - - /** - * Display results - */ - private void displayResults() { - for (int i = 0; i < classList.size() ; i++ ) { - String Name = (String)classList.elementAt(i); - InputStream classin = env.getFileInputStream(Name); - - try { - JavapPrinter printer = new JavapPrinter(classin, out, env); - printer.print(); // actual do display - - } catch (IllegalArgumentException exc) { - error(exc.getMessage()); - } - } - } -}