醉君惜红颜 2015-07-15 10:17 采纳率: 0%
浏览 1969

findbugs自定义规则怎么确定for循环的边界

我主要是要检测for循环里面是否进行了数据交换,但是不知道该怎么确定for循环的边界。
package edu.umd.cs.findbugs.detect;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector;
import org.apache.bcel.classfile.Code;

public class ForNotHasDataExchange extends OpcodeStackDetector {
public BugReporter bugReporter;
public int logBlockStart = -1;
public int logBlockEnd = -1;
public int forPC = -1;
public int astorePC = -1;
public int gotoPC = -2;

public ForNotHasDataExchange(BugReporter bugReporter) {
    this.bugReporter = bugReporter;
}

public void visit(Code obj) {
    this.forPC = -1;
    this.logBlockStart = -1;
    this.logBlockEnd = -1;
    this.astorePC = -1;
    this.gotoPC = -2;
    super.visit(obj);
}

public void sawOpcode(int seen) {
    switch (seen) {
    case ASTORE_0 :
        this.astorePC = getPC();
        break;
    case ASTORE_1 :
        this.astorePC = getPC();
        break;
    case ASTORE_2 :
        this.astorePC = getPC();
        break;
    case ASTORE_3 :
        this.astorePC = getPC();
        break;
    case ASTORE:
        this.astorePC = getPC();
    }

    if (seen == GOTO) {
        this.gotoPC = getPC();

        if ((this.astorePC != -1) && (this.astorePC + 1 != this.gotoPC)) {
            this.forPC = getPC();
            this.logBlockStart = getBranchFallThrough();
            this.logBlockEnd = getBranchTarget();
            return;
        }
    }
    if (seen == INVOKEVIRTUAL) {
        String classConstant = getClassConstantOperand();
        String nameConstant = getNameConstantOperand();
        String sigConstant = getSigConstantOperand();

        if ((((!"getSession".equals(nameConstant)) || (!"()Lorg/hibernate/Session;"
                .equals(sigConstant))))
                && (((!"getJdbcTemplate".equals(nameConstant)) || (!"()Lorg/springframework/jdbc/core/JdbcTemplate;"
                        .equals(sigConstant))))
                && (((!"getConnection".equals(nameConstant)) || (!"()Ljava/sql/Connection;"
                        .equals(sigConstant))))) {
            return;
        }
        if ((getPC() >= this.logBlockStart)
                && (getPC() <= this.logBlockEnd)
                && (this.logBlockStart != -1) && (this.logBlockEnd != -1)) {
            BugInstance bug = new BugInstance(this, "CHECK_DATA_EXCHANGE",
                    HIGH_PRIORITY).addClassAndMethod(this).addSourceLine(this, getPC());
            bug.addInt(getPC());
            this.bugReporter.reportBug(bug);
            return;
        }
        return;
    }
}

}


目前做到这种程度了,但是不准确,也会确定到else和switch里面

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度