private void writeMethods() throws IOException {
   for (MethodInfo method : methods) {
     os.writeShort(method.getAccessFlags());
     os.writeShort(method.getNameIndex());
     os.writeShort(method.getDescriptorIndex());
     os.writeShort(method.getAttributes().length);
     attributeFacility.writeAttributes(method.getAttributes());
   }
 }
  private MethodInfo readMethod() throws IOException {
    MethodInfo methodInfo = new MethodInfo();

    methodInfo.setAccessFlags(is.readUnsignedShort());
    methodInfo.setNameIndex(is.readUnsignedShort());
    methodInfo.setDescriptorIndex(is.readUnsignedShort());
    int attributesCount = is.readUnsignedShort();

    methodInfo.setAttributes(new AbstractAttribute[attributesCount]);

    for (int i = 0; i < attributesCount; i++) {
      AbstractAttribute attribute = attributeFacility.readAttribute();
      if (isInterceptorAnnotation(attribute)) {
        methodInfo.setInterceptorClass(
            ((RuntimeInvisibleAnnotations) attribute).getInterceptorClass());
      }
      methodInfo.getAttributes()[i] = attribute;
      if (attribute.getClass() == Code.class) {
        methodInfo.setCode((Code) attribute);
      }
    }

    methodInfo.setSignature(getMethodSignature(methodInfo));
    methodInfo.setMethodName(constantPool.getUtf8(methodInfo.getNameIndex()).getString());

    return methodInfo;
  }
Пример #3
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');
  }