# HG changeset patch # User Jaroslav Tulach # Date 1353049716 -3600 # Node ID 77f7135b6eb1c61f259deae8104b638d33ad7ac1 # Parent 413d37a24a4d2bb9c9f5e602677ad5047d8580df Re-packaging javap as we are about to make significant changes to it anyway diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/AnnotationParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/AnnotationParser.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; + +/** An abstract parser for annotation definitions. Analyses the bytes and + * performs some callbacks to the overriden parser methods. + * + * @author Jaroslav Tulach + */ +public class AnnotationParser { + protected AnnotationParser() { + } + + protected void visitAttr(String type, String attr, String value) { + } + + /** Initialize the parsing with constant pool from cd. + * + * @param attr the attribute defining annotations + * @param cd constant pool + * @throws IOException in case I/O fails + */ + public final void parse(byte[] attr, ClassData cd) throws IOException { + ByteArrayInputStream is = new ByteArrayInputStream(attr); + DataInputStream dis = new DataInputStream(is); + try { + read(dis, cd); + } finally { + is.close(); + } + } + + private void read(DataInputStream dis, ClassData cd) throws IOException { + int cnt = dis.readUnsignedShort(); + for (int i = 0; i < cnt; i++) { + readAnno(dis, cd); + } + } + + private void readAnno(DataInputStream dis, ClassData cd) throws IOException { + int type = dis.readUnsignedShort(); + String typeName = cd.StringValue(type); + int cnt = dis.readUnsignedShort(); + for (int i = 0; i < cnt; i++) { + String attrName = cd.StringValue(dis.readUnsignedShort()); + readValue(dis, cd, typeName, attrName); + } + } + + private void readValue(DataInputStream dis, ClassData cd, String typeName, String attrName) + throws IOException { + char type = (char)dis.readByte(); + if (type == '@') { + readAnno(dis, cd); + } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N + int primitive = dis.readUnsignedShort(); + visitAttr(typeName, attrName, cd.StringValue(primitive)); + } else if (type == 'c') { + int cls = dis.readUnsignedShort(); + } else if (type == '[') { + int cnt = dis.readUnsignedShort(); + for (int i = 0; i < cnt; i++) { + readValue(dis, cd, typeName, attrName); + } + } else if (type == 'e') { + int enumT = dis.readUnsignedShort(); + int enumN = dis.readUnsignedShort(); + } else { + throw new IOException("Unknown type " + type); + } + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/AttrData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/AttrData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +import java.io.*; + +/** + * Reads and stores attribute information. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +class AttrData { + ClassData cls; + int name_cpx; + int datalen; + byte data[]; + + public AttrData (ClassData cls) { + this.cls=cls; + } + + /** + * Reads unknown attribute. + */ + public void read(int name_cpx, DataInputStream in) throws IOException { + this.name_cpx=name_cpx; + datalen=in.readInt(); + data=new byte[datalen]; + in.readFully(data); + } + + /** + * Reads just the name of known attribute. + */ + public void read(int name_cpx){ + this.name_cpx=name_cpx; + } + + /** + * Returns attribute name. + */ + public String getAttrName(){ + return cls.getString(name_cpx); + } + + /** + * Returns attribute data. + */ + public byte[] getData(){ + return data; + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/CPX.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/CPX.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +/** + * Stores constant pool entry information with one field. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +class CPX { + int cpx; + + CPX (int cpx) { + this.cpx=cpx; + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/CPX2.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/CPX2.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +/** + * Stores constant pool entry information with two fields. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +class CPX2 { + int cpx1,cpx2; + + CPX2 (int cpx1, int cpx2) { + this.cpx1=cpx1; + this.cpx2=cpx2; + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/ClassData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/ClassData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,718 @@ +/* + * Copyright (c) 2002, 2004, 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 org.apidesign.javap; + +import java.io.*; + +/** + * Central data repository of the Java Disassembler. + * Stores all the information in java class file. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +public final class ClassData implements RuntimeConstants { + + private int magic; + private int minor_version; + private int major_version; + private int cpool_count; + private Object cpool[]; + private int access; + private int this_class = 0;; + private int super_class; + private int interfaces_count; + private int[] interfaces = new int[0];; + private int fields_count; + private FieldData[] fields; + private int methods_count; + private MethodData[] methods; + private InnerClassData[] innerClasses; + private int attributes_count; + private AttrData[] attrs; + private String classname; + private String superclassname; + private int source_cpx=0; + private byte tags[]; + private Hashtable indexHashAscii = new Hashtable(); + private String pkgPrefix=""; + private int pkgPrefixLen=0; + + /** + * Read classfile to disassemble. + */ + public ClassData(InputStream infile) throws IOException { + this.read(new DataInputStream(infile)); + } + + /** + * Reads and stores class file information. + */ + public void read(DataInputStream in) throws IOException { + // Read the header + magic = in.readInt(); + if (magic != JAVA_MAGIC) { + throw new ClassFormatError("wrong magic: " + + toHex(magic) + ", expected " + + toHex(JAVA_MAGIC)); + } + minor_version = in.readShort(); + major_version = in.readShort(); + if (major_version != JAVA_VERSION) { + } + + // Read the constant pool + readCP(in); + access = in.readUnsignedShort(); + this_class = in.readUnsignedShort(); + super_class = in.readUnsignedShort(); + + //Read interfaces. + interfaces_count = in.readUnsignedShort(); + if(interfaces_count > 0){ + interfaces = new int[interfaces_count]; + } + for (int i = 0; i < interfaces_count; i++) { + interfaces[i]=in.readShort(); + } + + // Read the fields + readFields(in); + + // Read the methods + readMethods(in); + + // Read the attributes + attributes_count = in.readUnsignedShort(); + attrs=new AttrData[attributes_count]; + for (int k = 0; k < attributes_count; k++) { + int name_cpx=in.readUnsignedShort(); + if (getTag(name_cpx)==CONSTANT_UTF8 + && getString(name_cpx).equals("SourceFile") + ){ if (in.readInt()!=2) + throw new ClassFormatError("invalid attr length"); + source_cpx=in.readUnsignedShort(); + AttrData attr=new AttrData(this); + attr.read(name_cpx); + attrs[k]=attr; + + } else if (getTag(name_cpx)==CONSTANT_UTF8 + && getString(name_cpx).equals("InnerClasses") + ){ int length=in.readInt(); + int num=in.readUnsignedShort(); + if (2+num*8 != length) + throw new ClassFormatError("invalid attr length"); + innerClasses=new InnerClassData[num]; + for (int j = 0; j < num; j++) { + InnerClassData innerClass=new InnerClassData(this); + innerClass.read(in); + innerClasses[j]=innerClass; + } + AttrData attr=new AttrData(this); + attr.read(name_cpx); + attrs[k]=attr; + } else { + AttrData attr=new AttrData(this); + attr.read(name_cpx, in); + attrs[k]=attr; + } + } + in.close(); + } // end ClassData.read() + + /** + * Reads and stores constant pool info. + */ + void readCP(DataInputStream in) throws IOException { + cpool_count = in.readUnsignedShort(); + tags = new byte[cpool_count]; + cpool = new Object[cpool_count]; + for (int i = 1; i < cpool_count; i++) { + byte tag = in.readByte(); + + switch(tags[i] = tag) { + case CONSTANT_UTF8: + String str=in.readUTF(); + indexHashAscii.put(cpool[i] = str, new Integer(i)); + break; + case CONSTANT_INTEGER: + cpool[i] = new Integer(in.readInt()); + break; + case CONSTANT_FLOAT: + cpool[i] = new Float(in.readFloat()); + break; + case CONSTANT_LONG: + cpool[i++] = new Long(in.readLong()); + break; + case CONSTANT_DOUBLE: + cpool[i++] = new Double(in.readDouble()); + break; + case CONSTANT_CLASS: + case CONSTANT_STRING: + cpool[i] = new CPX(in.readUnsignedShort()); + break; + + case CONSTANT_FIELD: + case CONSTANT_METHOD: + case CONSTANT_INTERFACEMETHOD: + case CONSTANT_NAMEANDTYPE: + cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort()); + break; + + case 0: + default: + throw new ClassFormatError("invalid constant type: " + (int)tags[i]); + } + } + } + + /** + * Reads and strores field info. + */ + protected void readFields(DataInputStream in) throws IOException { + int fields_count = in.readUnsignedShort(); + fields=new FieldData[fields_count]; + for (int k = 0; k < fields_count; k++) { + FieldData field=new FieldData(this); + field.read(in); + fields[k]=field; + } + } + + /** + * Reads and strores Method info. + */ + protected void readMethods(DataInputStream in) throws IOException { + int methods_count = in.readUnsignedShort(); + methods=new MethodData[methods_count]; + for (int k = 0; k < methods_count ; k++) { + MethodData method=new MethodData(this); + method.read(in); + methods[k]=method; + } + } + + /** + * get a string + */ + public String getString(int n) { + if (n == 0) { + return null; + } else { + return (String)cpool[n]; + } + } + + /** + * get the type of constant given an index + */ + public byte getTag(int n) { + try{ + return tags[n]; + } catch (ArrayIndexOutOfBoundsException e) { + return (byte)100; + } + } + + static final String hexString="0123456789ABCDEF"; + + public static char hexTable[]=hexString.toCharArray(); + + static String toHex(long val, int width) { + StringBuffer s = new StringBuffer(); + for (int i=width-1; i>=0; i--) + s.append(hexTable[((int)(val>>(4*i)))&0xF]); + return "0x"+s.toString(); + } + + static String toHex(long val) { + int width; + for (width=16; width>0; width--) { + if ((val>>(width-1)*4)!=0) break; + } + return toHex(val, width); + } + + static String toHex(int val) { + int width; + for (width=8; width>0; width--) { + if ((val>>(width-1)*4)!=0) break; + } + return toHex(val, width); + } + + /** + * Returns the name of this class. + */ + public String getClassName() { + String res=null; + if (this_class==0) { + return res; + } + int tcpx; + try { + if (tags[this_class]!=CONSTANT_CLASS) { + return res; //" "; + } + tcpx=((CPX)cpool[this_class]).cpx; + } catch (ArrayIndexOutOfBoundsException e) { + return res; // "#"+cpx+"// invalid constant pool index"; + } catch (Throwable e) { + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; + } + + try { + return (String)(cpool[tcpx]); + } catch (ArrayIndexOutOfBoundsException e) { + return res; // "class #"+scpx+"// invalid constant pool index"; + } catch (ClassCastException e) { + return res; // "class #"+scpx+"// invalid constant pool reference"; + } catch (Throwable e) { + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; + } + + } + + /** + * Returns the name of class at perticular index. + */ + public String getClassName(int cpx) { + String res="#"+cpx; + if (cpx==0) { + return res; + } + int scpx; + try { + if (tags[cpx]!=CONSTANT_CLASS) { + return res; //" "; + } + scpx=((CPX)cpool[cpx]).cpx; + } catch (ArrayIndexOutOfBoundsException e) { + return res; // "#"+cpx+"// invalid constant pool index"; + } catch (Throwable e) { + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; + } + res="#"+scpx; + try { + return (String)(cpool[scpx]); + } catch (ArrayIndexOutOfBoundsException e) { + return res; // "class #"+scpx+"// invalid constant pool index"; + } catch (ClassCastException e) { + return res; // "class #"+scpx+"// invalid constant pool reference"; + } catch (Throwable e) { + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; + } + } + + /** + * Returns true if it is a class + */ + public boolean isClass() { + if((access & ACC_INTERFACE) == 0) return true; + return false; + } + + /** + * Returns true if it is a interface. + */ + public boolean isInterface(){ + if((access & ACC_INTERFACE) != 0) return true; + return false; + } + + /** + * Returns true if this member is public, false otherwise. + */ + public boolean isPublic(){ + return (access & ACC_PUBLIC) != 0; + } + + /** + * Returns the access of this class or interface. + */ + public String[] getAccess(){ + Vector v = new Vector(); + if ((access & ACC_PUBLIC) !=0) v.addElement("public"); + if ((access & ACC_FINAL) !=0) v.addElement("final"); + if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract"); + String[] accflags = new String[v.size()]; + v.copyInto(accflags); + return accflags; + } + + /** + * Returns list of innerclasses. + */ + public InnerClassData[] getInnerClasses(){ + return innerClasses; + } + + /** + * Returns list of attributes. + */ + final AttrData[] getAttributes(){ + return attrs; + } + + public byte[] findAnnotationData(boolean classRetention) { + String n = classRetention ? + "RuntimeInvisibleAnnotations" : // NOI18N + "RuntimeVisibleAnnotations"; // NOI18N + return findAttr(n, attrs); + } + + /** + * Returns true if superbit is set. + */ + public boolean isSuperSet(){ + if ((access & ACC_SUPER) !=0) return true; + return false; + } + + /** + * Returns super class name. + */ + public String getSuperClassName(){ + String res=null; + if (super_class==0) { + return res; + } + int scpx; + try { + if (tags[super_class]!=CONSTANT_CLASS) { + return res; //" "; + } + scpx=((CPX)cpool[super_class]).cpx; + } catch (ArrayIndexOutOfBoundsException e) { + return res; // "#"+cpx+"// invalid constant pool index"; + } catch (Throwable e) { + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; + } + + try { + return (String)(cpool[scpx]); + } catch (ArrayIndexOutOfBoundsException e) { + return res; // "class #"+scpx+"// invalid constant pool index"; + } catch (ClassCastException e) { + return res; // "class #"+scpx+"// invalid constant pool reference"; + } catch (Throwable e) { + return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; + } + } + + /** + * Returns list of super interfaces. + */ + public String[] getSuperInterfaces(){ + String interfacenames[] = new String[interfaces.length]; + int interfacecpx = -1; + for(int i = 0; i < interfaces.length; i++){ + interfacecpx=((CPX)cpool[interfaces[i]]).cpx; + interfacenames[i] = (String)(cpool[interfacecpx]); + } + return interfacenames; + } + + /** + * Returns string at prticular constant pool index. + */ + public String getStringValue(int cpoolx) { + try { + return ((String)cpool[cpoolx]); + } catch (ArrayIndexOutOfBoundsException e) { + return "//invalid constant pool index:"+cpoolx; + } catch (ClassCastException e) { + return "//invalid constant pool ref:"+cpoolx; + } + } + + /** + * Returns list of field info. + */ + public FieldData[] getFields(){ + return fields; + } + + /** + * Returns list of method info. + */ + public MethodData[] getMethods(){ + return methods; + } + + /** + * Returns constant pool entry at that index. + */ + public CPX2 getCpoolEntry(int cpx){ + return ((CPX2)(cpool[cpx])); + } + + public Object getCpoolEntryobj(int cpx){ + return (cpool[cpx]); + } + + /** + * Returns index of this class. + */ + public int getthis_cpx(){ + return this_class; + } + + public String TagString (int tag) { + String res=Tables.tagName(tag); + if (res==null) return "BOGUS_TAG:"+tag; + return res; + } + + /** + * Returns string at that index. + */ + public String StringValue(int cpx) { + return stringValue(cpx, false); + } + public String stringValue(int cpx, boolean textual) { + if (cpx==0) return "#0"; + int tag; + Object x; + String suffix=""; + try { + tag=tags[cpx]; + x=cpool[cpx]; + } catch (IndexOutOfBoundsException e) { + return ""; + } + + if (x==null) return ""; + switch (tag) { + case CONSTANT_UTF8: { + if (!textual) { + return (String)x; + } + StringBuilder sb=new StringBuilder(); + String s=(String)x; + for (int k=0; k"; + } catch (ClassCastException e) { + return ""; + } + } + + /** + * Returns unqualified class name. + */ + public String getShortClassName(int cpx) { + String classname=javaName(getClassName(cpx)); + pkgPrefixLen=classname.lastIndexOf("/")+1; + if (pkgPrefixLen!=0) { + pkgPrefix=classname.substring(0,pkgPrefixLen); + if (classname.startsWith(pkgPrefix)) { + return classname.substring(pkgPrefixLen); + } + } + return classname; + } + + /** + * Returns source file name. + */ + public String getSourceName(){ + return getName(source_cpx); + } + + /** + * Returns package name. + */ + public String getPkgName(){ + String classname=getClassName(this_class); + pkgPrefixLen=classname.lastIndexOf("/")+1; + if (pkgPrefixLen!=0) { + pkgPrefix=classname.substring(0,pkgPrefixLen); + return("package "+pkgPrefix.substring(0,pkgPrefixLen-1)+";\n"); + }else return null; + } + + /** + * Returns total constant pool entry count. + */ + public int getCpoolCount(){ + return cpool_count; + } + + public String StringTag(int cpx) { + byte tag=0; + String str=null; + try { + if (cpx==0) throw new IndexOutOfBoundsException(); + tag=tags[cpx]; + return TagString(tag); + } catch (IndexOutOfBoundsException e) { + str="Incorrect CP index:"+cpx; + } + return str; + } + + /** + * Returns minor version of class file. + */ + public int getMinor_version(){ + return minor_version; + } + + /** + * Returns major version of class file. + */ + public int getMajor_version(){ + return major_version; + } + + private boolean isJavaIdentifierStart(int cp) { + return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z'); + } + + private boolean isJavaIdentifierPart(int cp) { + return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9'); + } + + public String[] getNameAndType(int indx) { + return getNameAndType(indx, 0, new String[2]); + } + + private String[] getNameAndType(int indx, int at, String[] arr) { + CPX2 c2 = getCpoolEntry(indx); + arr[at] = StringValue(c2.cpx1); + arr[at + 1] = StringValue(c2.cpx2); + return arr; + } + + public String[] getFieldInfoName(int indx) { + CPX2 c2 = getCpoolEntry(indx); + String[] arr = new String[3]; + arr[0] = getClassName(c2.cpx1); + return getNameAndType(c2.cpx2, 1, arr); + } + + static byte[] findAttr(String n, AttrData[] attrs) { + for (AttrData ad : attrs) { + if (n.equals(ad.getAttrName())) { + return ad.getData(); + } + } + return null; + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/Constants.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/Constants.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,372 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +/** + * This interface defines constant that are used + * throughout the compiler. It inherits from RuntimeConstants, + * which is an autogenerated class that contains contstants + * defined in the interpreter. + */ + +public +interface Constants extends RuntimeConstants { + + /** + * End of input + */ + public static final int EOF = -1; + + /* + * Flags + */ + public static final int F_VERBOSE = 1 << 0; + public static final int F_DUMP = 1 << 1; + public static final int F_WARNINGS = 1 << 2; + public static final int F_DEBUG = 1 << 3; + public static final int F_OPTIMIZE = 1 << 4; + public static final int F_DEPENDENCIES = 1 << 5; + + /* + * Type codes + */ + public static final int TC_BOOLEAN = 0; + public static final int TC_BYTE = 1; + public static final int TC_CHAR = 2; + public static final int TC_SHORT = 3; + public static final int TC_INT = 4; + public static final int TC_LONG = 5; + public static final int TC_FLOAT = 6; + public static final int TC_DOUBLE = 7; + public static final int TC_NULL = 8; + public static final int TC_ARRAY = 9; + public static final int TC_CLASS = 10; + public static final int TC_VOID = 11; + public static final int TC_METHOD = 12; + public static final int TC_ERROR = 13; + + /* + * Type Masks + */ + public static final int TM_NULL = 1 << TC_NULL; + public static final int TM_VOID = 1 << TC_VOID; + public static final int TM_BOOLEAN = 1 << TC_BOOLEAN; + public static final int TM_BYTE = 1 << TC_BYTE; + public static final int TM_CHAR = 1 << TC_CHAR; + public static final int TM_SHORT = 1 << TC_SHORT; + public static final int TM_INT = 1 << TC_INT; + public static final int TM_LONG = 1 << TC_LONG; + public static final int TM_FLOAT = 1 << TC_FLOAT; + public static final int TM_DOUBLE = 1 << TC_DOUBLE; + public static final int TM_ARRAY = 1 << TC_ARRAY; + public static final int TM_CLASS = 1 << TC_CLASS; + public static final int TM_METHOD = 1 << TC_METHOD; + public static final int TM_ERROR = 1 << TC_ERROR; + + public static final int TM_INT32 = TM_BYTE | TM_SHORT | TM_CHAR | TM_INT; + public static final int TM_NUM32 = TM_INT32 | TM_FLOAT; + public static final int TM_NUM64 = TM_LONG | TM_DOUBLE; + public static final int TM_INTEGER = TM_INT32 | TM_LONG; + public static final int TM_REAL = TM_FLOAT | TM_DOUBLE; + public static final int TM_NUMBER = TM_INTEGER | TM_REAL; + public static final int TM_REFERENCE = TM_ARRAY | TM_CLASS | TM_NULL; + + /* + * Class status + */ + public static final int CS_UNDEFINED = 0; + public static final int CS_UNDECIDED = 1; + public static final int CS_BINARY = 2; + public static final int CS_SOURCE = 3; + public static final int CS_PARSED = 4; + public static final int CS_COMPILED = 5; + public static final int CS_NOTFOUND = 6; + + /* + * Attributes + */ + public static final int ATT_ALL = -1; + public static final int ATT_CODE = 1; + + /* + * Number of bits used in file offsets + */ + public static final int OFFSETBITS = 19; + public static final int MAXFILESIZE = (1 << OFFSETBITS) - 1; + public static final int MAXLINENUMBER = (1 << (32 - OFFSETBITS)) - 1; + + /* + * Operators + */ + public final int COMMA = 0; + public final int ASSIGN = 1; + + public final int ASGMUL = 2; + public final int ASGDIV = 3; + public final int ASGREM = 4; + public final int ASGADD = 5; + public final int ASGSUB = 6; + public final int ASGLSHIFT = 7; + public final int ASGRSHIFT = 8; + public final int ASGURSHIFT = 9; + public final int ASGBITAND = 10; + public final int ASGBITOR = 11; + public final int ASGBITXOR = 12; + + public final int COND = 13; + public final int OR = 14; + public final int AND = 15; + public final int BITOR = 16; + public final int BITXOR = 17; + public final int BITAND = 18; + public final int NE = 19; + public final int EQ = 20; + public final int GE = 21; + public final int GT = 22; + public final int LE = 23; + public final int LT = 24; + public final int INSTANCEOF = 25; + public final int LSHIFT = 26; + public final int RSHIFT = 27; + public final int URSHIFT = 28; + public final int ADD = 29; + public final int SUB = 30; + public final int DIV = 31; + public final int REM = 32; + public final int MUL = 33; + public final int CAST = 34; // (x)y + public final int POS = 35; // +x + public final int NEG = 36; // -x + public final int NOT = 37; + public final int BITNOT = 38; + public final int PREINC = 39; // ++x + public final int PREDEC = 40; // --x + public final int NEWARRAY = 41; + public final int NEWINSTANCE = 42; + public final int NEWFROMNAME = 43; + public final int POSTINC = 44; // x++ + public final int POSTDEC = 45; // x-- + public final int FIELD = 46; + public final int METHOD = 47; // x(y) + public final int ARRAYACCESS = 48; // x[y] + public final int NEW = 49; + public final int INC = 50; + public final int DEC = 51; + + public final int CONVERT = 55; // implicit conversion + public final int EXPR = 56; // (x) + public final int ARRAY = 57; // {x, y, ...} + public final int GOTO = 58; + + /* + * Value tokens + */ + public final int IDENT = 60; + public final int BOOLEANVAL = 61; + public final int BYTEVAL = 62; + public final int CHARVAL = 63; + public final int SHORTVAL = 64; + public final int INTVAL = 65; + public final int LONGVAL = 66; + public final int FLOATVAL = 67; + public final int DOUBLEVAL = 68; + public final int STRINGVAL = 69; + + /* + * Type keywords + */ + public final int BYTE = 70; + public final int CHAR = 71; + public final int SHORT = 72; + public final int INT = 73; + public final int LONG = 74; + public final int FLOAT = 75; + public final int DOUBLE = 76; + public final int VOID = 77; + public final int BOOLEAN = 78; + + /* + * Expression keywords + */ + public final int TRUE = 80; + public final int FALSE = 81; + public final int THIS = 82; + public final int SUPER = 83; + public final int NULL = 84; + + /* + * Statement keywords + */ + public final int IF = 90; + public final int ELSE = 91; + public final int FOR = 92; + public final int WHILE = 93; + public final int DO = 94; + public final int SWITCH = 95; + public final int CASE = 96; + public final int DEFAULT = 97; + public final int BREAK = 98; + public final int CONTINUE = 99; + public final int RETURN = 100; + public final int TRY = 101; + public final int CATCH = 102; + public final int FINALLY = 103; + public final int THROW = 104; + public final int STAT = 105; + public final int EXPRESSION = 106; + public final int DECLARATION = 107; + public final int VARDECLARATION = 108; + + /* + * Declaration keywords + */ + public final int IMPORT = 110; + public final int CLASS = 111; + public final int EXTENDS = 112; + public final int IMPLEMENTS = 113; + public final int INTERFACE = 114; + public final int PACKAGE = 115; + + /* + * Modifier keywords + */ + public final int PRIVATE = 120; + public final int PUBLIC = 121; + public final int PROTECTED = 122; + public final int CONST = 123; + public final int STATIC = 124; + public final int TRANSIENT = 125; + public final int SYNCHRONIZED = 126; + public final int NATIVE = 127; + public final int FINAL = 128; + public final int VOLATILE = 129; + public final int ABSTRACT = 130; + public final int STRICT = 165; + + /* + * Punctuation + */ + public final int SEMICOLON = 135; + public final int COLON = 136; + public final int QUESTIONMARK = 137; + public final int LBRACE = 138; + public final int RBRACE = 139; + public final int LPAREN = 140; + public final int RPAREN = 141; + public final int LSQBRACKET = 142; + public final int RSQBRACKET = 143; + public final int THROWS = 144; + + /* + * Special tokens + */ + public final int ERROR = 145; // an error + public final int COMMENT = 146; // not used anymore. + public final int TYPE = 147; + public final int LENGTH = 148; + public final int INLINERETURN = 149; + public final int INLINEMETHOD = 150; + public final int INLINENEWINSTANCE = 151; + + /* + * Added for jasm + */ + public final int METHODREF = 152; + public final int FIELDREF = 153; + public final int STACK = 154; + public final int LOCAL = 155; + public final int CPINDEX = 156; + public final int CPNAME = 157; + public final int SIGN = 158; + public final int BITS = 159; + public final int INF = 160; + public final int NAN = 161; + public final int INNERCLASS = 162; + public final int OF = 163; + public final int SYNTHETIC = 164; +// last used=165; + + /* + * Operator precedence + */ + public static final int opPrecedence[] = { + 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 12, 13, 14, 15, 16, 17, 18, + 18, 19, 19, 19, 19, 19, 20, 20, 20, 21, + 21, 22, 22, 22, 23, 24, 24, 24, 24, 24, + 24, 25, 25, 26, 26, 26, 26, 26, 26 + }; + + /* + * Operator names + */ + public static final String opNames[] = { + ",", "=", "*=", "/=", "%=", + "+=", "-=", "<<=", ">>=", "<<<=", + "&=", "|=", "^=", "?:", "||", + "&&", "|", "^", "&", "!=", + "==", ">=", ">", "<=", "<", + "instanceof", "<<", ">>", "<<<", "+", + "-", "/", "%", "*", "cast", + "+", "-", "!", "~", "++", + "--", "new", "new", "new", "++", + "--", "field", "method", "[]", "new", + "++", "--", null, null, null, + + "convert", "expr", "array", "goto", null, + + "Identifier", "Boolean", "Byte", "Char", "Short", + "Integer", "Long", "Float", "Double", "String", + + "byte", "char", "short", "int", "long", + "float", "double", "void", "boolean", null, + + "true", "false", "this", "super", "null", + null, null, null, null, null, + + "if", "else", "for", "while", "do", + "switch", "case", "default", "break", "continue", + "return", "try", "catch", "finally", "throw", + "stat", "expression", "declaration", "declaration", null, + + "import", "class", "extends", "implements", "interface", + "package", null, null, null, null, + + "private", "public", "protected", "const", "static", + "transient", "synchronized", "native", "final", "volatile", + "abstract", null, null, null, null, + + ";", ":", "?", "{", "}", + "(", ")", "[", "]", "throws", + "error", "comment", "type", "length", "inline-return", + "inline-method", "inline-new", + "method", "field", "stack", "locals", "CPINDEX", "CPName", "SIGN", + "bits", "INF", "NaN", "InnerClass", "of", "synthetic" + }; + +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/FieldData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/FieldData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +import java.util.*; +import java.io.*; + +/** + * Strores field data informastion. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ + +public class FieldData implements RuntimeConstants { + + ClassData cls; + int access; + int name_index; + int descriptor_index; + int attributes_count; + int value_cpx=0; + boolean isSynthetic=false; + boolean isDeprecated=false; + Vector attrs; + + public FieldData(ClassData cls){ + this.cls=cls; + } + + /** + * Read and store field info. + */ + public void read(DataInputStream in) throws IOException { + access = in.readUnsignedShort(); + name_index = in.readUnsignedShort(); + descriptor_index = in.readUnsignedShort(); + // Read the attributes + int attributes_count = in.readUnsignedShort(); + attrs=new Vector(attributes_count); + for (int i = 0; i < attributes_count; i++) { + int attr_name_index=in.readUnsignedShort(); + if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue; + String attr_name=cls.getString(attr_name_index); + if (attr_name.equals("ConstantValue")){ + if (in.readInt()!=2) + throw new ClassFormatError("invalid ConstantValue attr length"); + value_cpx=in.readUnsignedShort(); + AttrData attr=new AttrData(cls); + attr.read(attr_name_index); + attrs.addElement(attr); + } else if (attr_name.equals("Synthetic")){ + if (in.readInt()!=0) + throw new ClassFormatError("invalid Synthetic attr length"); + isSynthetic=true; + AttrData attr=new AttrData(cls); + attr.read(attr_name_index); + attrs.addElement(attr); + } else if (attr_name.equals("Deprecated")){ + if (in.readInt()!=0) + throw new ClassFormatError("invalid Synthetic attr length"); + isDeprecated = true; + AttrData attr=new AttrData(cls); + attr.read(attr_name_index); + attrs.addElement(attr); + } else { + AttrData attr=new AttrData(cls); + attr.read(attr_name_index, in); + attrs.addElement(attr); + } + } + + } // end read + + public boolean isStatic() { + return (access & ACC_STATIC) != 0; + } + + /** + * Returns access of a field. + */ + public String[] getAccess(){ + Vector v = new Vector(); + if ((access & ACC_PUBLIC) !=0) v.addElement("public"); + if ((access & ACC_PRIVATE) !=0) v.addElement("private"); + if ((access & ACC_PROTECTED) !=0) v.addElement("protected"); + if ((access & ACC_STATIC) !=0) v.addElement("static"); + if ((access & ACC_FINAL) !=0) v.addElement("final"); + if ((access & ACC_VOLATILE) !=0) v.addElement("volatile"); + if ((access & ACC_TRANSIENT) !=0) v.addElement("transient"); + String[] accflags = new String[v.size()]; + v.copyInto(accflags); + return accflags; + } + + /** + * Returns name of a field. + */ + public String getName(){ + return cls.getStringValue(name_index); + } + + /** + * Returns internal signature of a field + */ + public String getInternalSig(){ + return cls.getStringValue(descriptor_index); + } + + /** + * Returns java type signature of a field. + */ + public String getType(){ + return new TypeSignature(getInternalSig()).getFieldType(); + } + + /** + * Returns true if field is synthetic. + */ + public boolean isSynthetic(){ + return isSynthetic; + } + + /** + * Returns true if field is deprecated. + */ + public boolean isDeprecated(){ + return isDeprecated; + } + + /** + * Returns index of constant value in cpool. + */ + public int getConstantValueIndex(){ + return (value_cpx); + } + + /** + * Returns list of attributes of field. + */ + public Vector getAttributes(){ + return attrs; + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/Hashtable.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/Hashtable.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,81 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.apidesign.javap; + +/** A JavaScript optimized replacement for Hashtable. + * + * @author Jaroslav Tulach + */ +final class Hashtable { + private Object[] keys; + private Object[] values; + + Hashtable(int i) { + this(); + } + + Hashtable(int i, double d) { + this(); + } + + Hashtable() { + } + + synchronized void put(Object key, Object val) { + int[] where = { -1, -1 }; + Object found = get(key, where); + if (where[0] != -1) { + // key exists + values[where[0]] = val; + } else { + if (where[1] != -1) { + // null found + keys[where[1]] = key; + values[where[1]] = val; + } else { + if (keys == null) { + keys = new Object[11]; + values = new Object[11]; + keys[0] = key; + values[0] = val; + } else { + Object[] newKeys = new Object[keys.length * 2]; + Object[] newValues = new Object[values.length * 2]; + for (int i = 0; i < keys.length; i++) { + newKeys[i] = keys[i]; + newValues[i] = values[i]; + } + newKeys[keys.length] = key; + newValues[keys.length] = val; + keys = newKeys; + values = newValues; + } + } + } + } + + Object get(Object key) { + return get(key, null); + } + private synchronized Object get(Object key, int[] foundAndNull) { + if (keys == null) { + return null; + } + for (int i = 0; i < keys.length; i++) { + if (keys[i] == null) { + if (foundAndNull != null) { + foundAndNull[1] = i; + } + } else if (keys[i].equals(key)) { + if (foundAndNull != null) { + foundAndNull[0] = i; + } + return values[i]; + } + } + return null; + } + +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/InnerClassData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/InnerClassData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +import java.io.*; +import java.util.*; + +/** + * Strores InnerClass data informastion. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +class InnerClassData implements RuntimeConstants { + ClassData cls; + + + int inner_class_info_index + ,outer_class_info_index + ,inner_name_index + ,access + ; + + public InnerClassData(ClassData cls) { + this.cls=cls; + + } + + /** + * Read Innerclass attribute data. + */ + public void read(DataInputStream in) throws IOException { + inner_class_info_index = in.readUnsignedShort(); + outer_class_info_index = in.readUnsignedShort(); + inner_name_index = in.readUnsignedShort(); + access = in.readUnsignedShort(); + } // end read + + /** + * Returns the access of this class or interface. + */ + public String[] getAccess(){ + Vector v = new Vector(); + if ((access & ACC_PUBLIC) !=0) v.addElement("public"); + if ((access & ACC_FINAL) !=0) v.addElement("final"); + if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract"); + String[] accflags = new String[v.size()]; + v.copyInto(accflags); + return accflags; + } + +} // end InnerClassData diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/LineNumData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/LineNumData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +import java.util.*; +import java.io.*; + +/** + * Strores LineNumberTable data information. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +class LineNumData { + short start_pc, line_number; + + public LineNumData() {} + + /** + * Read LineNumberTable attribute. + */ + public LineNumData(DataInputStream in) throws IOException { + start_pc = in.readShort(); + line_number=in.readShort(); + + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/LocVarData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/LocVarData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +import java.util.*; +import java.io.*; + +/** + * Strores LocalVariableTable data information. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +class LocVarData { + short start_pc, length, name_cpx, sig_cpx, slot; + + public LocVarData() { + } + + /** + * Read LocalVariableTable attribute. + */ + public LocVarData(DataInputStream in) throws IOException { + start_pc = in.readShort(); + length=in.readShort(); + name_cpx=in.readShort(); + sig_cpx=in.readShort(); + slot=in.readShort(); + + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/MethodData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/MethodData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,425 @@ +/* + * Copyright (c) 2002, 2005, 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 org.apidesign.javap; + +import java.io.*; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +import static org.apidesign.javap.RuntimeConstants.*; + +/** + * Strores method data informastion. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +public class MethodData { + + ClassData cls; + int access; + int name_index; + int descriptor_index; + int attributes_count; + byte[] code; + Vector exception_table = new Vector(0); + Vector lin_num_tb = new Vector(0); + Vector loc_var_tb = new Vector(0); + StackMapTableData[] stackMapTable; + StackMapData[] stackMap; + int[] exc_index_table=null; + Vector attrs=new Vector(0); + Vector code_attrs=new Vector(0); + int max_stack, max_locals; + boolean isSynthetic=false; + boolean isDeprecated=false; + + public MethodData(ClassData cls){ + this.cls=cls; + } + + /** + * Read method info. + */ + public void read(DataInputStream in) throws IOException { + access = in.readUnsignedShort(); + name_index=in.readUnsignedShort(); + descriptor_index =in.readUnsignedShort(); + int attributes_count = in.readUnsignedShort(); + for (int i = 0; i < attributes_count; i++) { + int attr_name_index=in.readUnsignedShort(); + + readAttr: { + if (cls.getTag(attr_name_index)==CONSTANT_UTF8) { + String attr_name=cls.getString(attr_name_index); + if ( attr_name.equals("Code")){ + readCode (in); + AttrData attr=new AttrData(cls); + attr.read(attr_name_index); + attrs.addElement(attr); + break readAttr; + } else if ( attr_name.equals("Exceptions")){ + readExceptions(in); + AttrData attr=new AttrData(cls); + attr.read(attr_name_index); + attrs.addElement(attr); + break readAttr; + } else if (attr_name.equals("Synthetic")){ + if (in.readInt()!=0) + throw new ClassFormatError("invalid Synthetic attr length"); + isSynthetic=true; + AttrData attr=new AttrData(cls); + attr.read(attr_name_index); + attrs.addElement(attr); + break readAttr; + } else if (attr_name.equals("Deprecated")){ + if (in.readInt()!=0) + throw new ClassFormatError("invalid Synthetic attr length"); + isDeprecated = true; + AttrData attr=new AttrData(cls); + attr.read(attr_name_index); + attrs.addElement(attr); + break readAttr; + } + } + AttrData attr=new AttrData(cls); + attr.read(attr_name_index, in); + attrs.addElement(attr); + } + } + } + + /** + * Read code attribute info. + */ + public void readCode(DataInputStream in) throws IOException { + + int attr_length = in.readInt(); + max_stack=in.readUnsignedShort(); + max_locals=in.readUnsignedShort(); + int codelen=in.readInt(); + + code=new byte[codelen]; + int totalread = 0; + while(totalread < codelen){ + totalread += in.read(code, totalread, codelen-totalread); + } + // in.read(code, 0, codelen); + int clen = 0; + readExceptionTable(in); + int code_attributes_count = in.readUnsignedShort(); + + for (int k = 0 ; k < code_attributes_count ; k++) { + int table_name_index=in.readUnsignedShort(); + int table_name_tag=cls.getTag(table_name_index); + AttrData attr=new AttrData(cls); + if (table_name_tag==CONSTANT_UTF8) { + String table_name_tstr=cls.getString(table_name_index); + if (table_name_tstr.equals("LineNumberTable")) { + readLineNumTable(in); + attr.read(table_name_index); + } else if (table_name_tstr.equals("LocalVariableTable")) { + readLocVarTable(in); + attr.read(table_name_index); + } else if (table_name_tstr.equals("StackMapTable")) { + readStackMapTable(in); + attr.read(table_name_index); + } else if (table_name_tstr.equals("StackMap")) { + readStackMap(in); + attr.read(table_name_index); + } else { + attr.read(table_name_index, in); + } + code_attrs.addElement(attr); + continue; + } + + attr.read(table_name_index, in); + code_attrs.addElement(attr); + } + } + + /** + * Read exception table info. + */ + void readExceptionTable (DataInputStream in) throws IOException { + int exception_table_len=in.readUnsignedShort(); + exception_table=new Vector(exception_table_len); + for (int l = 0; l < exception_table_len; l++) { + exception_table.addElement(new TrapData(in, l)); + } + } + + /** + * Read LineNumberTable attribute info. + */ + void readLineNumTable (DataInputStream in) throws IOException { + int attr_len = in.readInt(); // attr_length + int lin_num_tb_len = in.readUnsignedShort(); + lin_num_tb=new Vector(lin_num_tb_len); + for (int l = 0; l < lin_num_tb_len; l++) { + lin_num_tb.addElement(new LineNumData(in)); + } + } + + /** + * Read LocalVariableTable attribute info. + */ + void readLocVarTable (DataInputStream in) throws IOException { + int attr_len=in.readInt(); // attr_length + int loc_var_tb_len = in.readUnsignedShort(); + loc_var_tb = new Vector(loc_var_tb_len); + for (int l = 0; l < loc_var_tb_len; l++) { + loc_var_tb.addElement(new LocVarData(in)); + } + } + + /** + * Read Exception attribute info. + */ + public void readExceptions(DataInputStream in) throws IOException { + int attr_len=in.readInt(); // attr_length in prog + int num_exceptions = in.readUnsignedShort(); + exc_index_table=new int[num_exceptions]; + for (int l = 0; l < num_exceptions; l++) { + int exc=in.readShort(); + exc_index_table[l]=exc; + } + } + + /** + * Read StackMapTable attribute info. + */ + void readStackMapTable(DataInputStream in) throws IOException { + int attr_len = in.readInt(); //attr_length + int stack_map_tb_len = in.readUnsignedShort(); + stackMapTable = new StackMapTableData[stack_map_tb_len]; + for (int i=0; i>8) { + case 0: + return opcLengthsTab[opc]; + case opc_wide: + switch (opc&0xFF) { + 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: + return 4; + case opc_iinc: + return 6; + default: + throw new ArrayIndexOutOfBoundsException(); + } + case opc_nonpriv: + case opc_priv: + return 2; + default: + throw new ArrayIndexOutOfBoundsException(); + } + } + + public static String opcName(int opc) { + try { + switch (opc>>8) { + case 0: + return opcNamesTab[opc]; + case opc_wide: { + String mnem=opcNamesTab[opc&0xFF]+"_w"; + if (mnemocodes.get(mnem) == null) + return null; // non-existent opcode + return mnem; + } + case opc_nonpriv: + return opcExtNamesTab[opc&0xFF]; + case opc_priv: + return opcPrivExtNamesTab[opc&0xFF]; + default: + return null; + } + } catch (ArrayIndexOutOfBoundsException e) { + switch (opc) { + case opc_nonpriv: + return "nonpriv"; + case opc_priv: + return "priv"; + default: + return null; + } + } + } + + public static int opcode(String mnem) { + Integer Val=(Integer)(mnemocodes.get(mnem)); + if (Val == null) return -1; + return Val.intValue(); + } + + /** + * Initialized keyword and token Hashtables + */ + static Vector keywordNames = new Vector(40); + private static void defineKeywordName(String id, int token) { + + if (token>=keywordNames.size()) { + keywordNames.setSize(token+1); + } + keywordNames.setElementAt(id, token); + } + public static String keywordName(int token) { + if (token==-1) return "EOF"; + if (token>=keywordNames.size()) return null; + return (String)keywordNames.elementAt(token); + } + static { + defineKeywordName("ident", IDENT); + defineKeywordName("STRINGVAL", STRINGVAL); + defineKeywordName("intVal", INTVAL); + defineKeywordName("longVal", LONGVAL); + defineKeywordName("floatVal", FLOATVAL); + defineKeywordName("doubleVal", DOUBLEVAL); + defineKeywordName("SEMICOLON", SEMICOLON); + defineKeywordName("COLON", COLON); + defineKeywordName("LBRACE", LBRACE); + defineKeywordName("RBRACE", RBRACE); + } + + static Hashtable keywords = new Hashtable(40); + public static int keyword(String idValue) { + Integer Val=(Integer)(keywords.get(idValue)); + if (Val == null) return IDENT; + return Val.intValue(); + } + + private static void defineKeyword(String id, int token) { + keywords.put(id, new Integer(token)); + defineKeywordName(id, token); + } + static { + // Modifier keywords + defineKeyword("private", PRIVATE); + defineKeyword("public", PUBLIC); + defineKeyword("protected", PROTECTED); + defineKeyword("static", STATIC); + defineKeyword("transient", TRANSIENT); + defineKeyword("synchronized", SYNCHRONIZED); + defineKeyword("super", SUPER); + defineKeyword("native", NATIVE); + defineKeyword("abstract", ABSTRACT); + defineKeyword("volatile", VOLATILE); + defineKeyword("final", FINAL); + defineKeyword("interface",INTERFACE); + defineKeyword("synthetic",SYNTHETIC); + defineKeyword("strict",STRICT); + + // Declaration keywords + defineKeyword("package",PACKAGE); + defineKeyword("class",CLASS); + defineKeyword("extends",EXTENDS); + defineKeyword("implements",IMPLEMENTS); + defineKeyword("const", CONST); + defineKeyword("throws",THROWS); + defineKeyword("interface",INTERFACE); + defineKeyword("Method",METHODREF); + defineKeyword("Field",FIELDREF); + defineKeyword("stack",STACK); + defineKeyword("locals",LOCAL); + + // used in switchtables + defineKeyword("default", DEFAULT); + + // used in inner class declarations + defineKeyword("InnerClass", INNERCLASS); + defineKeyword("of", OF); + + // misc + defineKeyword("bits",BITS); + defineKeyword("Infinity",INF); + defineKeyword("Inf",INF); + defineKeyword("NaN",NAN); + } + + /** + * Define tag table. + */ + private static Vector tagNames = new Vector(10); + private static Hashtable Tags = new Hashtable(10); + static { + defineTag("Asciz",CONSTANT_UTF8); + defineTag("int",CONSTANT_INTEGER); + defineTag("float",CONSTANT_FLOAT); + defineTag("long",CONSTANT_LONG); + defineTag("double",CONSTANT_DOUBLE); + defineTag("class",CONSTANT_CLASS); + defineTag("String",CONSTANT_STRING); + defineTag("Field",CONSTANT_FIELD); + defineTag("Method",CONSTANT_METHOD); + defineTag("InterfaceMethod",CONSTANT_INTERFACEMETHOD); + defineTag("NameAndType",CONSTANT_NAMEANDTYPE); + } + private static void defineTag(String id, int val) { + Tags.put(id, new Integer(val)); + if (val>=tagNames.size()) { + tagNames.setSize(val+1); + } + tagNames.setElementAt(id, val); + } + public static String tagName(int tag) { + if (tag>=tagNames.size()) return null; + return (String)tagNames.elementAt(tag); + } + public static int tagValue(String idValue) { + Integer Val=(Integer)(Tags.get(idValue)); + if (Val == null) return 0; + return Val.intValue(); + } + + /** + * Define type table. These types used in "newarray" instruction only. + */ + private static Vector typeNames = new Vector(10); + private static Hashtable Types = new Hashtable(10); + static { + defineType("int",T_INT); + defineType("long",T_LONG); + defineType("float",T_FLOAT); + defineType("double",T_DOUBLE); + defineType("class",T_CLASS); + defineType("boolean",T_BOOLEAN); + defineType("char",T_CHAR); + defineType("byte",T_BYTE); + defineType("short",T_SHORT); + } + private static void defineType(String id, int val) { + Types.put(id, new Integer(val)); + if (val>=typeNames.size()) { + typeNames.setSize(val+1); + } + typeNames.setElementAt(id, val); + } + public static int typeValue(String idValue) { + Integer Val=(Integer)(Types.get(idValue)); + if (Val == null) return -1; + return Val.intValue(); + } + public static String typeName(int type) { + if (type>=typeNames.size()) return null; + return (String)typeNames.elementAt(type); + } + + /** + * Define MapTypes table. + * These constants used in stackmap tables only. + */ + private static Vector mapTypeNames = new Vector(10); + private static Hashtable MapTypes = new Hashtable(10); + static { + defineMapType("bogus", ITEM_Bogus); + defineMapType("int", ITEM_Integer); + defineMapType("float", ITEM_Float); + defineMapType("double", ITEM_Double); + defineMapType("long", ITEM_Long); + defineMapType("null", ITEM_Null); + defineMapType("this", ITEM_InitObject); + defineMapType("CP", ITEM_Object); + defineMapType("uninitialized", ITEM_NewObject); + } + private static void defineMapType(String id, int val) { + MapTypes.put(id, new Integer(val)); + if (val>=mapTypeNames.size()) { + mapTypeNames.setSize(val+1); + } + mapTypeNames.setElementAt(id, val); + } + public static int mapTypeValue(String idValue) { + Integer Val=(Integer)(MapTypes.get(idValue)); + if (Val == null) return -1; + return Val.intValue(); + } + public static String mapTypeName(int type) { + if (type>=mapTypeNames.size()) return null; + return (String)mapTypeNames.elementAt(type); + } + +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/TrapData.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/TrapData.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2002, 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 org.apidesign.javap; + +import java.util.*; +import java.io.*; + +/** + * Stores exception table data in code attribute. + * + * @author Sucheta Dambalkar (Adopted code from jdis) + */ +class TrapData { + short start_pc, end_pc, handler_pc, catch_cpx; + int num; + + + /** + * Read and store exception table data in code attribute. + */ + public TrapData(DataInputStream in, int num) throws IOException { + this.num=num; + start_pc = in.readShort(); + end_pc=in.readShort(); + handler_pc=in.readShort(); + catch_cpx=in.readShort(); + } + + /** + * returns recommended identifier + */ + public String ident() { + return "t"+num; + } + +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/TypeSignature.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/TypeSignature.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,295 @@ +/* + * 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 org.apidesign.javap; + +import java.util.*; +import java.io.*; + +/** + * Returns java type signature. + * + * @author Sucheta Dambalkar + */ +public class TypeSignature { + + String parameters = null; + String returntype = null; + String fieldtype = null; + int argumentlength = 0; + + public TypeSignature(String JVMSignature){ + + if(JVMSignature != null){ + if(JVMSignature.indexOf("(") == -1){ + //This is a field type. + this.fieldtype = getFieldTypeSignature(JVMSignature); + }else { + String parameterdes = null; + if((JVMSignature.indexOf(")")-1) > (JVMSignature.indexOf("("))){ + //Get parameter signature. + parameterdes = + JVMSignature.substring(JVMSignature.indexOf("(")+1, + JVMSignature.indexOf(")")); + this.parameters = getParametersHelper(parameterdes); + }else this.parameters = "()"; + //Get return type signature. + String returndes = JVMSignature.substring(JVMSignature.lastIndexOf(")")+1); + this.returntype = getReturnTypeHelper(returndes); + } + } + } + + /** + * Returns java type signature of a field. + */ + public String getFieldTypeSignature(String fielddes){ + if(fielddes.startsWith("L")){ + return(getObjectType(fielddes)); + }else if(fielddes.startsWith("[")){ + return(getArrayType(fielddes)); + }else + return(getBaseType(fielddes)); + } + + /** + * Returns java type signature of a parameter. + */ + public String getParametersHelper(String parameterdes){ + Vector parameters = new Vector(); + int startindex = -1; + int endindex = -1; + String param = ""; + + while(parameterdes != null){ + + if(parameterdes.startsWith("L")){ + //parameter is a object. + startindex = parameterdes.indexOf("L"); + endindex = parameterdes.indexOf(";"); + if(startindex < parameterdes.length()) { + if(endindex == parameterdes.length()-1) { + //last parameter + param = parameterdes.substring(startindex); + parameterdes = null; + }else if(endindex+1 < parameterdes.length()){ + //rest parameters + param = parameterdes.substring(startindex, endindex+1); + parameterdes = parameterdes.substring(endindex+1); + + } + parameters.add(getObjectType(param)); + } + }else if(parameterdes.startsWith("[")){ + //parameter is an array. + String componentType = ""; + int enddim = -1; + int st = 0; + while(true){ + if(st < parameterdes.length()){ + if(parameterdes.charAt(st) == '['){ + + enddim = st; + st++; + } + else break; + } + else break; + } + + if(enddim+1 < parameterdes.length()){ + /* Array dimension.*/ + param = parameterdes.substring(0,enddim+1); + + } + + int stotherparam = param.lastIndexOf("[")+1; + + if(stotherparam < parameterdes.length()){ + componentType = parameterdes.substring(stotherparam); + } + + if(componentType.startsWith("L")){ + //parameter is array of objects. + startindex = parameterdes.indexOf("L"); + endindex = parameterdes.indexOf(";"); + + if(endindex == parameterdes.length()-1){ + //last parameter + param += parameterdes.substring(startindex); + parameterdes = null; + }else if(endindex+1 < parameterdes.length()){ + //rest parameters + param += parameterdes.substring(startindex, endindex+1); + parameterdes = parameterdes.substring(endindex+1); + } + }else{ + //parameter is array of base type. + if(componentType.length() == 1){ + //last parameter. + param += componentType; + parameterdes = null; + } + else if (componentType.length() > 1) { + //rest parameters. + param += componentType.substring(0,1); + parameterdes = componentType.substring(1); + } + } + parameters.add(getArrayType(param)); + + + }else { + + //parameter is of base type. + if(parameterdes.length() == 1){ + //last parameter + param = parameterdes; + parameterdes = null; + } + else if (parameterdes.length() > 1) { + //rest parameters. + param = parameterdes.substring(0,1); + parameterdes = parameterdes.substring(1); + } + parameters.add(getBaseType(param)); + } + } + + /* number of arguments of a method.*/ + argumentlength = parameters.size(); + + /* java type signature.*/ + String parametersignature = "("; + int i; + + for(i = 0; i < parameters.size(); i++){ + parametersignature += (String)parameters.elementAt(i); + if(i != parameters.size()-1){ + parametersignature += ", "; + } + } + parametersignature += ")"; + return parametersignature; + } + + /** + * Returns java type signature for a return type. + */ + public String getReturnTypeHelper(String returndes){ + return getFieldTypeSignature(returndes); + } + + /** + * Returns java type signature for a base type. + */ + public String getBaseType(String baseType){ + if(baseType != null){ + if(baseType.equals("B")) return "byte"; + else if(baseType.equals("C")) return "char"; + else if(baseType.equals("D")) return "double"; + else if(baseType.equals("F")) return "float"; + else if(baseType.equals("I")) return "int"; + else if(baseType.equals("J")) return "long"; + else if(baseType.equals("S")) return "short"; + else if(baseType.equals("Z")) return "boolean"; + else if(baseType.equals("V")) return "void"; + } + return null; + } + + /** + * Returns java type signature for a object type. + */ + public String getObjectType(String JVMobjectType) { + String objectType = ""; + int startindex = JVMobjectType.indexOf("L")+1; + int endindex = JVMobjectType.indexOf(";"); + if((startindex != -1) && (endindex != -1)){ + if((startindex < JVMobjectType.length()) && (endindex < JVMobjectType.length())){ + objectType = JVMobjectType.substring(startindex, endindex); + } + objectType = objectType.replace('/','.'); + return objectType; + } + return null; + } + + /** + * Returns java type signature for array type. + */ + public String getArrayType(String arrayType) { + if(arrayType != null){ + String dimention = ""; + + while(arrayType.indexOf("[") != -1){ + dimention += "[]"; + + int startindex = arrayType.indexOf("[")+1; + if(startindex <= arrayType.length()){ + arrayType = arrayType.substring(startindex); + } + } + + String componentType = ""; + if(arrayType.startsWith("L")){ + componentType = getObjectType(arrayType); + }else { + componentType = getBaseType(arrayType); + } + return componentType+dimention; + } + return null; + } + + /** + * Returns java type signature for parameters. + */ + public String getParameters(){ + return parameters; + } + + /** + * Returns java type signature for return type. + */ + public String getReturnType(){ + return returntype; + } + + /** + * Returns java type signature for field type. + */ + public String getFieldType(){ + return fieldtype; + } + + /** + * Return number of arguments of a method. + */ + public int getArgumentlength(){ + return argumentlength; + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/org/apidesign/javap/Vector.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javap/src/main/java/org/apidesign/javap/Vector.java Fri Nov 16 08:08:36 2012 +0100 @@ -0,0 +1,57 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.apidesign.javap; + +/** A JavaScript ready replacement for java.util.Vector + * + * @author Jaroslav Tulach + */ +final class Vector { + private Object[] arr; + + Vector() { + } + + Vector(int i) { + this(); + } + + void add(Object objectType) { + addElement(objectType); + } + void addElement(Object obj) { + final int s = size(); + setSize(s + 1); + setElementAt(obj, s); + } + + int size() { + return arr == null ? 0 : arr.length; + } + + void copyInto(Object[] newArr) { + if (arr == null) { + return; + } + int min = Math.min(newArr.length, arr.length); + for (int i = 0; i < min; i++) { + newArr[i] = arr[i]; + } + } + + Object elementAt(int index) { + return arr[index]; + } + + void setSize(int len) { + Object[] newArr = new Object[len]; + copyInto(newArr); + arr = newArr; + } + + void setElementAt(Object val, int index) { + arr[index] = val; + } +} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/AnnotationParser.java --- a/javap/src/main/java/sun/tools/javap/AnnotationParser.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2002, 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.io.ByteArrayInputStream; -import java.io.DataInputStream; -import java.io.IOException; - -/** An abstract parser for annotation definitions. Analyses the bytes and - * performs some callbacks to the overriden parser methods. - * - * @author Jaroslav Tulach - */ -public class AnnotationParser { - protected AnnotationParser() { - } - - protected void visitAttr(String type, String attr, String value) { - } - - /** Initialize the parsing with constant pool from cd. - * - * @param attr the attribute defining annotations - * @param cd constant pool - * @throws IOException in case I/O fails - */ - public final void parse(byte[] attr, ClassData cd) throws IOException { - ByteArrayInputStream is = new ByteArrayInputStream(attr); - DataInputStream dis = new DataInputStream(is); - try { - read(dis, cd); - } finally { - is.close(); - } - } - - private void read(DataInputStream dis, ClassData cd) throws IOException { - int cnt = dis.readUnsignedShort(); - for (int i = 0; i < cnt; i++) { - readAnno(dis, cd); - } - } - - private void readAnno(DataInputStream dis, ClassData cd) throws IOException { - int type = dis.readUnsignedShort(); - String typeName = cd.StringValue(type); - int cnt = dis.readUnsignedShort(); - for (int i = 0; i < cnt; i++) { - String attrName = cd.StringValue(dis.readUnsignedShort()); - readValue(dis, cd, typeName, attrName); - } - } - - private void readValue(DataInputStream dis, ClassData cd, String typeName, String attrName) - throws IOException { - char type = (char)dis.readByte(); - if (type == '@') { - readAnno(dis, cd); - } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N - int primitive = dis.readUnsignedShort(); - visitAttr(typeName, attrName, cd.StringValue(primitive)); - } else if (type == 'c') { - int cls = dis.readUnsignedShort(); - } else if (type == '[') { - int cnt = dis.readUnsignedShort(); - for (int i = 0; i < cnt; i++) { - readValue(dis, cd, typeName, attrName); - } - } else if (type == 'e') { - int enumT = dis.readUnsignedShort(); - int enumN = dis.readUnsignedShort(); - } else { - throw new IOException("Unknown type " + type); - } - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/AttrData.java --- a/javap/src/main/java/sun/tools/javap/AttrData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2002, 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.io.*; - -/** - * Reads and stores attribute information. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -class AttrData { - ClassData cls; - int name_cpx; - int datalen; - byte data[]; - - public AttrData (ClassData cls) { - this.cls=cls; - } - - /** - * Reads unknown attribute. - */ - public void read(int name_cpx, DataInputStream in) throws IOException { - this.name_cpx=name_cpx; - datalen=in.readInt(); - data=new byte[datalen]; - in.readFully(data); - } - - /** - * Reads just the name of known attribute. - */ - public void read(int name_cpx){ - this.name_cpx=name_cpx; - } - - /** - * Returns attribute name. - */ - public String getAttrName(){ - return cls.getString(name_cpx); - } - - /** - * Returns attribute data. - */ - public byte[] getData(){ - return data; - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/CPX.java --- a/javap/src/main/java/sun/tools/javap/CPX.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2002, 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; - -/** - * Stores constant pool entry information with one field. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -class CPX { - int cpx; - - CPX (int cpx) { - this.cpx=cpx; - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/CPX2.java --- a/javap/src/main/java/sun/tools/javap/CPX2.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2002, 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; - -/** - * Stores constant pool entry information with two fields. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -class CPX2 { - int cpx1,cpx2; - - CPX2 (int cpx1, int cpx2) { - this.cpx1=cpx1; - this.cpx2=cpx2; - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/ClassData.java --- a/javap/src/main/java/sun/tools/javap/ClassData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,718 +0,0 @@ -/* - * Copyright (c) 2002, 2004, 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.io.*; - -/** - * Central data repository of the Java Disassembler. - * Stores all the information in java class file. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -public final class ClassData implements RuntimeConstants { - - private int magic; - private int minor_version; - private int major_version; - private int cpool_count; - private Object cpool[]; - private int access; - private int this_class = 0;; - private int super_class; - private int interfaces_count; - private int[] interfaces = new int[0];; - private int fields_count; - private FieldData[] fields; - private int methods_count; - private MethodData[] methods; - private InnerClassData[] innerClasses; - private int attributes_count; - private AttrData[] attrs; - private String classname; - private String superclassname; - private int source_cpx=0; - private byte tags[]; - private Hashtable indexHashAscii = new Hashtable(); - private String pkgPrefix=""; - private int pkgPrefixLen=0; - - /** - * Read classfile to disassemble. - */ - public ClassData(InputStream infile) throws IOException { - this.read(new DataInputStream(infile)); - } - - /** - * Reads and stores class file information. - */ - public void read(DataInputStream in) throws IOException { - // Read the header - magic = in.readInt(); - if (magic != JAVA_MAGIC) { - throw new ClassFormatError("wrong magic: " + - toHex(magic) + ", expected " + - toHex(JAVA_MAGIC)); - } - minor_version = in.readShort(); - major_version = in.readShort(); - if (major_version != JAVA_VERSION) { - } - - // Read the constant pool - readCP(in); - access = in.readUnsignedShort(); - this_class = in.readUnsignedShort(); - super_class = in.readUnsignedShort(); - - //Read interfaces. - interfaces_count = in.readUnsignedShort(); - if(interfaces_count > 0){ - interfaces = new int[interfaces_count]; - } - for (int i = 0; i < interfaces_count; i++) { - interfaces[i]=in.readShort(); - } - - // Read the fields - readFields(in); - - // Read the methods - readMethods(in); - - // Read the attributes - attributes_count = in.readUnsignedShort(); - attrs=new AttrData[attributes_count]; - for (int k = 0; k < attributes_count; k++) { - int name_cpx=in.readUnsignedShort(); - if (getTag(name_cpx)==CONSTANT_UTF8 - && getString(name_cpx).equals("SourceFile") - ){ if (in.readInt()!=2) - throw new ClassFormatError("invalid attr length"); - source_cpx=in.readUnsignedShort(); - AttrData attr=new AttrData(this); - attr.read(name_cpx); - attrs[k]=attr; - - } else if (getTag(name_cpx)==CONSTANT_UTF8 - && getString(name_cpx).equals("InnerClasses") - ){ int length=in.readInt(); - int num=in.readUnsignedShort(); - if (2+num*8 != length) - throw new ClassFormatError("invalid attr length"); - innerClasses=new InnerClassData[num]; - for (int j = 0; j < num; j++) { - InnerClassData innerClass=new InnerClassData(this); - innerClass.read(in); - innerClasses[j]=innerClass; - } - AttrData attr=new AttrData(this); - attr.read(name_cpx); - attrs[k]=attr; - } else { - AttrData attr=new AttrData(this); - attr.read(name_cpx, in); - attrs[k]=attr; - } - } - in.close(); - } // end ClassData.read() - - /** - * Reads and stores constant pool info. - */ - void readCP(DataInputStream in) throws IOException { - cpool_count = in.readUnsignedShort(); - tags = new byte[cpool_count]; - cpool = new Object[cpool_count]; - for (int i = 1; i < cpool_count; i++) { - byte tag = in.readByte(); - - switch(tags[i] = tag) { - case CONSTANT_UTF8: - String str=in.readUTF(); - indexHashAscii.put(cpool[i] = str, new Integer(i)); - break; - case CONSTANT_INTEGER: - cpool[i] = new Integer(in.readInt()); - break; - case CONSTANT_FLOAT: - cpool[i] = new Float(in.readFloat()); - break; - case CONSTANT_LONG: - cpool[i++] = new Long(in.readLong()); - break; - case CONSTANT_DOUBLE: - cpool[i++] = new Double(in.readDouble()); - break; - case CONSTANT_CLASS: - case CONSTANT_STRING: - cpool[i] = new CPX(in.readUnsignedShort()); - break; - - case CONSTANT_FIELD: - case CONSTANT_METHOD: - case CONSTANT_INTERFACEMETHOD: - case CONSTANT_NAMEANDTYPE: - cpool[i] = new CPX2(in.readUnsignedShort(), in.readUnsignedShort()); - break; - - case 0: - default: - throw new ClassFormatError("invalid constant type: " + (int)tags[i]); - } - } - } - - /** - * Reads and strores field info. - */ - protected void readFields(DataInputStream in) throws IOException { - int fields_count = in.readUnsignedShort(); - fields=new FieldData[fields_count]; - for (int k = 0; k < fields_count; k++) { - FieldData field=new FieldData(this); - field.read(in); - fields[k]=field; - } - } - - /** - * Reads and strores Method info. - */ - protected void readMethods(DataInputStream in) throws IOException { - int methods_count = in.readUnsignedShort(); - methods=new MethodData[methods_count]; - for (int k = 0; k < methods_count ; k++) { - MethodData method=new MethodData(this); - method.read(in); - methods[k]=method; - } - } - - /** - * get a string - */ - public String getString(int n) { - if (n == 0) { - return null; - } else { - return (String)cpool[n]; - } - } - - /** - * get the type of constant given an index - */ - public byte getTag(int n) { - try{ - return tags[n]; - } catch (ArrayIndexOutOfBoundsException e) { - return (byte)100; - } - } - - static final String hexString="0123456789ABCDEF"; - - public static char hexTable[]=hexString.toCharArray(); - - static String toHex(long val, int width) { - StringBuffer s = new StringBuffer(); - for (int i=width-1; i>=0; i--) - s.append(hexTable[((int)(val>>(4*i)))&0xF]); - return "0x"+s.toString(); - } - - static String toHex(long val) { - int width; - for (width=16; width>0; width--) { - if ((val>>(width-1)*4)!=0) break; - } - return toHex(val, width); - } - - static String toHex(int val) { - int width; - for (width=8; width>0; width--) { - if ((val>>(width-1)*4)!=0) break; - } - return toHex(val, width); - } - - /** - * Returns the name of this class. - */ - public String getClassName() { - String res=null; - if (this_class==0) { - return res; - } - int tcpx; - try { - if (tags[this_class]!=CONSTANT_CLASS) { - return res; //" "; - } - tcpx=((CPX)cpool[this_class]).cpx; - } catch (ArrayIndexOutOfBoundsException e) { - return res; // "#"+cpx+"// invalid constant pool index"; - } catch (Throwable e) { - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; - } - - try { - return (String)(cpool[tcpx]); - } catch (ArrayIndexOutOfBoundsException e) { - return res; // "class #"+scpx+"// invalid constant pool index"; - } catch (ClassCastException e) { - return res; // "class #"+scpx+"// invalid constant pool reference"; - } catch (Throwable e) { - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; - } - - } - - /** - * Returns the name of class at perticular index. - */ - public String getClassName(int cpx) { - String res="#"+cpx; - if (cpx==0) { - return res; - } - int scpx; - try { - if (tags[cpx]!=CONSTANT_CLASS) { - return res; //" "; - } - scpx=((CPX)cpool[cpx]).cpx; - } catch (ArrayIndexOutOfBoundsException e) { - return res; // "#"+cpx+"// invalid constant pool index"; - } catch (Throwable e) { - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; - } - res="#"+scpx; - try { - return (String)(cpool[scpx]); - } catch (ArrayIndexOutOfBoundsException e) { - return res; // "class #"+scpx+"// invalid constant pool index"; - } catch (ClassCastException e) { - return res; // "class #"+scpx+"// invalid constant pool reference"; - } catch (Throwable e) { - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; - } - } - - /** - * Returns true if it is a class - */ - public boolean isClass() { - if((access & ACC_INTERFACE) == 0) return true; - return false; - } - - /** - * Returns true if it is a interface. - */ - public boolean isInterface(){ - if((access & ACC_INTERFACE) != 0) return true; - return false; - } - - /** - * Returns true if this member is public, false otherwise. - */ - public boolean isPublic(){ - return (access & ACC_PUBLIC) != 0; - } - - /** - * Returns the access of this class or interface. - */ - public String[] getAccess(){ - Vector v = new Vector(); - if ((access & ACC_PUBLIC) !=0) v.addElement("public"); - if ((access & ACC_FINAL) !=0) v.addElement("final"); - if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract"); - String[] accflags = new String[v.size()]; - v.copyInto(accflags); - return accflags; - } - - /** - * Returns list of innerclasses. - */ - public InnerClassData[] getInnerClasses(){ - return innerClasses; - } - - /** - * Returns list of attributes. - */ - final AttrData[] getAttributes(){ - return attrs; - } - - public byte[] findAnnotationData(boolean classRetention) { - String n = classRetention ? - "RuntimeInvisibleAnnotations" : // NOI18N - "RuntimeVisibleAnnotations"; // NOI18N - return findAttr(n, attrs); - } - - /** - * Returns true if superbit is set. - */ - public boolean isSuperSet(){ - if ((access & ACC_SUPER) !=0) return true; - return false; - } - - /** - * Returns super class name. - */ - public String getSuperClassName(){ - String res=null; - if (super_class==0) { - return res; - } - int scpx; - try { - if (tags[super_class]!=CONSTANT_CLASS) { - return res; //" "; - } - scpx=((CPX)cpool[super_class]).cpx; - } catch (ArrayIndexOutOfBoundsException e) { - return res; // "#"+cpx+"// invalid constant pool index"; - } catch (Throwable e) { - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; - } - - try { - return (String)(cpool[scpx]); - } catch (ArrayIndexOutOfBoundsException e) { - return res; // "class #"+scpx+"// invalid constant pool index"; - } catch (ClassCastException e) { - return res; // "class #"+scpx+"// invalid constant pool reference"; - } catch (Throwable e) { - return res; // "#"+cpx+"// ERROR IN DISASSEMBLER"; - } - } - - /** - * Returns list of super interfaces. - */ - public String[] getSuperInterfaces(){ - String interfacenames[] = new String[interfaces.length]; - int interfacecpx = -1; - for(int i = 0; i < interfaces.length; i++){ - interfacecpx=((CPX)cpool[interfaces[i]]).cpx; - interfacenames[i] = (String)(cpool[interfacecpx]); - } - return interfacenames; - } - - /** - * Returns string at prticular constant pool index. - */ - public String getStringValue(int cpoolx) { - try { - return ((String)cpool[cpoolx]); - } catch (ArrayIndexOutOfBoundsException e) { - return "//invalid constant pool index:"+cpoolx; - } catch (ClassCastException e) { - return "//invalid constant pool ref:"+cpoolx; - } - } - - /** - * Returns list of field info. - */ - public FieldData[] getFields(){ - return fields; - } - - /** - * Returns list of method info. - */ - public MethodData[] getMethods(){ - return methods; - } - - /** - * Returns constant pool entry at that index. - */ - public CPX2 getCpoolEntry(int cpx){ - return ((CPX2)(cpool[cpx])); - } - - public Object getCpoolEntryobj(int cpx){ - return (cpool[cpx]); - } - - /** - * Returns index of this class. - */ - public int getthis_cpx(){ - return this_class; - } - - public String TagString (int tag) { - String res=Tables.tagName(tag); - if (res==null) return "BOGUS_TAG:"+tag; - return res; - } - - /** - * Returns string at that index. - */ - public String StringValue(int cpx) { - return stringValue(cpx, false); - } - public String stringValue(int cpx, boolean textual) { - if (cpx==0) return "#0"; - int tag; - Object x; - String suffix=""; - try { - tag=tags[cpx]; - x=cpool[cpx]; - } catch (IndexOutOfBoundsException e) { - return ""; - } - - if (x==null) return ""; - switch (tag) { - case CONSTANT_UTF8: { - if (!textual) { - return (String)x; - } - StringBuilder sb=new StringBuilder(); - String s=(String)x; - for (int k=0; k"; - } catch (ClassCastException e) { - return ""; - } - } - - /** - * Returns unqualified class name. - */ - public String getShortClassName(int cpx) { - String classname=javaName(getClassName(cpx)); - pkgPrefixLen=classname.lastIndexOf("/")+1; - if (pkgPrefixLen!=0) { - pkgPrefix=classname.substring(0,pkgPrefixLen); - if (classname.startsWith(pkgPrefix)) { - return classname.substring(pkgPrefixLen); - } - } - return classname; - } - - /** - * Returns source file name. - */ - public String getSourceName(){ - return getName(source_cpx); - } - - /** - * Returns package name. - */ - public String getPkgName(){ - String classname=getClassName(this_class); - pkgPrefixLen=classname.lastIndexOf("/")+1; - if (pkgPrefixLen!=0) { - pkgPrefix=classname.substring(0,pkgPrefixLen); - return("package "+pkgPrefix.substring(0,pkgPrefixLen-1)+";\n"); - }else return null; - } - - /** - * Returns total constant pool entry count. - */ - public int getCpoolCount(){ - return cpool_count; - } - - public String StringTag(int cpx) { - byte tag=0; - String str=null; - try { - if (cpx==0) throw new IndexOutOfBoundsException(); - tag=tags[cpx]; - return TagString(tag); - } catch (IndexOutOfBoundsException e) { - str="Incorrect CP index:"+cpx; - } - return str; - } - - /** - * Returns minor version of class file. - */ - public int getMinor_version(){ - return minor_version; - } - - /** - * Returns major version of class file. - */ - public int getMajor_version(){ - return major_version; - } - - private boolean isJavaIdentifierStart(int cp) { - return ('a' <= cp && cp <= 'z') || ('A' <= cp && cp <= 'Z'); - } - - private boolean isJavaIdentifierPart(int cp) { - return isJavaIdentifierStart(cp) || ('0' <= cp && cp <= '9'); - } - - public String[] getNameAndType(int indx) { - return getNameAndType(indx, 0, new String[2]); - } - - private String[] getNameAndType(int indx, int at, String[] arr) { - CPX2 c2 = getCpoolEntry(indx); - arr[at] = StringValue(c2.cpx1); - arr[at + 1] = StringValue(c2.cpx2); - return arr; - } - - public String[] getFieldInfoName(int indx) { - CPX2 c2 = getCpoolEntry(indx); - String[] arr = new String[3]; - arr[0] = getClassName(c2.cpx1); - return getNameAndType(c2.cpx2, 1, arr); - } - - static byte[] findAttr(String n, AttrData[] attrs) { - for (AttrData ad : attrs) { - if (n.equals(ad.getAttrName())) { - return ad.getData(); - } - } - return null; - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/Constants.java --- a/javap/src/main/java/sun/tools/javap/Constants.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,372 +0,0 @@ -/* - * Copyright (c) 2002, 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; - -/** - * This interface defines constant that are used - * throughout the compiler. It inherits from RuntimeConstants, - * which is an autogenerated class that contains contstants - * defined in the interpreter. - */ - -public -interface Constants extends RuntimeConstants { - - /** - * End of input - */ - public static final int EOF = -1; - - /* - * Flags - */ - public static final int F_VERBOSE = 1 << 0; - public static final int F_DUMP = 1 << 1; - public static final int F_WARNINGS = 1 << 2; - public static final int F_DEBUG = 1 << 3; - public static final int F_OPTIMIZE = 1 << 4; - public static final int F_DEPENDENCIES = 1 << 5; - - /* - * Type codes - */ - public static final int TC_BOOLEAN = 0; - public static final int TC_BYTE = 1; - public static final int TC_CHAR = 2; - public static final int TC_SHORT = 3; - public static final int TC_INT = 4; - public static final int TC_LONG = 5; - public static final int TC_FLOAT = 6; - public static final int TC_DOUBLE = 7; - public static final int TC_NULL = 8; - public static final int TC_ARRAY = 9; - public static final int TC_CLASS = 10; - public static final int TC_VOID = 11; - public static final int TC_METHOD = 12; - public static final int TC_ERROR = 13; - - /* - * Type Masks - */ - public static final int TM_NULL = 1 << TC_NULL; - public static final int TM_VOID = 1 << TC_VOID; - public static final int TM_BOOLEAN = 1 << TC_BOOLEAN; - public static final int TM_BYTE = 1 << TC_BYTE; - public static final int TM_CHAR = 1 << TC_CHAR; - public static final int TM_SHORT = 1 << TC_SHORT; - public static final int TM_INT = 1 << TC_INT; - public static final int TM_LONG = 1 << TC_LONG; - public static final int TM_FLOAT = 1 << TC_FLOAT; - public static final int TM_DOUBLE = 1 << TC_DOUBLE; - public static final int TM_ARRAY = 1 << TC_ARRAY; - public static final int TM_CLASS = 1 << TC_CLASS; - public static final int TM_METHOD = 1 << TC_METHOD; - public static final int TM_ERROR = 1 << TC_ERROR; - - public static final int TM_INT32 = TM_BYTE | TM_SHORT | TM_CHAR | TM_INT; - public static final int TM_NUM32 = TM_INT32 | TM_FLOAT; - public static final int TM_NUM64 = TM_LONG | TM_DOUBLE; - public static final int TM_INTEGER = TM_INT32 | TM_LONG; - public static final int TM_REAL = TM_FLOAT | TM_DOUBLE; - public static final int TM_NUMBER = TM_INTEGER | TM_REAL; - public static final int TM_REFERENCE = TM_ARRAY | TM_CLASS | TM_NULL; - - /* - * Class status - */ - public static final int CS_UNDEFINED = 0; - public static final int CS_UNDECIDED = 1; - public static final int CS_BINARY = 2; - public static final int CS_SOURCE = 3; - public static final int CS_PARSED = 4; - public static final int CS_COMPILED = 5; - public static final int CS_NOTFOUND = 6; - - /* - * Attributes - */ - public static final int ATT_ALL = -1; - public static final int ATT_CODE = 1; - - /* - * Number of bits used in file offsets - */ - public static final int OFFSETBITS = 19; - public static final int MAXFILESIZE = (1 << OFFSETBITS) - 1; - public static final int MAXLINENUMBER = (1 << (32 - OFFSETBITS)) - 1; - - /* - * Operators - */ - public final int COMMA = 0; - public final int ASSIGN = 1; - - public final int ASGMUL = 2; - public final int ASGDIV = 3; - public final int ASGREM = 4; - public final int ASGADD = 5; - public final int ASGSUB = 6; - public final int ASGLSHIFT = 7; - public final int ASGRSHIFT = 8; - public final int ASGURSHIFT = 9; - public final int ASGBITAND = 10; - public final int ASGBITOR = 11; - public final int ASGBITXOR = 12; - - public final int COND = 13; - public final int OR = 14; - public final int AND = 15; - public final int BITOR = 16; - public final int BITXOR = 17; - public final int BITAND = 18; - public final int NE = 19; - public final int EQ = 20; - public final int GE = 21; - public final int GT = 22; - public final int LE = 23; - public final int LT = 24; - public final int INSTANCEOF = 25; - public final int LSHIFT = 26; - public final int RSHIFT = 27; - public final int URSHIFT = 28; - public final int ADD = 29; - public final int SUB = 30; - public final int DIV = 31; - public final int REM = 32; - public final int MUL = 33; - public final int CAST = 34; // (x)y - public final int POS = 35; // +x - public final int NEG = 36; // -x - public final int NOT = 37; - public final int BITNOT = 38; - public final int PREINC = 39; // ++x - public final int PREDEC = 40; // --x - public final int NEWARRAY = 41; - public final int NEWINSTANCE = 42; - public final int NEWFROMNAME = 43; - public final int POSTINC = 44; // x++ - public final int POSTDEC = 45; // x-- - public final int FIELD = 46; - public final int METHOD = 47; // x(y) - public final int ARRAYACCESS = 48; // x[y] - public final int NEW = 49; - public final int INC = 50; - public final int DEC = 51; - - public final int CONVERT = 55; // implicit conversion - public final int EXPR = 56; // (x) - public final int ARRAY = 57; // {x, y, ...} - public final int GOTO = 58; - - /* - * Value tokens - */ - public final int IDENT = 60; - public final int BOOLEANVAL = 61; - public final int BYTEVAL = 62; - public final int CHARVAL = 63; - public final int SHORTVAL = 64; - public final int INTVAL = 65; - public final int LONGVAL = 66; - public final int FLOATVAL = 67; - public final int DOUBLEVAL = 68; - public final int STRINGVAL = 69; - - /* - * Type keywords - */ - public final int BYTE = 70; - public final int CHAR = 71; - public final int SHORT = 72; - public final int INT = 73; - public final int LONG = 74; - public final int FLOAT = 75; - public final int DOUBLE = 76; - public final int VOID = 77; - public final int BOOLEAN = 78; - - /* - * Expression keywords - */ - public final int TRUE = 80; - public final int FALSE = 81; - public final int THIS = 82; - public final int SUPER = 83; - public final int NULL = 84; - - /* - * Statement keywords - */ - public final int IF = 90; - public final int ELSE = 91; - public final int FOR = 92; - public final int WHILE = 93; - public final int DO = 94; - public final int SWITCH = 95; - public final int CASE = 96; - public final int DEFAULT = 97; - public final int BREAK = 98; - public final int CONTINUE = 99; - public final int RETURN = 100; - public final int TRY = 101; - public final int CATCH = 102; - public final int FINALLY = 103; - public final int THROW = 104; - public final int STAT = 105; - public final int EXPRESSION = 106; - public final int DECLARATION = 107; - public final int VARDECLARATION = 108; - - /* - * Declaration keywords - */ - public final int IMPORT = 110; - public final int CLASS = 111; - public final int EXTENDS = 112; - public final int IMPLEMENTS = 113; - public final int INTERFACE = 114; - public final int PACKAGE = 115; - - /* - * Modifier keywords - */ - public final int PRIVATE = 120; - public final int PUBLIC = 121; - public final int PROTECTED = 122; - public final int CONST = 123; - public final int STATIC = 124; - public final int TRANSIENT = 125; - public final int SYNCHRONIZED = 126; - public final int NATIVE = 127; - public final int FINAL = 128; - public final int VOLATILE = 129; - public final int ABSTRACT = 130; - public final int STRICT = 165; - - /* - * Punctuation - */ - public final int SEMICOLON = 135; - public final int COLON = 136; - public final int QUESTIONMARK = 137; - public final int LBRACE = 138; - public final int RBRACE = 139; - public final int LPAREN = 140; - public final int RPAREN = 141; - public final int LSQBRACKET = 142; - public final int RSQBRACKET = 143; - public final int THROWS = 144; - - /* - * Special tokens - */ - public final int ERROR = 145; // an error - public final int COMMENT = 146; // not used anymore. - public final int TYPE = 147; - public final int LENGTH = 148; - public final int INLINERETURN = 149; - public final int INLINEMETHOD = 150; - public final int INLINENEWINSTANCE = 151; - - /* - * Added for jasm - */ - public final int METHODREF = 152; - public final int FIELDREF = 153; - public final int STACK = 154; - public final int LOCAL = 155; - public final int CPINDEX = 156; - public final int CPNAME = 157; - public final int SIGN = 158; - public final int BITS = 159; - public final int INF = 160; - public final int NAN = 161; - public final int INNERCLASS = 162; - public final int OF = 163; - public final int SYNTHETIC = 164; -// last used=165; - - /* - * Operator precedence - */ - public static final int opPrecedence[] = { - 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 12, 13, 14, 15, 16, 17, 18, - 18, 19, 19, 19, 19, 19, 20, 20, 20, 21, - 21, 22, 22, 22, 23, 24, 24, 24, 24, 24, - 24, 25, 25, 26, 26, 26, 26, 26, 26 - }; - - /* - * Operator names - */ - public static final String opNames[] = { - ",", "=", "*=", "/=", "%=", - "+=", "-=", "<<=", ">>=", "<<<=", - "&=", "|=", "^=", "?:", "||", - "&&", "|", "^", "&", "!=", - "==", ">=", ">", "<=", "<", - "instanceof", "<<", ">>", "<<<", "+", - "-", "/", "%", "*", "cast", - "+", "-", "!", "~", "++", - "--", "new", "new", "new", "++", - "--", "field", "method", "[]", "new", - "++", "--", null, null, null, - - "convert", "expr", "array", "goto", null, - - "Identifier", "Boolean", "Byte", "Char", "Short", - "Integer", "Long", "Float", "Double", "String", - - "byte", "char", "short", "int", "long", - "float", "double", "void", "boolean", null, - - "true", "false", "this", "super", "null", - null, null, null, null, null, - - "if", "else", "for", "while", "do", - "switch", "case", "default", "break", "continue", - "return", "try", "catch", "finally", "throw", - "stat", "expression", "declaration", "declaration", null, - - "import", "class", "extends", "implements", "interface", - "package", null, null, null, null, - - "private", "public", "protected", "const", "static", - "transient", "synchronized", "native", "final", "volatile", - "abstract", null, null, null, null, - - ";", ":", "?", "{", "}", - "(", ")", "[", "]", "throws", - "error", "comment", "type", "length", "inline-return", - "inline-method", "inline-new", - "method", "field", "stack", "locals", "CPINDEX", "CPName", "SIGN", - "bits", "INF", "NaN", "InnerClass", "of", "synthetic" - }; - -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/FieldData.java --- a/javap/src/main/java/sun/tools/javap/FieldData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,167 +0,0 @@ -/* - * Copyright (c) 2002, 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.*; - -/** - * Strores field data informastion. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ - -public class FieldData implements RuntimeConstants { - - ClassData cls; - int access; - int name_index; - int descriptor_index; - int attributes_count; - int value_cpx=0; - boolean isSynthetic=false; - boolean isDeprecated=false; - Vector attrs; - - public FieldData(ClassData cls){ - this.cls=cls; - } - - /** - * Read and store field info. - */ - public void read(DataInputStream in) throws IOException { - access = in.readUnsignedShort(); - name_index = in.readUnsignedShort(); - descriptor_index = in.readUnsignedShort(); - // Read the attributes - int attributes_count = in.readUnsignedShort(); - attrs=new Vector(attributes_count); - for (int i = 0; i < attributes_count; i++) { - int attr_name_index=in.readUnsignedShort(); - if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue; - String attr_name=cls.getString(attr_name_index); - if (attr_name.equals("ConstantValue")){ - if (in.readInt()!=2) - throw new ClassFormatError("invalid ConstantValue attr length"); - value_cpx=in.readUnsignedShort(); - AttrData attr=new AttrData(cls); - attr.read(attr_name_index); - attrs.addElement(attr); - } else if (attr_name.equals("Synthetic")){ - if (in.readInt()!=0) - throw new ClassFormatError("invalid Synthetic attr length"); - isSynthetic=true; - AttrData attr=new AttrData(cls); - attr.read(attr_name_index); - attrs.addElement(attr); - } else if (attr_name.equals("Deprecated")){ - if (in.readInt()!=0) - throw new ClassFormatError("invalid Synthetic attr length"); - isDeprecated = true; - AttrData attr=new AttrData(cls); - attr.read(attr_name_index); - attrs.addElement(attr); - } else { - AttrData attr=new AttrData(cls); - attr.read(attr_name_index, in); - attrs.addElement(attr); - } - } - - } // end read - - public boolean isStatic() { - return (access & ACC_STATIC) != 0; - } - - /** - * Returns access of a field. - */ - public String[] getAccess(){ - Vector v = new Vector(); - if ((access & ACC_PUBLIC) !=0) v.addElement("public"); - if ((access & ACC_PRIVATE) !=0) v.addElement("private"); - if ((access & ACC_PROTECTED) !=0) v.addElement("protected"); - if ((access & ACC_STATIC) !=0) v.addElement("static"); - if ((access & ACC_FINAL) !=0) v.addElement("final"); - if ((access & ACC_VOLATILE) !=0) v.addElement("volatile"); - if ((access & ACC_TRANSIENT) !=0) v.addElement("transient"); - String[] accflags = new String[v.size()]; - v.copyInto(accflags); - return accflags; - } - - /** - * Returns name of a field. - */ - public String getName(){ - return cls.getStringValue(name_index); - } - - /** - * Returns internal signature of a field - */ - public String getInternalSig(){ - return cls.getStringValue(descriptor_index); - } - - /** - * Returns java type signature of a field. - */ - public String getType(){ - return new TypeSignature(getInternalSig()).getFieldType(); - } - - /** - * Returns true if field is synthetic. - */ - public boolean isSynthetic(){ - return isSynthetic; - } - - /** - * Returns true if field is deprecated. - */ - public boolean isDeprecated(){ - return isDeprecated; - } - - /** - * Returns index of constant value in cpool. - */ - public int getConstantValueIndex(){ - return (value_cpx); - } - - /** - * Returns list of attributes of field. - */ - public Vector getAttributes(){ - return attrs; - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/Hashtable.java --- a/javap/src/main/java/sun/tools/javap/Hashtable.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package sun.tools.javap; - -/** A JavaScript optimized replacement for Hashtable. - * - * @author Jaroslav Tulach - */ -final class Hashtable { - private Object[] keys; - private Object[] values; - - Hashtable(int i) { - this(); - } - - Hashtable(int i, double d) { - this(); - } - - Hashtable() { - } - - synchronized void put(Object key, Object val) { - int[] where = { -1, -1 }; - Object found = get(key, where); - if (where[0] != -1) { - // key exists - values[where[0]] = val; - } else { - if (where[1] != -1) { - // null found - keys[where[1]] = key; - values[where[1]] = val; - } else { - if (keys == null) { - keys = new Object[11]; - values = new Object[11]; - keys[0] = key; - values[0] = val; - } else { - Object[] newKeys = new Object[keys.length * 2]; - Object[] newValues = new Object[values.length * 2]; - for (int i = 0; i < keys.length; i++) { - newKeys[i] = keys[i]; - newValues[i] = values[i]; - } - newKeys[keys.length] = key; - newValues[keys.length] = val; - keys = newKeys; - values = newValues; - } - } - } - } - - Object get(Object key) { - return get(key, null); - } - private synchronized Object get(Object key, int[] foundAndNull) { - if (keys == null) { - return null; - } - for (int i = 0; i < keys.length; i++) { - if (keys[i] == null) { - if (foundAndNull != null) { - foundAndNull[1] = i; - } - } else if (keys[i].equals(key)) { - if (foundAndNull != null) { - foundAndNull[0] = i; - } - return values[i]; - } - } - return null; - } - -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/InnerClassData.java --- a/javap/src/main/java/sun/tools/javap/InnerClassData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2002, 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.io.*; -import java.util.*; - -/** - * Strores InnerClass data informastion. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -class InnerClassData implements RuntimeConstants { - ClassData cls; - - - int inner_class_info_index - ,outer_class_info_index - ,inner_name_index - ,access - ; - - public InnerClassData(ClassData cls) { - this.cls=cls; - - } - - /** - * Read Innerclass attribute data. - */ - public void read(DataInputStream in) throws IOException { - inner_class_info_index = in.readUnsignedShort(); - outer_class_info_index = in.readUnsignedShort(); - inner_name_index = in.readUnsignedShort(); - access = in.readUnsignedShort(); - } // end read - - /** - * Returns the access of this class or interface. - */ - public String[] getAccess(){ - Vector v = new Vector(); - if ((access & ACC_PUBLIC) !=0) v.addElement("public"); - if ((access & ACC_FINAL) !=0) v.addElement("final"); - if ((access & ACC_ABSTRACT) !=0) v.addElement("abstract"); - String[] accflags = new String[v.size()]; - v.copyInto(accflags); - return accflags; - } - -} // end InnerClassData diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/LineNumData.java --- a/javap/src/main/java/sun/tools/javap/LineNumData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2002, 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.*; - -/** - * Strores LineNumberTable data information. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -class LineNumData { - short start_pc, line_number; - - public LineNumData() {} - - /** - * Read LineNumberTable attribute. - */ - public LineNumData(DataInputStream in) throws IOException { - start_pc = in.readShort(); - line_number=in.readShort(); - - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/LocVarData.java --- a/javap/src/main/java/sun/tools/javap/LocVarData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2002, 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.*; - -/** - * Strores LocalVariableTable data information. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -class LocVarData { - short start_pc, length, name_cpx, sig_cpx, slot; - - public LocVarData() { - } - - /** - * Read LocalVariableTable attribute. - */ - public LocVarData(DataInputStream in) throws IOException { - start_pc = in.readShort(); - length=in.readShort(); - name_cpx=in.readShort(); - sig_cpx=in.readShort(); - slot=in.readShort(); - - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/MethodData.java --- a/javap/src/main/java/sun/tools/javap/MethodData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,425 +0,0 @@ -/* - * Copyright (c) 2002, 2005, 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.io.*; -import org.apidesign.bck2brwsr.core.JavaScriptBody; - -import static sun.tools.javap.RuntimeConstants.*; - -/** - * Strores method data informastion. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -public class MethodData { - - ClassData cls; - int access; - int name_index; - int descriptor_index; - int attributes_count; - byte[] code; - Vector exception_table = new Vector(0); - Vector lin_num_tb = new Vector(0); - Vector loc_var_tb = new Vector(0); - StackMapTableData[] stackMapTable; - StackMapData[] stackMap; - int[] exc_index_table=null; - Vector attrs=new Vector(0); - Vector code_attrs=new Vector(0); - int max_stack, max_locals; - boolean isSynthetic=false; - boolean isDeprecated=false; - - public MethodData(ClassData cls){ - this.cls=cls; - } - - /** - * Read method info. - */ - public void read(DataInputStream in) throws IOException { - access = in.readUnsignedShort(); - name_index=in.readUnsignedShort(); - descriptor_index =in.readUnsignedShort(); - int attributes_count = in.readUnsignedShort(); - for (int i = 0; i < attributes_count; i++) { - int attr_name_index=in.readUnsignedShort(); - - readAttr: { - if (cls.getTag(attr_name_index)==CONSTANT_UTF8) { - String attr_name=cls.getString(attr_name_index); - if ( attr_name.equals("Code")){ - readCode (in); - AttrData attr=new AttrData(cls); - attr.read(attr_name_index); - attrs.addElement(attr); - break readAttr; - } else if ( attr_name.equals("Exceptions")){ - readExceptions(in); - AttrData attr=new AttrData(cls); - attr.read(attr_name_index); - attrs.addElement(attr); - break readAttr; - } else if (attr_name.equals("Synthetic")){ - if (in.readInt()!=0) - throw new ClassFormatError("invalid Synthetic attr length"); - isSynthetic=true; - AttrData attr=new AttrData(cls); - attr.read(attr_name_index); - attrs.addElement(attr); - break readAttr; - } else if (attr_name.equals("Deprecated")){ - if (in.readInt()!=0) - throw new ClassFormatError("invalid Synthetic attr length"); - isDeprecated = true; - AttrData attr=new AttrData(cls); - attr.read(attr_name_index); - attrs.addElement(attr); - break readAttr; - } - } - AttrData attr=new AttrData(cls); - attr.read(attr_name_index, in); - attrs.addElement(attr); - } - } - } - - /** - * Read code attribute info. - */ - public void readCode(DataInputStream in) throws IOException { - - int attr_length = in.readInt(); - max_stack=in.readUnsignedShort(); - max_locals=in.readUnsignedShort(); - int codelen=in.readInt(); - - code=new byte[codelen]; - int totalread = 0; - while(totalread < codelen){ - totalread += in.read(code, totalread, codelen-totalread); - } - // in.read(code, 0, codelen); - int clen = 0; - readExceptionTable(in); - int code_attributes_count = in.readUnsignedShort(); - - for (int k = 0 ; k < code_attributes_count ; k++) { - int table_name_index=in.readUnsignedShort(); - int table_name_tag=cls.getTag(table_name_index); - AttrData attr=new AttrData(cls); - if (table_name_tag==CONSTANT_UTF8) { - String table_name_tstr=cls.getString(table_name_index); - if (table_name_tstr.equals("LineNumberTable")) { - readLineNumTable(in); - attr.read(table_name_index); - } else if (table_name_tstr.equals("LocalVariableTable")) { - readLocVarTable(in); - attr.read(table_name_index); - } else if (table_name_tstr.equals("StackMapTable")) { - readStackMapTable(in); - attr.read(table_name_index); - } else if (table_name_tstr.equals("StackMap")) { - readStackMap(in); - attr.read(table_name_index); - } else { - attr.read(table_name_index, in); - } - code_attrs.addElement(attr); - continue; - } - - attr.read(table_name_index, in); - code_attrs.addElement(attr); - } - } - - /** - * Read exception table info. - */ - void readExceptionTable (DataInputStream in) throws IOException { - int exception_table_len=in.readUnsignedShort(); - exception_table=new Vector(exception_table_len); - for (int l = 0; l < exception_table_len; l++) { - exception_table.addElement(new TrapData(in, l)); - } - } - - /** - * Read LineNumberTable attribute info. - */ - void readLineNumTable (DataInputStream in) throws IOException { - int attr_len = in.readInt(); // attr_length - int lin_num_tb_len = in.readUnsignedShort(); - lin_num_tb=new Vector(lin_num_tb_len); - for (int l = 0; l < lin_num_tb_len; l++) { - lin_num_tb.addElement(new LineNumData(in)); - } - } - - /** - * Read LocalVariableTable attribute info. - */ - void readLocVarTable (DataInputStream in) throws IOException { - int attr_len=in.readInt(); // attr_length - int loc_var_tb_len = in.readUnsignedShort(); - loc_var_tb = new Vector(loc_var_tb_len); - for (int l = 0; l < loc_var_tb_len; l++) { - loc_var_tb.addElement(new LocVarData(in)); - } - } - - /** - * Read Exception attribute info. - */ - public void readExceptions(DataInputStream in) throws IOException { - int attr_len=in.readInt(); // attr_length in prog - int num_exceptions = in.readUnsignedShort(); - exc_index_table=new int[num_exceptions]; - for (int l = 0; l < num_exceptions; l++) { - int exc=in.readShort(); - exc_index_table[l]=exc; - } - } - - /** - * Read StackMapTable attribute info. - */ - void readStackMapTable(DataInputStream in) throws IOException { - int attr_len = in.readInt(); //attr_length - int stack_map_tb_len = in.readUnsignedShort(); - stackMapTable = new StackMapTableData[stack_map_tb_len]; - for (int i=0; i>8) { - case 0: - return opcLengthsTab[opc]; - case opc_wide: - switch (opc&0xFF) { - 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: - return 4; - case opc_iinc: - return 6; - default: - throw new ArrayIndexOutOfBoundsException(); - } - case opc_nonpriv: - case opc_priv: - return 2; - default: - throw new ArrayIndexOutOfBoundsException(); - } - } - - public static String opcName(int opc) { - try { - switch (opc>>8) { - case 0: - return opcNamesTab[opc]; - case opc_wide: { - String mnem=opcNamesTab[opc&0xFF]+"_w"; - if (mnemocodes.get(mnem) == null) - return null; // non-existent opcode - return mnem; - } - case opc_nonpriv: - return opcExtNamesTab[opc&0xFF]; - case opc_priv: - return opcPrivExtNamesTab[opc&0xFF]; - default: - return null; - } - } catch (ArrayIndexOutOfBoundsException e) { - switch (opc) { - case opc_nonpriv: - return "nonpriv"; - case opc_priv: - return "priv"; - default: - return null; - } - } - } - - public static int opcode(String mnem) { - Integer Val=(Integer)(mnemocodes.get(mnem)); - if (Val == null) return -1; - return Val.intValue(); - } - - /** - * Initialized keyword and token Hashtables - */ - static Vector keywordNames = new Vector(40); - private static void defineKeywordName(String id, int token) { - - if (token>=keywordNames.size()) { - keywordNames.setSize(token+1); - } - keywordNames.setElementAt(id, token); - } - public static String keywordName(int token) { - if (token==-1) return "EOF"; - if (token>=keywordNames.size()) return null; - return (String)keywordNames.elementAt(token); - } - static { - defineKeywordName("ident", IDENT); - defineKeywordName("STRINGVAL", STRINGVAL); - defineKeywordName("intVal", INTVAL); - defineKeywordName("longVal", LONGVAL); - defineKeywordName("floatVal", FLOATVAL); - defineKeywordName("doubleVal", DOUBLEVAL); - defineKeywordName("SEMICOLON", SEMICOLON); - defineKeywordName("COLON", COLON); - defineKeywordName("LBRACE", LBRACE); - defineKeywordName("RBRACE", RBRACE); - } - - static Hashtable keywords = new Hashtable(40); - public static int keyword(String idValue) { - Integer Val=(Integer)(keywords.get(idValue)); - if (Val == null) return IDENT; - return Val.intValue(); - } - - private static void defineKeyword(String id, int token) { - keywords.put(id, new Integer(token)); - defineKeywordName(id, token); - } - static { - // Modifier keywords - defineKeyword("private", PRIVATE); - defineKeyword("public", PUBLIC); - defineKeyword("protected", PROTECTED); - defineKeyword("static", STATIC); - defineKeyword("transient", TRANSIENT); - defineKeyword("synchronized", SYNCHRONIZED); - defineKeyword("super", SUPER); - defineKeyword("native", NATIVE); - defineKeyword("abstract", ABSTRACT); - defineKeyword("volatile", VOLATILE); - defineKeyword("final", FINAL); - defineKeyword("interface",INTERFACE); - defineKeyword("synthetic",SYNTHETIC); - defineKeyword("strict",STRICT); - - // Declaration keywords - defineKeyword("package",PACKAGE); - defineKeyword("class",CLASS); - defineKeyword("extends",EXTENDS); - defineKeyword("implements",IMPLEMENTS); - defineKeyword("const", CONST); - defineKeyword("throws",THROWS); - defineKeyword("interface",INTERFACE); - defineKeyword("Method",METHODREF); - defineKeyword("Field",FIELDREF); - defineKeyword("stack",STACK); - defineKeyword("locals",LOCAL); - - // used in switchtables - defineKeyword("default", DEFAULT); - - // used in inner class declarations - defineKeyword("InnerClass", INNERCLASS); - defineKeyword("of", OF); - - // misc - defineKeyword("bits",BITS); - defineKeyword("Infinity",INF); - defineKeyword("Inf",INF); - defineKeyword("NaN",NAN); - } - - /** - * Define tag table. - */ - private static Vector tagNames = new Vector(10); - private static Hashtable Tags = new Hashtable(10); - static { - defineTag("Asciz",CONSTANT_UTF8); - defineTag("int",CONSTANT_INTEGER); - defineTag("float",CONSTANT_FLOAT); - defineTag("long",CONSTANT_LONG); - defineTag("double",CONSTANT_DOUBLE); - defineTag("class",CONSTANT_CLASS); - defineTag("String",CONSTANT_STRING); - defineTag("Field",CONSTANT_FIELD); - defineTag("Method",CONSTANT_METHOD); - defineTag("InterfaceMethod",CONSTANT_INTERFACEMETHOD); - defineTag("NameAndType",CONSTANT_NAMEANDTYPE); - } - private static void defineTag(String id, int val) { - Tags.put(id, new Integer(val)); - if (val>=tagNames.size()) { - tagNames.setSize(val+1); - } - tagNames.setElementAt(id, val); - } - public static String tagName(int tag) { - if (tag>=tagNames.size()) return null; - return (String)tagNames.elementAt(tag); - } - public static int tagValue(String idValue) { - Integer Val=(Integer)(Tags.get(idValue)); - if (Val == null) return 0; - return Val.intValue(); - } - - /** - * Define type table. These types used in "newarray" instruction only. - */ - private static Vector typeNames = new Vector(10); - private static Hashtable Types = new Hashtable(10); - static { - defineType("int",T_INT); - defineType("long",T_LONG); - defineType("float",T_FLOAT); - defineType("double",T_DOUBLE); - defineType("class",T_CLASS); - defineType("boolean",T_BOOLEAN); - defineType("char",T_CHAR); - defineType("byte",T_BYTE); - defineType("short",T_SHORT); - } - private static void defineType(String id, int val) { - Types.put(id, new Integer(val)); - if (val>=typeNames.size()) { - typeNames.setSize(val+1); - } - typeNames.setElementAt(id, val); - } - public static int typeValue(String idValue) { - Integer Val=(Integer)(Types.get(idValue)); - if (Val == null) return -1; - return Val.intValue(); - } - public static String typeName(int type) { - if (type>=typeNames.size()) return null; - return (String)typeNames.elementAt(type); - } - - /** - * Define MapTypes table. - * These constants used in stackmap tables only. - */ - private static Vector mapTypeNames = new Vector(10); - private static Hashtable MapTypes = new Hashtable(10); - static { - defineMapType("bogus", ITEM_Bogus); - defineMapType("int", ITEM_Integer); - defineMapType("float", ITEM_Float); - defineMapType("double", ITEM_Double); - defineMapType("long", ITEM_Long); - defineMapType("null", ITEM_Null); - defineMapType("this", ITEM_InitObject); - defineMapType("CP", ITEM_Object); - defineMapType("uninitialized", ITEM_NewObject); - } - private static void defineMapType(String id, int val) { - MapTypes.put(id, new Integer(val)); - if (val>=mapTypeNames.size()) { - mapTypeNames.setSize(val+1); - } - mapTypeNames.setElementAt(id, val); - } - public static int mapTypeValue(String idValue) { - Integer Val=(Integer)(MapTypes.get(idValue)); - if (Val == null) return -1; - return Val.intValue(); - } - public static String mapTypeName(int type) { - if (type>=mapTypeNames.size()) return null; - return (String)mapTypeNames.elementAt(type); - } - -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/TrapData.java --- a/javap/src/main/java/sun/tools/javap/TrapData.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2002, 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.*; - -/** - * Stores exception table data in code attribute. - * - * @author Sucheta Dambalkar (Adopted code from jdis) - */ -class TrapData { - short start_pc, end_pc, handler_pc, catch_cpx; - int num; - - - /** - * Read and store exception table data in code attribute. - */ - public TrapData(DataInputStream in, int num) throws IOException { - this.num=num; - start_pc = in.readShort(); - end_pc=in.readShort(); - handler_pc=in.readShort(); - catch_cpx=in.readShort(); - } - - /** - * returns recommended identifier - */ - public String ident() { - return "t"+num; - } - -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/TypeSignature.java --- a/javap/src/main/java/sun/tools/javap/TypeSignature.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,295 +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.*; - -/** - * Returns java type signature. - * - * @author Sucheta Dambalkar - */ -public class TypeSignature { - - String parameters = null; - String returntype = null; - String fieldtype = null; - int argumentlength = 0; - - public TypeSignature(String JVMSignature){ - - if(JVMSignature != null){ - if(JVMSignature.indexOf("(") == -1){ - //This is a field type. - this.fieldtype = getFieldTypeSignature(JVMSignature); - }else { - String parameterdes = null; - if((JVMSignature.indexOf(")")-1) > (JVMSignature.indexOf("("))){ - //Get parameter signature. - parameterdes = - JVMSignature.substring(JVMSignature.indexOf("(")+1, - JVMSignature.indexOf(")")); - this.parameters = getParametersHelper(parameterdes); - }else this.parameters = "()"; - //Get return type signature. - String returndes = JVMSignature.substring(JVMSignature.lastIndexOf(")")+1); - this.returntype = getReturnTypeHelper(returndes); - } - } - } - - /** - * Returns java type signature of a field. - */ - public String getFieldTypeSignature(String fielddes){ - if(fielddes.startsWith("L")){ - return(getObjectType(fielddes)); - }else if(fielddes.startsWith("[")){ - return(getArrayType(fielddes)); - }else - return(getBaseType(fielddes)); - } - - /** - * Returns java type signature of a parameter. - */ - public String getParametersHelper(String parameterdes){ - Vector parameters = new Vector(); - int startindex = -1; - int endindex = -1; - String param = ""; - - while(parameterdes != null){ - - if(parameterdes.startsWith("L")){ - //parameter is a object. - startindex = parameterdes.indexOf("L"); - endindex = parameterdes.indexOf(";"); - if(startindex < parameterdes.length()) { - if(endindex == parameterdes.length()-1) { - //last parameter - param = parameterdes.substring(startindex); - parameterdes = null; - }else if(endindex+1 < parameterdes.length()){ - //rest parameters - param = parameterdes.substring(startindex, endindex+1); - parameterdes = parameterdes.substring(endindex+1); - - } - parameters.add(getObjectType(param)); - } - }else if(parameterdes.startsWith("[")){ - //parameter is an array. - String componentType = ""; - int enddim = -1; - int st = 0; - while(true){ - if(st < parameterdes.length()){ - if(parameterdes.charAt(st) == '['){ - - enddim = st; - st++; - } - else break; - } - else break; - } - - if(enddim+1 < parameterdes.length()){ - /* Array dimension.*/ - param = parameterdes.substring(0,enddim+1); - - } - - int stotherparam = param.lastIndexOf("[")+1; - - if(stotherparam < parameterdes.length()){ - componentType = parameterdes.substring(stotherparam); - } - - if(componentType.startsWith("L")){ - //parameter is array of objects. - startindex = parameterdes.indexOf("L"); - endindex = parameterdes.indexOf(";"); - - if(endindex == parameterdes.length()-1){ - //last parameter - param += parameterdes.substring(startindex); - parameterdes = null; - }else if(endindex+1 < parameterdes.length()){ - //rest parameters - param += parameterdes.substring(startindex, endindex+1); - parameterdes = parameterdes.substring(endindex+1); - } - }else{ - //parameter is array of base type. - if(componentType.length() == 1){ - //last parameter. - param += componentType; - parameterdes = null; - } - else if (componentType.length() > 1) { - //rest parameters. - param += componentType.substring(0,1); - parameterdes = componentType.substring(1); - } - } - parameters.add(getArrayType(param)); - - - }else { - - //parameter is of base type. - if(parameterdes.length() == 1){ - //last parameter - param = parameterdes; - parameterdes = null; - } - else if (parameterdes.length() > 1) { - //rest parameters. - param = parameterdes.substring(0,1); - parameterdes = parameterdes.substring(1); - } - parameters.add(getBaseType(param)); - } - } - - /* number of arguments of a method.*/ - argumentlength = parameters.size(); - - /* java type signature.*/ - String parametersignature = "("; - int i; - - for(i = 0; i < parameters.size(); i++){ - parametersignature += (String)parameters.elementAt(i); - if(i != parameters.size()-1){ - parametersignature += ", "; - } - } - parametersignature += ")"; - return parametersignature; - } - - /** - * Returns java type signature for a return type. - */ - public String getReturnTypeHelper(String returndes){ - return getFieldTypeSignature(returndes); - } - - /** - * Returns java type signature for a base type. - */ - public String getBaseType(String baseType){ - if(baseType != null){ - if(baseType.equals("B")) return "byte"; - else if(baseType.equals("C")) return "char"; - else if(baseType.equals("D")) return "double"; - else if(baseType.equals("F")) return "float"; - else if(baseType.equals("I")) return "int"; - else if(baseType.equals("J")) return "long"; - else if(baseType.equals("S")) return "short"; - else if(baseType.equals("Z")) return "boolean"; - else if(baseType.equals("V")) return "void"; - } - return null; - } - - /** - * Returns java type signature for a object type. - */ - public String getObjectType(String JVMobjectType) { - String objectType = ""; - int startindex = JVMobjectType.indexOf("L")+1; - int endindex = JVMobjectType.indexOf(";"); - if((startindex != -1) && (endindex != -1)){ - if((startindex < JVMobjectType.length()) && (endindex < JVMobjectType.length())){ - objectType = JVMobjectType.substring(startindex, endindex); - } - objectType = objectType.replace('/','.'); - return objectType; - } - return null; - } - - /** - * Returns java type signature for array type. - */ - public String getArrayType(String arrayType) { - if(arrayType != null){ - String dimention = ""; - - while(arrayType.indexOf("[") != -1){ - dimention += "[]"; - - int startindex = arrayType.indexOf("[")+1; - if(startindex <= arrayType.length()){ - arrayType = arrayType.substring(startindex); - } - } - - String componentType = ""; - if(arrayType.startsWith("L")){ - componentType = getObjectType(arrayType); - }else { - componentType = getBaseType(arrayType); - } - return componentType+dimention; - } - return null; - } - - /** - * Returns java type signature for parameters. - */ - public String getParameters(){ - return parameters; - } - - /** - * Returns java type signature for return type. - */ - public String getReturnType(){ - return returntype; - } - - /** - * Returns java type signature for field type. - */ - public String getFieldType(){ - return fieldtype; - } - - /** - * Return number of arguments of a method. - */ - public int getArgumentlength(){ - return argumentlength; - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 javap/src/main/java/sun/tools/javap/Vector.java --- a/javap/src/main/java/sun/tools/javap/Vector.java Fri Nov 16 08:06:48 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package sun.tools.javap; - -/** A JavaScript ready replacement for java.util.Vector - * - * @author Jaroslav Tulach - */ -final class Vector { - private Object[] arr; - - Vector() { - } - - Vector(int i) { - this(); - } - - void add(Object objectType) { - addElement(objectType); - } - void addElement(Object obj) { - final int s = size(); - setSize(s + 1); - setElementAt(obj, s); - } - - int size() { - return arr == null ? 0 : arr.length; - } - - void copyInto(Object[] newArr) { - if (arr == null) { - return; - } - int min = Math.min(newArr.length, arr.length); - for (int i = 0; i < min; i++) { - newArr[i] = arr[i]; - } - } - - Object elementAt(int index) { - return arr[index]; - } - - void setSize(int len) { - Object[] newArr = new Object[len]; - copyInto(newArr); - arr = newArr; - } - - void setElementAt(Object val, int index) { - arr[index] = val; - } -} diff -r 413d37a24a4d -r 77f7135b6eb1 vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri Nov 16 08:06:48 2012 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri Nov 16 08:08:36 2012 +0100 @@ -21,11 +21,11 @@ import java.io.InputStream; import org.apidesign.bck2brwsr.core.ExtraJavaScript; import org.apidesign.bck2brwsr.core.JavaScriptBody; -import sun.tools.javap.AnnotationParser; -import sun.tools.javap.ClassData; -import sun.tools.javap.FieldData; -import sun.tools.javap.MethodData; -import static sun.tools.javap.RuntimeConstants.*; +import org.apidesign.javap.AnnotationParser; +import org.apidesign.javap.ClassData; +import org.apidesign.javap.FieldData; +import org.apidesign.javap.MethodData; +import static org.apidesign.javap.RuntimeConstants.*; /** Translator of the code inside class files to JavaScript. *