Пример #1
0
  /**
   * Creates a public setter method. The setter method assigns the value of the first parameter to
   * the specified field in the class to which this method is added. The created method is not
   * static even if the field is static. You may not change it to be static by <code>setModifiers()
   * </code> in <code>CtBehavior</code>.
   *
   * @param methodName the name of the setter
   * @param field the field accessed.
   */
  public static CtMethod setter(String methodName, CtField field) throws CannotCompileException {
    FieldInfo finfo = field.getFieldInfo2();
    String fieldType = finfo.getDescriptor();
    String desc = "(" + fieldType + ")V";
    ConstPool cp = finfo.getConstPool();
    MethodInfo minfo = new MethodInfo(cp, methodName, desc);
    minfo.setAccessFlags(AccessFlag.PUBLIC);

    Bytecode code = new Bytecode(cp, 3, 3);
    try {
      String fieldName = finfo.getName();
      if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
        code.addAload(0);
        code.addLoad(1, field.getType());
        code.addPutfield(Bytecode.THIS, fieldName, fieldType);
      } else {
        code.addLoad(1, field.getType());
        code.addPutstatic(Bytecode.THIS, fieldName, fieldType);
      }

      code.addReturn(null);
    } catch (NotFoundException e) {
      throw new CannotCompileException(e);
    }

    minfo.setCodeAttribute(code.toCodeAttribute());
    return new CtMethod(minfo, field.getDeclaringClass());
  }
Пример #2
0
  /** Prints the contents of a class file. */
  public static void print(ClassFile cf, PrintWriter out) {
    List<?> list;
    int n;

    /*
     * 0x0020 (SYNCHRONIZED) means ACC_SUPER if the modifiers are of a
     * class.
     */
    int mod = AccessFlag.toModifier(cf.getAccessFlags() & ~AccessFlag.SYNCHRONIZED);
    out.println(
        "major: "
            + cf.major
            + ", minor: "
            + cf.minor
            + " modifiers: "
            + Integer.toHexString(cf.getAccessFlags()));
    out.println(
        Modifier.toString(mod) + " class " + cf.getName() + " extends " + cf.getSuperclass());

    String[] infs = cf.getInterfaces();
    if (infs != null && infs.length > 0) {
      out.print("    implements ");
      out.print(infs[0]);
      for (int i = 1; i < infs.length; ++i) {
        out.print(", " + infs[i]);
      }

      out.println();
    }

    out.println();
    list = cf.getFields();
    n = list.size();
    for (int i = 0; i < n; ++i) {
      FieldInfo finfo = (FieldInfo) list.get(i);
      int acc = finfo.getAccessFlags();
      out.println(
          Modifier.toString(AccessFlag.toModifier(acc))
              + " "
              + finfo.getName()
              + "\t"
              + finfo.getDescriptor());
      printAttributes(finfo.getAttributes(), out, 'f');
    }

    out.println();
    list = cf.getMethods();
    n = list.size();
    for (int i = 0; i < n; ++i) {
      MethodInfo minfo = (MethodInfo) list.get(i);
      int acc = minfo.getAccessFlags();
      out.println(
          Modifier.toString(AccessFlag.toModifier(acc))
              + " "
              + minfo.getName()
              + "\t"
              + minfo.getDescriptor());
      printAttributes(minfo.getAttributes(), out, 'm');
      out.println();
    }

    out.println();
    printAttributes(cf.getAttributes(), out, 'c');
  }