@Override
  public void sawOpcode(int seen) {
    if (ifInstructionSet.get(seen)) {
      if (getBranchTarget() == getBranchFallThrough()) {
        int priority = NORMAL_PRIORITY;

        LineNumberTable lineNumbers = getCode().getLineNumberTable();
        if (lineNumbers != null) {
          int branchLineNumber = lineNumbers.getSourceLine(getPC());
          int targetLineNumber = lineNumbers.getSourceLine(getBranchFallThrough());
          int nextLine = getNextSourceLine(lineNumbers, branchLineNumber);

          if (branchLineNumber + 1 == targetLineNumber
              || branchLineNumber == targetLineNumber && nextLine == branchLineNumber + 1)
            priority = HIGH_PRIORITY;
          else if (branchLineNumber + 2 < Math.max(targetLineNumber, nextLine))
            priority = LOW_PRIORITY;
        } else priority = LOW_PRIORITY;
        bugAccumulator.accumulateBug(
            new BugInstance(
                    this,
                    priority == HIGH_PRIORITY
                        ? "UCF_USELESS_CONTROL_FLOW_NEXT_LINE"
                        : "UCF_USELESS_CONTROL_FLOW",
                    priority)
                .addClassAndMethod(this),
            this);
      }
    }
  }
  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);
  }
Exemplo n.º 3
0
  /** Loads the line numbers for the method. */
  protected int[] loadLineNumbers(Method m) {
    Code c = m.getCode();

    if (c == null) {
      return null;
    }

    LineNumberTable lnt = c.getLineNumberTable();

    int length = code.length;
    int[] ln = new int[length];

    if (lnt == null) {
      // no line information
      return null;
    } else {
      for (int i = 0; i < length; i++) {
        try { // annoying bug when BCEL don't seem to find linenumber - pos match
          ln[i] = lnt.getSourceLine(code[i].getPosition());
        } catch (RuntimeException e) {
          System.out.print("^");
        }
      }
    }

    return ln;
  }