问题描述
- 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里面
解决方案
http://blog.csdn.net/jdsjlzx/article/details/21472253
http://www.cnblogs.com/musicmovie/p/3682729.html
时间: 2024-10-06 10:17:24