Example #1
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]);
  }
Example #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]);
  }