KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005-2006 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 java.util.BitSet JavaDoc;
24
25 import org.apache.bcel.classfile.Code;
26
27 import edu.umd.cs.findbugs.BugInstance;
28 import edu.umd.cs.findbugs.BugReporter;
29 import edu.umd.cs.findbugs.BytecodeScanningDetector;
30 import edu.umd.cs.findbugs.LocalVariableAnnotation;
31 import edu.umd.cs.findbugs.StatelessDetector;
32
33 public class FindLocalSelfAssignment2 extends BytecodeScanningDetector implements StatelessDetector {
34
35     private BugReporter bugReporter;
36     private int previousLoadOf = -1;
37     private int previousGotoTarget;
38     private int gotoCount;
39     public FindLocalSelfAssignment2(BugReporter bugReporter) {
40         this.bugReporter = bugReporter;
41     }
42
43     private BitSet JavaDoc previousStores = new BitSet JavaDoc();
44
45
46     @Override JavaDoc
47        public void visit(Code obj) {
48         previousLoadOf = -1;
49         previousGotoTarget = -1;
50         gotoCount = 0;
51         previousStores.clear();
52         super.visit(obj);
53     }
54
55
56     @Override JavaDoc
57     public void sawOpcode(int seen) {
58         if (seen == GOTO) {
59             previousGotoTarget = getBranchTarget();
60             gotoCount++;
61             if (previousGotoTarget < getPC())
62                 previousLoadOf = -1;
63         } else {
64             if (isRegisterLoad())
65                 previousLoadOf = getRegisterOperand();
66             else {
67                 if (isRegisterStore()) {
68                     if (previousLoadOf == getRegisterOperand() && gotoCount < 2 && getPC() != previousGotoTarget) {
69                     int priority = NORMAL_PRIORITY;
70                     String JavaDoc methodName = getMethodName();
71                     if (methodName.equals("<init>") || methodName.startsWith("set") && getCode().getCode().length <= 5 ||
72                             !previousStores.get(getRegisterOperand())) priority = HIGH_PRIORITY;
73                        bugReporter.reportBug(
74                     new BugInstance(this,
75                             "SA_LOCAL_SELF_ASSIGNMENT", priority)
76                                             .addClassAndMethod(this)
77                                             .add(LocalVariableAnnotation.getLocalVariableAnnotation(getMethod(), getRegisterOperand(), getPC(), getPC()))
78                                             
79                                             .addSourceLine(this));
80                     }
81                     previousStores.set(getRegisterOperand());
82                 }
83                 
84     
85                 previousLoadOf = -1;
86                 gotoCount = 0;
87             }
88         }
89     }
90 }
91
Popular Tags