Esempio n. 1
0
  /**
   * Override this method to create you own classes on the fly. The name contains the special token
   * $$BCEL$$. Everything before that token is consddered to be a package name. You can encode you
   * own arguments into the subsequent string. You must regard however not to use any "illegal"
   * characters, i.e., characters that may not appear in a Java class name too<br>
   * The default implementation interprets the string as a encoded compressed Java class, unpacks
   * and decodes it with the Utility.decode() method, and parses the resulting byte array and
   * returns the resulting JavaClass object.
   *
   * @param class_name compressed byte code with "$$BCEL$$" in it
   */
  protected JavaClass createClass(String class_name) {
    int index = class_name.indexOf("$$BCEL$$");
    String real_name = class_name.substring(index + 8);

    JavaClass clazz = null;
    try {
      byte[] bytes = Utility.decode(real_name, true);
      ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");

      clazz = parser.parse();
    } catch (Throwable e) {
      e.printStackTrace();
      return null;
    }

    // Adapt the class name to the passed value
    ConstantPool cp = clazz.getConstantPool();

    ConstantClass cl =
        (ConstantClass) cp.getConstant(clazz.getClassNameIndex(), Constants.CONSTANT_Class);
    ConstantUtf8 name = (ConstantUtf8) cp.getConstant(cl.getNameIndex(), Constants.CONSTANT_Utf8);
    name.setBytes(class_name.replace('.', '/'));

    return clazz;
  }
Esempio n. 2
0
  /* Class method reads one attribute from the input data stream.
   * This method must not be accessible from the outside.  It is
   * called by the Field and Method constructor methods.
   *
   * @see    Field
   * @see    Method
   * @param  file Input stream
   * @param  constant_pool Array of constants
   * @return Attribute
   * @throws  IOException
   * @throws  ClassFormatException
   */
  public static final Attribute readAttribute(DataInputStream file, ConstantPool constant_pool)
      throws IOException, ClassFormatException {
    ConstantUtf8 c;
    String name;
    int name_index;
    int length;
    byte tag = Constants.ATTR_UNKNOWN; // Unknown attribute

    // Get class name from constant pool via `name_index' indirection
    name_index = (int) file.readUnsignedShort();
    c = (ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
    name = c.getBytes();

    // Length of data in bytes
    length = file.readInt();

    // Compare strings to find known attribute
    for (byte i = 0; i < Constants.KNOWN_ATTRIBUTES; i++) {
      if (name.equals(Constants.ATTRIBUTE_NAMES[i])) {
        tag = i; // found!
        break;
      }
    }

    // Call proper constructor, depending on `tag'
    switch (tag) {
      case Constants.ATTR_UNKNOWN:
        AttributeReader r = (AttributeReader) readers.get(name);

        if (r != null) return r.createAttribute(name_index, length, file, constant_pool);
        else return new Unknown(name_index, length, file, constant_pool);

      case Constants.ATTR_CONSTANT_VALUE:
        return new ConstantValue(name_index, length, file, constant_pool);

      case Constants.ATTR_SOURCE_FILE:
        return new SourceFile(name_index, length, file, constant_pool);

      case Constants.ATTR_CODE:
        return new Code(name_index, length, file, constant_pool);

      case Constants.ATTR_EXCEPTIONS:
        return new ExceptionTable(name_index, length, file, constant_pool);

      case Constants.ATTR_LINE_NUMBER_TABLE:
        return new LineNumberTable(name_index, length, file, constant_pool);

      case Constants.ATTR_LOCAL_VARIABLE_TABLE:
        return new LocalVariableTable(name_index, length, file, constant_pool);

      case Constants.ATTR_INNER_CLASSES:
        return new InnerClasses(name_index, length, file, constant_pool);

      case Constants.ATTR_SYNTHETIC:
        return new Synthetic(name_index, length, file, constant_pool);

      case Constants.ATTR_DEPRECATED:
        return new Deprecated(name_index, length, file, constant_pool);

      case Constants.ATTR_PMG:
        return new PMGClass(name_index, length, file, constant_pool);

      case Constants.ATTR_SIGNATURE:
        return new Signature(name_index, length, file, constant_pool);

      case Constants.ATTR_STACK_MAP:
        return new StackMap(name_index, length, file, constant_pool);

      default: // Never reached
        throw new IllegalStateException("Ooops! default case reached.");
    }
  }
Esempio n. 3
0
 /** @return Signature. */
 public final String getSignature() {
   ConstantUtf8 c;
   c = (ConstantUtf8) constant_pool.getConstant(signature_index, CONSTANT_Utf8);
   return c.getBytes();
 }
Esempio n. 4
0
 public String getName() {
   ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
   return c.getBytes();
 }
Esempio n. 5
0
 /** Initialize from another object. */
 public ConstantUtf8(ConstantUtf8 c) {
   this(c.getBytes());
 }