langtools/src/share/classes/sun/tools/javap/Tables.java
changeset 656 4718b910737c
parent 10 06bc494ca11e
child 735 372aa565a221
equal deleted inserted replaced
655:1ebc7ce89018 656:4718b910737c
    24  */
    24  */
    25 
    25 
    26 
    26 
    27 package sun.tools.javap;
    27 package sun.tools.javap;
    28 
    28 
    29 import java.io.IOException;
       
    30 import java.io.InputStream;
       
    31 import java.util.Hashtable;
    29 import java.util.Hashtable;
    32 import java.util.Vector;
    30 import java.util.Vector;
    33 
    31 
    34 
    32 
    35 public class Tables implements Constants {
    33 public class Tables implements Constants {
    36     /**
    34     /**
    37      * Define mnemocodes table.
    35      * Define mnemocodes table.
    38      */
    36      */
    39   static  Hashtable mnemocodes = new Hashtable(301, 0.5f);
    37   static  Hashtable<String,Integer> mnemocodes = new Hashtable<String,Integer>(301, 0.5f);
    40   static  String opcExtNamesTab[]=new String[128];
    38   static  String opcExtNamesTab[]=new String[128];
    41   static  String opcPrivExtNamesTab[]=new String[128];
    39   static  String opcPrivExtNamesTab[]=new String[128];
    42   static  void defineNonPriv(int opc, String mnem) {
    40   static  void defineNonPriv(int opc, String mnem) {
    43         mnemocodes.put(opcExtNamesTab[opc]=mnem, new Integer(opc_nonpriv*256+opc));
    41         mnemocodes.put(opcExtNamesTab[opc]=mnem, opc_nonpriv*256+opc);
    44   }
    42   }
    45   static  void definePriv(int opc, String mnem) {
    43   static  void definePriv(int opc, String mnem) {
    46         mnemocodes.put(opcPrivExtNamesTab[opc]="priv_"+mnem, new Integer(opc_priv*256+opc));
    44         mnemocodes.put(opcPrivExtNamesTab[opc]="priv_"+mnem, opc_priv*256+opc);
    47   }
    45   }
    48   static  void defineExt(int opc, String mnem) {
    46   static  void defineExt(int opc, String mnem) {
    49         defineNonPriv(opc, mnem);
    47         defineNonPriv(opc, mnem);
    50         definePriv(opc, mnem);
    48         definePriv(opc, mnem);
    51   }
    49   }
    52   static { int k;
    50   static { int k;
    53         for (k=0; k<opc_wide; k++) {
    51         for (k=0; k<opc_wide; k++) {
    54                 mnemocodes.put(opcNamesTab[k], new Integer(k));
    52                 mnemocodes.put(opcNamesTab[k], k);
    55         }
    53         }
    56         for (k=opc_wide+1; k<opcNamesTab.length; k++) {
    54         for (k=opc_wide+1; k<opcNamesTab.length; k++) {
    57                 mnemocodes.put(opcNamesTab[k], new Integer(k));
    55                 mnemocodes.put(opcNamesTab[k], k);
    58         }
    56         }
    59         mnemocodes.put("invokenonvirtual", new Integer(opc_invokespecial));
    57         mnemocodes.put("invokenonvirtual", opc_invokespecial);
    60 
    58 
    61         mnemocodes.put("iload_w", new Integer(opc_iload_w));
    59         mnemocodes.put("iload_w", opc_iload_w);
    62         mnemocodes.put("lload_w", new Integer(opc_lload_w));
    60         mnemocodes.put("lload_w", opc_lload_w);
    63         mnemocodes.put("fload_w", new Integer(opc_fload_w));
    61         mnemocodes.put("fload_w", opc_fload_w);
    64         mnemocodes.put("dload_w", new Integer(opc_dload_w));
    62         mnemocodes.put("dload_w", opc_dload_w);
    65         mnemocodes.put("aload_w", new Integer(opc_aload_w));
    63         mnemocodes.put("aload_w", opc_aload_w);
    66         mnemocodes.put("istore_w", new Integer(opc_istore_w));
    64         mnemocodes.put("istore_w", opc_istore_w);
    67         mnemocodes.put("lstore_w", new Integer(opc_lstore_w));
    65         mnemocodes.put("lstore_w", opc_lstore_w);
    68         mnemocodes.put("fstore_w", new Integer(opc_fstore_w));
    66         mnemocodes.put("fstore_w", opc_fstore_w);
    69         mnemocodes.put("dstore_w", new Integer(opc_dstore_w));
    67         mnemocodes.put("dstore_w", opc_dstore_w);
    70         mnemocodes.put("astore_w", new Integer(opc_astore_w));
    68         mnemocodes.put("astore_w", opc_astore_w);
    71         mnemocodes.put("ret_w", new Integer(opc_ret_w));
    69         mnemocodes.put("ret_w", opc_ret_w);
    72         mnemocodes.put("iinc_w", new Integer(opc_iinc_w));
    70         mnemocodes.put("iinc_w", opc_iinc_w);
    73 
    71 
    74         mnemocodes.put("nonpriv", new Integer(opc_nonpriv));
    72         mnemocodes.put("nonpriv", opc_nonpriv);
    75         mnemocodes.put("priv", new Integer(opc_priv));
    73         mnemocodes.put("priv", opc_priv);
    76 
    74 
    77         defineExt(0, "load_ubyte");
    75         defineExt(0, "load_ubyte");
    78         defineExt(1, "load_byte");
    76         defineExt(1, "load_byte");
    79         defineExt(2, "load_char");
    77         defineExt(2, "load_char");
    80         defineExt(3, "load_short");
    78         defineExt(3, "load_short");
   181                 }
   179                 }
   182         }
   180         }
   183   }
   181   }
   184 
   182 
   185   public static int opcode(String mnem) {
   183   public static int opcode(String mnem) {
   186         Integer Val=(Integer)(mnemocodes.get(mnem));
   184         Integer Val=mnemocodes.get(mnem);
   187         if (Val == null) return -1;
   185         if (Val == null) return -1;
   188         return Val.intValue();
   186         return Val.intValue();
   189   }
   187   }
   190 
   188 
   191     /**
   189     /**
   192      * Initialized keyword and token Hashtables
   190      * Initialized keyword and token Hashtables
   193      */
   191      */
   194   static Vector keywordNames = new Vector(40);
   192   static Vector<String> keywordNames = new Vector<String>(40);
   195   private static void defineKeywordName(String id, int token) {
   193   private static void defineKeywordName(String id, int token) {
   196 
   194 
   197         if (token>=keywordNames.size()) {
   195         if (token>=keywordNames.size()) {
   198                 keywordNames.setSize(token+1);
   196                 keywordNames.setSize(token+1);
   199         }
   197         }
   200         keywordNames.setElementAt(id, token);
   198         keywordNames.setElementAt(id, token);
   201   }
   199   }
   202   public static String keywordName(int token) {
   200   public static String keywordName(int token) {
   203         if (token==-1) return "EOF";
   201         if (token==-1) return "EOF";
   204         if (token>=keywordNames.size()) return null;
   202         if (token>=keywordNames.size()) return null;
   205         return (String)keywordNames.elementAt(token);
   203         return keywordNames.elementAt(token);
   206   }
   204   }
   207   static {
   205   static {
   208         defineKeywordName("ident", IDENT);
   206         defineKeywordName("ident", IDENT);
   209         defineKeywordName("STRINGVAL", STRINGVAL);
   207         defineKeywordName("STRINGVAL", STRINGVAL);
   210         defineKeywordName("intVal", INTVAL);
   208         defineKeywordName("intVal", INTVAL);
   215         defineKeywordName("COLON", COLON);
   213         defineKeywordName("COLON", COLON);
   216         defineKeywordName("LBRACE", LBRACE);
   214         defineKeywordName("LBRACE", LBRACE);
   217         defineKeywordName("RBRACE", RBRACE);
   215         defineKeywordName("RBRACE", RBRACE);
   218   }
   216   }
   219 
   217 
   220   static Hashtable keywords = new Hashtable(40);
   218   static Hashtable<String,Integer> keywords = new Hashtable<String,Integer>(40);
   221   public static int keyword(String idValue) {
   219   public static int keyword(String idValue) {
   222         Integer Val=(Integer)(keywords.get(idValue));
   220         Integer val=keywords.get(idValue);
   223         if (Val == null) return IDENT;
   221         if (val == null) return IDENT;
   224         return Val.intValue();
   222         return val.intValue();
   225   }
   223   }
   226 
   224 
   227   private static void defineKeyword(String id, int token) {
   225   private static void defineKeyword(String id, int token) {
   228         keywords.put(id, new Integer(token));
   226         keywords.put(id, token);
   229         defineKeywordName(id, token);
   227         defineKeywordName(id, token);
   230   }
   228   }
   231   static {
   229   static {
   232         // Modifier keywords
   230         // Modifier keywords
   233         defineKeyword("private", PRIVATE);
   231         defineKeyword("private", PRIVATE);
   273   }
   271   }
   274 
   272 
   275    /**
   273    /**
   276      * Define tag table.
   274      * Define tag table.
   277      */
   275      */
   278   private static Vector tagNames = new Vector(10);
   276   private static Vector<String> tagNames = new Vector<String>(10);
   279   private static Hashtable Tags = new Hashtable(10);
   277   private static Hashtable<String,Integer> Tags = new Hashtable<String,Integer>(10);
   280   static {
   278   static {
   281         defineTag("Asciz",CONSTANT_UTF8);
   279         defineTag("Asciz",CONSTANT_UTF8);
   282         defineTag("int",CONSTANT_INTEGER);
   280         defineTag("int",CONSTANT_INTEGER);
   283         defineTag("float",CONSTANT_FLOAT);
   281         defineTag("float",CONSTANT_FLOAT);
   284         defineTag("long",CONSTANT_LONG);
   282         defineTag("long",CONSTANT_LONG);
   289         defineTag("Method",CONSTANT_METHOD);
   287         defineTag("Method",CONSTANT_METHOD);
   290         defineTag("InterfaceMethod",CONSTANT_INTERFACEMETHOD);
   288         defineTag("InterfaceMethod",CONSTANT_INTERFACEMETHOD);
   291         defineTag("NameAndType",CONSTANT_NAMEANDTYPE);
   289         defineTag("NameAndType",CONSTANT_NAMEANDTYPE);
   292   }
   290   }
   293   private static void defineTag(String id, int val) {
   291   private static void defineTag(String id, int val) {
   294         Tags.put(id, new Integer(val));
   292         Tags.put(id, val);
   295         if (val>=tagNames.size()) {
   293         if (val>=tagNames.size()) {
   296                 tagNames.setSize(val+1);
   294                 tagNames.setSize(val+1);
   297         }
   295         }
   298         tagNames.setElementAt(id, val);
   296         tagNames.setElementAt(id, val);
   299   }
   297   }
   300   public static String tagName(int tag) {
   298   public static String tagName(int tag) {
   301         if (tag>=tagNames.size()) return null;
   299         if (tag>=tagNames.size()) return null;
   302         return (String)tagNames.elementAt(tag);
   300         return tagNames.elementAt(tag);
   303   }
   301   }
   304   public static int tagValue(String idValue) {
   302   public static int tagValue(String idValue) {
   305         Integer Val=(Integer)(Tags.get(idValue));
   303         Integer Val=Tags.get(idValue);
   306         if (Val == null) return 0;
   304         if (Val == null) return 0;
   307         return Val.intValue();
   305         return Val.intValue();
   308   }
   306   }
   309 
   307 
   310    /**
   308    /**
   311      * Define type table. These types used in "newarray" instruction only.
   309      * Define type table. These types used in "newarray" instruction only.
   312      */
   310      */
   313   private static Vector typeNames = new Vector(10);
   311   private static Vector<String> typeNames = new Vector<String>(10);
   314   private static Hashtable Types = new Hashtable(10);
   312   private static Hashtable<String,Integer> Types = new Hashtable<String,Integer>(10);
   315   static {
   313   static {
   316         defineType("int",T_INT);
   314         defineType("int",T_INT);
   317         defineType("long",T_LONG);
   315         defineType("long",T_LONG);
   318         defineType("float",T_FLOAT);
   316         defineType("float",T_FLOAT);
   319         defineType("double",T_DOUBLE);
   317         defineType("double",T_DOUBLE);
   322         defineType("char",T_CHAR);
   320         defineType("char",T_CHAR);
   323         defineType("byte",T_BYTE);
   321         defineType("byte",T_BYTE);
   324         defineType("short",T_SHORT);
   322         defineType("short",T_SHORT);
   325   }
   323   }
   326   private static void defineType(String id, int val) {
   324   private static void defineType(String id, int val) {
   327         Types.put(id, new Integer(val));
   325         Types.put(id, val);
   328         if (val>=typeNames.size()) {
   326         if (val>=typeNames.size()) {
   329                 typeNames.setSize(val+1);
   327                 typeNames.setSize(val+1);
   330         }
   328         }
   331         typeNames.setElementAt(id, val);
   329         typeNames.setElementAt(id, val);
   332   }
   330   }
   333   public static int typeValue(String idValue) {
   331   public static int typeValue(String idValue) {
   334         Integer Val=(Integer)(Types.get(idValue));
   332         Integer Val=Types.get(idValue);
   335         if (Val == null) return -1;
   333         if (Val == null) return -1;
   336         return Val.intValue();
   334         return Val.intValue();
   337   }
   335   }
   338   public static String typeName(int type) {
   336   public static String typeName(int type) {
   339         if (type>=typeNames.size()) return null;
   337         if (type>=typeNames.size()) return null;
   340         return (String)typeNames.elementAt(type);
   338         return typeNames.elementAt(type);
   341   }
   339   }
   342 
   340 
   343    /**
   341    /**
   344      * Define MapTypes table.
   342      * Define MapTypes table.
   345      * These constants used in stackmap tables only.
   343      * These constants used in stackmap tables only.
   346      */
   344      */
   347   private static Vector mapTypeNames = new Vector(10);
   345   private static Vector<String> mapTypeNames = new Vector<String>(10);
   348   private static Hashtable MapTypes = new Hashtable(10);
   346   private static Hashtable<String,Integer> MapTypes = new Hashtable<String,Integer>(10);
   349   static {
   347   static {
   350         defineMapType("bogus",             ITEM_Bogus);
   348         defineMapType("bogus",             ITEM_Bogus);
   351         defineMapType("int",               ITEM_Integer);
   349         defineMapType("int",               ITEM_Integer);
   352         defineMapType("float",             ITEM_Float);
   350         defineMapType("float",             ITEM_Float);
   353         defineMapType("double",            ITEM_Double);
   351         defineMapType("double",            ITEM_Double);
   356         defineMapType("this",              ITEM_InitObject);
   354         defineMapType("this",              ITEM_InitObject);
   357         defineMapType("CP",                ITEM_Object);
   355         defineMapType("CP",                ITEM_Object);
   358         defineMapType("uninitialized",     ITEM_NewObject);
   356         defineMapType("uninitialized",     ITEM_NewObject);
   359   }
   357   }
   360   private static void defineMapType(String id, int val) {
   358   private static void defineMapType(String id, int val) {
   361         MapTypes.put(id, new Integer(val));
   359         MapTypes.put(id, val);
   362         if (val>=mapTypeNames.size()) {
   360         if (val>=mapTypeNames.size()) {
   363                 mapTypeNames.setSize(val+1);
   361                 mapTypeNames.setSize(val+1);
   364         }
   362         }
   365         mapTypeNames.setElementAt(id, val);
   363         mapTypeNames.setElementAt(id, val);
   366   }
   364   }
   367   public static int mapTypeValue(String idValue) {
   365   public static int mapTypeValue(String idValue) {
   368         Integer Val=(Integer)(MapTypes.get(idValue));
   366         Integer Val=MapTypes.get(idValue);
   369         if (Val == null) return -1;
   367         if (Val == null) return -1;
   370         return Val.intValue();
   368         return Val.intValue();
   371   }
   369   }
   372   public static String mapTypeName(int type) {
   370   public static String mapTypeName(int type) {
   373         if (type>=mapTypeNames.size()) return null;
   371         if (type>=mapTypeNames.size()) return null;
   374         return (String)mapTypeNames.elementAt(type);
   372         return mapTypeNames.elementAt(type);
   375   }
   373   }
   376 
   374 
   377 }
   375 }