Example #1
0
  public Method lookupMethod(
      CtClass clazz,
      CtClass currentClass,
      MethodInfo current,
      String methodName,
      int[] argTypes,
      int[] argDims,
      String[] argClassNames)
      throws CompileError {
    Method maybe = null;
    // to enable the creation of a recursively called method
    if (current != null && clazz == currentClass)
      if (current.getName().equals(methodName)) {
        int res = compareSignature(current.getDescriptor(), argTypes, argDims, argClassNames);
        if (res != NO) {
          Method r = new Method(clazz, current, res);
          if (res == YES) return r;
          else maybe = r;
        }
      }

    Method m = lookupMethod(clazz, methodName, argTypes, argDims, argClassNames, maybe != null);
    if (m != null) return m;
    else return maybe;
  }
Example #2
0
  private Method lookupMethod(
      CtClass clazz,
      String methodName,
      int[] argTypes,
      int[] argDims,
      String[] argClassNames,
      boolean onlyExact)
      throws CompileError {
    Method maybe = null;
    ClassFile cf = clazz.getClassFile2();
    // If the class is an array type, the class file is null.
    // If so, search the super class java.lang.Object for clone() etc.
    if (cf != null) {
      List list = cf.getMethods();
      int n = list.size();
      for (int i = 0; i < n; ++i) {
        MethodInfo minfo = (MethodInfo) list.get(i);
        if (minfo.getName().equals(methodName)) {
          int res = compareSignature(minfo.getDescriptor(), argTypes, argDims, argClassNames);
          if (res != NO) {
            Method r = new Method(clazz, minfo, res);
            if (res == YES) return r;
            else if (maybe == null || maybe.notmatch > res) maybe = r;
          }
        }
      }
    }

    if (onlyExact) maybe = null;
    else onlyExact = maybe != null;

    int mod = clazz.getModifiers();
    boolean isIntf = Modifier.isInterface(mod);
    try {
      // skip searching java.lang.Object if clazz is an interface type.
      if (!isIntf) {
        CtClass pclazz = clazz.getSuperclass();
        if (pclazz != null) {
          Method r = lookupMethod(pclazz, methodName, argTypes, argDims, argClassNames, onlyExact);
          if (r != null) return r;
        }
      }
    } catch (NotFoundException e) {
    }

    if (isIntf || Modifier.isAbstract(mod))
      try {
        CtClass[] ifs = clazz.getInterfaces();
        int size = ifs.length;
        for (int i = 0; i < size; ++i) {
          Method r = lookupMethod(ifs[i], methodName, argTypes, argDims, argClassNames, onlyExact);
          if (r != null) return r;
        }

        if (isIntf) {
          // finally search java.lang.Object.
          CtClass pclazz = clazz.getSuperclass();
          if (pclazz != null) {
            Method r =
                lookupMethod(pclazz, methodName, argTypes, argDims, argClassNames, onlyExact);
            if (r != null) return r;
          }
        }
      } catch (NotFoundException e) {
      }

    return maybe;
  }
  /** 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');
  }