示例#1
0
 public void setSuperclassNameIndex(int superclass_name_index) {
   this.superclass_name_index = superclass_name_index;
   super_class_name =
       cp.getConstantPool()
           .getConstantString(superclass_name_index, Constants.CONSTANT_Class)
           .replace('/', '.');
 }
示例#2
0
 public void setClassNameIndex(int class_name_index) {
   this.class_name_index = class_name_index;
   class_name =
       cp.getConstantPool()
           .getConstantString(class_name_index, Constants.CONSTANT_Class)
           .replace('/', '.');
 }
  /**
   * Get LocalVariable object.
   *
   * <p>This relies on that the instruction list has already been dumped to byte code or or that the
   * `setPositions' methods has been called for the instruction list.
   *
   * <p>Note that for local variables whose scope end at the last instruction of the method's code,
   * the JVM specification is ambiguous: both a start_pc+length ending at the last instruction and
   * start_pc+length ending at first index beyond the end of the code are valid.
   *
   * @param il instruction list (byte code) which this variable belongs to
   * @param cp constant pool
   */
  public LocalVariable getLocalVariable(ConstantPoolGen cp) {
    int start_pc = start.getPosition();
    int length = end.getPosition() - start_pc;

    if (length > 0) length += end.getInstruction().getLength();

    int name_index = cp.addUtf8(name);
    int signature_index = cp.addUtf8(type.getSignature());

    return new LocalVariable(
        start_pc, length, name_index, signature_index, index, cp.getConstantPool());
  }
示例#4
0
  /**
   * Convenience constructor to set up some important values initially.
   *
   * @param class_name fully qualified class name
   * @param super_class_name fully qualified superclass name
   * @param file_name source file name
   * @param access_flags access qualifiers
   * @param interfaces implemented interfaces
   * @param cp constant pool to use
   */
  public ClassGen(
      String class_name,
      String super_class_name,
      String file_name,
      int access_flags,
      String[] interfaces,
      ConstantPoolGen cp) {
    this.class_name = class_name;
    this.super_class_name = super_class_name;
    this.file_name = file_name;
    this.access_flags = access_flags;
    this.cp = cp;

    // Put everything needed by default into the constant pool and the vectors
    if (file_name != null)
      addAttribute(
          new SourceFile(cp.addUtf8("SourceFile"), 2, cp.addUtf8(file_name), cp.getConstantPool()));

    class_name_index = cp.addClass(class_name);
    superclass_name_index = cp.addClass(super_class_name);

    if (interfaces != null) for (int i = 0; i < interfaces.length; i++) addInterface(interfaces[i]);
  }
  /** 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);
    }
  }