/**
   * 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());
  }
Beispiel #2
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('/', '.');
 }
Beispiel #3
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 CodeException object.<br>
  * 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.
  *
  * @param cp constant pool
  */
 public CodeException getCodeException(ConstantPoolGen cp) {
   return new CodeException(
       start_pc.getPosition(),
       end_pc.getPosition() + end_pc.getInstruction().getLength(),
       handler_pc.getPosition(),
       (catch_type == null) ? 0 : cp.addClass(catch_type));
 }
Beispiel #5
0
  public int[] getInterfaces() {
    int size = interface_vec.size();
    int[] interfaces = new int[size];

    for (int i = 0; i < size; i++) interfaces[i] = cp.addClass((String) interface_vec.get(i));

    return interfaces;
  }
Beispiel #6
0
  /**
   * Convenience method.
   *
   * <p>Add an empty constructor to this class that does nothing but calling super().
   *
   * @param access rights for constructor
   */
  public void addEmptyConstructor(int access_flags) {
    InstructionList il = new InstructionList();
    il.append(InstructionConstants.THIS); // Push `this'
    il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name, "<init>", "()V")));
    il.append(InstructionConstants.RETURN);

    MethodGen mg =
        new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null, "<init>", class_name, il, cp);
    mg.setMaxStack(1);
    addMethod(mg.getMethod());
  }
Beispiel #7
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]);
  }
Beispiel #8
0
 public void setSuperclassName(String name) {
   super_class_name = name.replace('/', '.');
   superclass_name_index = cp.addClass(name);
 }
Beispiel #9
0
 public void setClassName(String name) {
   class_name = name.replace('/', '.');
   class_name_index = cp.addClass(name);
 }
  /** 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);
    }
  }