Java Code Examples for edu.umd.cs.findbugs.SourceLineAnnotation#getEndLine()

The following examples show how to use edu.umd.cs.findbugs.SourceLineAnnotation#getEndLine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FindBugsParser.java    From analysis-model with MIT License 6 votes vote down vote up
private void setAffectedLines(final BugInstance warning, final IssueBuilder builder,
        final LineRange primary) {
    Iterator<BugAnnotation> annotationIterator = warning.annotationIterator();
    LineRangeList lineRanges = new LineRangeList();
    while (annotationIterator.hasNext()) {
        BugAnnotation bugAnnotation = annotationIterator.next();
        if (bugAnnotation instanceof SourceLineAnnotation) {
            SourceLineAnnotation annotation = (SourceLineAnnotation) bugAnnotation;
            LineRange lineRange = new LineRange(annotation.getStartLine(), annotation.getEndLine());
            if (!lineRanges.contains(lineRange) && !primary.equals(lineRange)) {
                lineRanges.add(lineRange);
            }
        }
    }
    builder.setLineRanges(lineRanges);
}
 
Example 2
Source File: SourceCodeDisplay.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void run() {
    frame.getSourceCodeTextPane().setEditorKit(src.getEditorKit());
    StyledDocument document = src.getDocument();
    frame.getSourceCodeTextPane().setDocument(document);
    String sourceFile = mySourceLine.getSourceFile();
    if (sourceFile == null || "<Unknown>".equals(sourceFile)) {
        sourceFile = mySourceLine.getSimpleClassName();
    }
    int startLine = mySourceLine.getStartLine();
    int endLine = mySourceLine.getEndLine();
    frame.setSourceTab(sourceFile + " in " + mySourceLine.getPackageName(), myBug);

    int originLine = (startLine + endLine) / 2;
    LinkedList<Integer> otherLines = new LinkedList<>();
    // show(frame.getSourceCodeTextPane(), document,
    // thisSource);
    for (Iterator<BugAnnotation> i = myBug.annotationIterator(); i.hasNext();) {
        BugAnnotation annotation = i.next();
        if (annotation instanceof SourceLineAnnotation) {
            SourceLineAnnotation sourceAnnotation = (SourceLineAnnotation) annotation;
            if (sourceAnnotation != mySourceLine) {
                // show(frame.getSourceCodeTextPane(),
                // document, sourceAnnotation);
                int otherLine = sourceAnnotation.getStartLine();
                if (otherLine > originLine) {
                    otherLine = sourceAnnotation.getEndLine();
                }
                otherLines.add(otherLine);
            }
        }
    }

    if (startLine >= 0 && endLine >= 0) {
        frame.getSourceCodeTextPane().scrollLinesToVisible(startLine, endLine, otherLines);
    }
}
 
Example 3
Source File: RepeatedConditionals.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (hasSideEffect(seen)) {
        reset();
    } else if (stack.getStackDepth() == 0) {
        if (emptyStackLocations.size() > 1) {
            for (int n = 1; n <= emptyStackLocations.size() / 2; n++) {
                int first = emptyStackLocations.get(emptyStackLocations.size() - 2 * n);
                int second = emptyStackLocations.get(emptyStackLocations.size() - n);
                int third = getPC();
                if (third - second == second - first) {
                    int endOfFirstSegment = prevOpcodeLocations.get(emptyStackLocations.size() - n);
                    int endOfSecondSegment = oldPC;
                    int opcodeAtEndOfFirst = getCodeByte(endOfFirstSegment);
                    int opcodeAtEndOfSecond = getCodeByte(endOfSecondSegment);

                    if (!isBranch(opcodeAtEndOfFirst) || !isBranch(opcodeAtEndOfSecond)) {
                        continue;
                    }
                    if (opcodeAtEndOfFirst == Opcodes.GOTO || opcodeAtEndOfSecond == Opcodes.GOTO) {
                        continue;
                    }
                    if (opcodeAtEndOfFirst != opcodeAtEndOfSecond
                            && !areOppositeBranches(opcodeAtEndOfFirst, opcodeAtEndOfSecond)) {
                        continue;
                    }

                    if (first == endOfFirstSegment) {
                        continue;
                    }
                    Integer firstTarget = branchTargets.get(endOfFirstSegment);
                    Integer secondTarget = branchTargets.get(endOfSecondSegment);
                    if (firstTarget == null || secondTarget == null) {
                        continue;
                    }
                    if (firstTarget >= second && firstTarget <= endOfSecondSegment) {
                        // first jumps inside second
                        continue;
                    }
                    boolean identicalCheck = firstTarget.equals(secondTarget) && opcodeAtEndOfFirst == opcodeAtEndOfSecond
                            || (firstTarget.intValue() == getPC() && opcodeAtEndOfFirst != opcodeAtEndOfSecond);
                    if (!compareCode(first, endOfFirstSegment, second, endOfSecondSegment, !identicalCheck)) {
                        continue;
                    }
                    SourceLineAnnotation firstSourceLine = SourceLineAnnotation.fromVisitedInstructionRange(getClassContext(),
                            this, first, endOfFirstSegment - 1);
                    SourceLineAnnotation secondSourceLine = SourceLineAnnotation.fromVisitedInstructionRange(getClassContext(),
                            this, second, endOfSecondSegment - 1);

                    int priority = HIGH_PRIORITY;
                    if (firstSourceLine.getStartLine() == -1 || firstSourceLine.getStartLine() != secondSourceLine.getEndLine()) {
                        priority++;
                    }
                    if (stack.isJumpTarget(second)) {
                        priority++;
                    }
                    if (!identicalCheck) {
                        // opposite checks
                        priority += 2;
                    }

                    BugInstance bug = new BugInstance(this, "RpC_REPEATED_CONDITIONAL_TEST", priority).addClassAndMethod(this)
                            .add(firstSourceLine).add(secondSourceLine);
                    bugReporter.reportBug(bug);
                }
            }
        }
        emptyStackLocations.add(getPC());
        prevOpcodeLocations.add(oldPC);

    }
    oldPC = getPC();
}