public static LocalVariableAnnotation getLocalVariableAnnotation(
      Method method, int local, int position1, int position2) {

    LocalVariableTable localVariableTable = method.getLocalVariableTable();
    String localName = "?";
    if (localVariableTable != null) {
      LocalVariable lv1 = localVariableTable.getLocalVariable(local, position1);
      if (lv1 == null) {
        lv1 = localVariableTable.getLocalVariable(local, position2);
        position1 = position2;
      }
      if (lv1 != null) localName = lv1.getName();
      else
        for (LocalVariable lv : localVariableTable.getLocalVariableTable()) {
          if (lv.getIndex() == local) {
            if (!localName.equals("?") && !localName.equals(lv.getName())) {
              // not a single consistent name
              localName = "?";
              break;
            }
            localName = lv.getName();
          }
        }
    }
    LineNumberTable lineNumbers = method.getLineNumberTable();
    if (lineNumbers == null) return new LocalVariableAnnotation(localName, local, position1);
    int line = lineNumbers.getSourceLine(position1);
    return new LocalVariableAnnotation(localName, local, position1, line);
  }
Ejemplo n.º 2
0
  /**
   * looks for methods that have it's parameters all follow the form arg0, arg1, arg2, or parm0,
   * parm1, parm2 etc, where the method actually has code in it
   *
   * @param cls the class to check
   */
  private void checkIDEGeneratedParmNames(JavaClass cls) {

    methods:
    for (Method m : cls.getMethods()) {
      if (!m.isPublic()) {
        continue;
      }

      String name = m.getName();
      if (Values.CONSTRUCTOR.equals(name) || Values.STATIC_INITIALIZER.equals(name)) {
        continue;
      }

      LocalVariableTable lvt = m.getLocalVariableTable();
      if (lvt == null) {
        continue;
      }

      if (m.getCode().getCode().length <= MAX_EMPTY_METHOD_SIZE) {
        continue;
      }

      int numArgs = m.getArgumentTypes().length;
      if (numArgs == 0) {
        continue;
      }

      int offset = m.isStatic() ? 0 : 1;

      for (int i = 0; i < numArgs; i++) {
        LocalVariable lv = lvt.getLocalVariable(offset + i, 0);
        if ((lv == null) || (lv.getName() == null)) {
          continue methods;
        }

        Matcher ma = ARG_PATTERN.matcher(lv.getName());
        if (!ma.matches()) {
          continue methods;
        }
      }

      bugReporter.reportBug(
          new BugInstance(
                  this,
                  BugType.IMC_IMMATURE_CLASS_IDE_GENERATED_PARAMETER_NAMES.name(),
                  NORMAL_PRIORITY)
              .addClass(cls)
              .addMethod(cls, m));
      return;
    }
  }