KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > PreferZeroLengthArrays


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

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 JavaDoc
39          public void visit(Code obj) {
40         found.clear();
41         String JavaDoc 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 JavaDoc
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