Example #1
0
 /** Import constant from another ConstantPool and return new index. */
 public int addConstant(Constant c, ConstantPoolGen cp) {
   Constant[] constants = cp.getConstantPool().getConstantPool();
   switch (c.getTag()) {
     case Constants.CONSTANT_String:
       {
         ConstantString s = (ConstantString) c;
         ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()];
         return addString(u8.getBytes());
       }
     case Constants.CONSTANT_Class:
       {
         ConstantClass s = (ConstantClass) c;
         ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
         return addClass(u8.getBytes());
       }
     case Constants.CONSTANT_NameAndType:
       {
         ConstantNameAndType n = (ConstantNameAndType) c;
         ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
         ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
         return addNameAndType(u8.getBytes(), u8_2.getBytes());
       }
     case Constants.CONSTANT_Utf8:
       return addUtf8(((ConstantUtf8) c).getBytes());
     case Constants.CONSTANT_Double:
       return addDouble(((ConstantDouble) c).getBytes());
     case Constants.CONSTANT_Float:
       return addFloat(((ConstantFloat) c).getBytes());
     case Constants.CONSTANT_Long:
       return addLong(((ConstantLong) c).getBytes());
     case Constants.CONSTANT_Integer:
       return addInteger(((ConstantInteger) c).getBytes());
     case Constants.CONSTANT_InterfaceMethodref:
     case Constants.CONSTANT_Methodref:
     case Constants.CONSTANT_Fieldref:
       {
         ConstantCP m = (ConstantCP) c;
         ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
         ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
         ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
         String class_name = u8.getBytes().replace('/', '.');
         u8 = (ConstantUtf8) constants[n.getNameIndex()];
         String name = u8.getBytes();
         u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
         String signature = u8.getBytes();
         switch (c.getTag()) {
           case Constants.CONSTANT_InterfaceMethodref:
             return addInterfaceMethodref(class_name, name, signature);
           case Constants.CONSTANT_Methodref:
             return addMethodref(class_name, name, signature);
           case Constants.CONSTANT_Fieldref:
             return addFieldref(class_name, name, signature);
           default: // Never reached
             throw new RuntimeException("Unknown constant type " + c);
         }
       }
     default: // Never reached
       throw new RuntimeException("Unknown constant type " + c);
   }
 }
Example #2
0
 /**
  * Initialize with given array of constants.
  *
  * @param cs array of given constants, new ones will be appended
  */
 public ConstantPoolGen(Constant[] cs) {
   if (cs.length > size) {
     size = cs.length;
     constants = new Constant[size];
   }
   System.arraycopy(cs, 0, constants, 0, cs.length);
   if (cs.length > 0) {
     index = cs.length;
   }
   for (int i = 1; i < index; i++) {
     Constant c = constants[i];
     if (c instanceof ConstantString) {
       ConstantString s = (ConstantString) c;
       ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()];
       String key = u8.getBytes();
       if (!string_table.containsKey(key)) {
         string_table.put(key, new Index(i));
       }
     } else if (c instanceof ConstantClass) {
       ConstantClass s = (ConstantClass) c;
       ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
       String key = u8.getBytes();
       if (!class_table.containsKey(key)) {
         class_table.put(key, new Index(i));
       }
     } else if (c instanceof ConstantNameAndType) {
       ConstantNameAndType n = (ConstantNameAndType) c;
       ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
       ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
       String key = u8.getBytes() + NAT_DELIM + u8_2.getBytes();
       if (!n_a_t_table.containsKey(key)) {
         n_a_t_table.put(key, new Index(i));
       }
     } else if (c instanceof ConstantUtf8) {
       ConstantUtf8 u = (ConstantUtf8) c;
       String key = u.getBytes();
       if (!utf8_table.containsKey(key)) {
         utf8_table.put(key, new Index(i));
       }
     } else if (c instanceof ConstantCP) {
       ConstantCP m = (ConstantCP) c;
       ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
       ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
       ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
       String class_name = u8.getBytes().replace('/', '.');
       u8 = (ConstantUtf8) constants[n.getNameIndex()];
       String method_name = u8.getBytes();
       u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
       String signature = u8.getBytes();
       String delim = METHODREF_DELIM;
       if (c instanceof ConstantInterfaceMethodref) {
         delim = IMETHODREF_DELIM;
       } else if (c instanceof ConstantFieldref) {
         delim = FIELDREF_DELIM;
       }
       String key = class_name + delim + method_name + delim + signature;
       if (!cp_table.containsKey(key)) {
         cp_table.put(key, new Index(i));
       }
     }
   }
 }