[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect LiteralStringComparison.java,NON
Brought to you by:
dbrosius
From: Dave B. <dbr...@us...> - 2005-09-19 04:20:29
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14551/src/com/mebigfatguy/fbcontrib/detect Added Files: LiteralStringComparison.java Log Message: Initial checkin: LSC Detector --- NEW FILE: LiteralStringComparison.java --- package com.mebigfatguy.fbcontrib.detect; import org.apache.bcel.classfile.Code; import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; import edu.umd.cs.findbugs.BytecodeScanningDetector; import edu.umd.cs.findbugs.OpcodeStack; public class LiteralStringComparison extends BytecodeScanningDetector { private BugReporter bugReporter; private OpcodeStack stack; public LiteralStringComparison(BugReporter bugReporter) { this.bugReporter = bugReporter; stack = new OpcodeStack(); } public Object clone() throws CloneNotSupportedException { return super.clone(); } public void visitCode(Code obj) { stack.resetForMethodEntry(this); super.visitCode(obj); } public void sawOpcode(int seen) { try { if ((seen == INVOKEVIRTUAL) && "java/lang/String".equals(getClassConstantOperand())) { String calledMethodName = getNameConstantOperand(); String calledMethodSig = getSigConstantOperand(); if (("equals".equals(calledMethodName) && "(Ljava/lang/Object;)Z".equals(calledMethodSig)) || ("compareTo".equals(calledMethodName) && "(Ljava/lang/String;)I".equals(calledMethodSig))) { if (stack.getStackDepth() > 0) { OpcodeStack.Item itm = stack.getStackItem(0); Object constant = itm.getConstant(); if ((constant != null) && constant.getClass().equals(String.class)) { bugReporter.reportBug( new BugInstance( this, "LSC_LITERAL_STRING_COMPARISON", LOW_PRIORITY) .addClass(this) .addMethod(this) .addSourceLine(this)); } } } } } finally { stack.sawOpcode(this, seen); } } } |