1 19 20 package edu.umd.cs.findbugs.detect; 21 22 23 import edu.umd.cs.findbugs.*; 24 import java.util.*; 25 import org.apache.bcel.classfile.Code; 26 27 public class PreferZeroLengthArrays extends BytecodeScanningDetector implements StatelessDetector { 28 boolean nullOnTOS = false; 29 private BugReporter bugReporter; 30 31 public PreferZeroLengthArrays(BugReporter bugReporter) { 32 this.bugReporter = bugReporter; 33 } 34 35 36 37 Collection<SourceLineAnnotation> found = new LinkedList<SourceLineAnnotation>(); 38 @Override 39 public void visit(Code obj) { 40 found.clear(); 41 String returnType = getMethodSig().substring(getMethodSig().indexOf(")") + 1); 42 if (returnType.startsWith("[")) { 43 nullOnTOS = false; 44 super.visit(obj); 45 if (!found.isEmpty()) { 46 BugInstance bug = new BugInstance(this, "PZLA_PREFER_ZERO_LENGTH_ARRAYS", LOW_PRIORITY) 47 .addClassAndMethod(this); 48 for(SourceLineAnnotation s : found) 49 bug.add(s); 50 bugReporter.reportBug(bug); 51 found.clear(); 52 } 53 } 54 } 55 56 57 @Override 58 public void sawOpcode(int seen) { 59 60 switch (seen) { 61 case ACONST_NULL: 62 nullOnTOS = true; 63 return; 64 case ARETURN: 65 if (nullOnTOS) { 66 SourceLineAnnotation sourceLineAnnotation = 67 SourceLineAnnotation.fromVisitedInstruction(getClassContext(), this, getPC()); 68 if (sourceLineAnnotation != null) 69 found.add(sourceLineAnnotation); 70 } 71 72 break; 73 } 74 nullOnTOS = false; 75 } 76 } 77 | Popular Tags |