Пример #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;
  }
Пример #2
0
  /**
   * Initialize with existing class.
   *
   * @param clazz JavaClass object (e.g. read from file)
   */
  public ClassGen(JavaClass clazz) {
    class_name_index = clazz.getClassNameIndex();
    superclass_name_index = clazz.getSuperclassNameIndex();
    class_name = clazz.getClassName();
    super_class_name = clazz.getSuperclassName();
    file_name = clazz.getSourceFileName();
    access_flags = clazz.getAccessFlags();
    cp = new ConstantPoolGen(clazz.getConstantPool());
    major = clazz.getMajor();
    minor = clazz.getMinor();

    Attribute[] attributes = clazz.getAttributes();
    Method[] methods = clazz.getMethods();
    Field[] fields = clazz.getFields();
    String[] interfaces = clazz.getInterfaceNames();

    for (int i = 0; i < interfaces.length; i++) addInterface(interfaces[i]);

    for (int i = 0; i < attributes.length; i++) addAttribute(attributes[i]);

    for (int i = 0; i < methods.length; i++) addMethod(methods[i]);

    for (int i = 0; i < fields.length; i++) addField(fields[i]);
  }