KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > bcel > checks > UnreadVariableCheck


1 package com.puppycrawl.tools.checkstyle.bcel.checks;
2
3 import java.io.File JavaDoc;
4 import java.util.BitSet JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import org.apache.bcel.classfile.Code;
8 import org.apache.bcel.classfile.JavaClass;
9 import org.apache.bcel.classfile.LocalVariable;
10 import org.apache.bcel.classfile.LocalVariableTable;
11 import org.apache.bcel.classfile.Method;
12 import org.apache.bcel.generic.LoadInstruction;
13
14 import com.puppycrawl.tools.checkstyle.bcel.AbstractCheckVisitor;
15 import com.puppycrawl.tools.checkstyle.bcel.EmptyClassFileVisitor;
16 import com.puppycrawl.tools.checkstyle.bcel.EmptyDeepVisitor;
17 import com.puppycrawl.tools.checkstyle.bcel.EmptyGenericVisitor;
18 import com.puppycrawl.tools.checkstyle.bcel.IDeepVisitor;
19
20 /**
21  * Checks for unread variables, i.e. unread formal parameters and local
22  * variables.
23  * Each class file must have a LocalVariableTable.
24  * To generate a LocalVariableTable, compile with debug information,
25  * as with option -g.
26  * @author Rick Giles
27  */

28 public class UnreadVariableCheck
29     extends AbstractCheckVisitor
30 {
31     private final DeepVisitor mDeepVisitor = new DeepVisitor();
32     
33     private final org.apache.bcel.classfile.Visitor mClassFileVisitor =
34         new ClassFileVisitor();
35     
36     private final org.apache.bcel.generic.Visitor mGenericVisitor =
37         new GenericVisitor();
38
39     private JavaClass mCurrentJavaClass = null;
40        
41     private Method mCurrentMethod = null;
42         
43     private LocalVariableTable mLocalVariableTable = null;
44     
45     private final BitSet JavaDoc mLocalVariableBitSet = new BitSet JavaDoc();
46     
47     private class DeepVisitor
48         extends EmptyDeepVisitor
49     {
50         public org.apache.bcel.classfile.Visitor getClassFileVisitor()
51         {
52             return mClassFileVisitor;
53         }
54         
55         public org.apache.bcel.generic.Visitor getGenericVisitor()
56         {
57             return mGenericVisitor;
58         }
59
60         public void visitObject(Object JavaDoc aObj)
61         {
62             mCurrentJavaClass = (JavaClass) aObj;
63         }
64          
65         public void leaveSet(Set JavaDoc aSet)
66         {
67             checkReferences();
68         }
69     }
70     
71     private class GenericVisitor
72         extends EmptyGenericVisitor
73     {
74         public void visitLoadInstruction(LoadInstruction aInstruction)
75         {
76             mLocalVariableBitSet.set(aInstruction.getIndex());
77         }
78     }
79     
80     private class ClassFileVisitor
81         extends EmptyClassFileVisitor
82     {
83         public void visitMethod(Method aMethod)
84         {
85             
86 // check references from previous method
87
checkReferences();
88             mCurrentMethod = aMethod;
89                      // update reference recording
90

91                      mLocalVariableTable = aMethod.getLocalVariableTable();
92                      mLocalVariableBitSet.clear();
93         }
94
95         public void visitLocalVariableTable(LocalVariableTable aTable)
96         {
97
98   
99         }
100     }
101     
102      private void checkReferences()
103      {
104         if (mLocalVariableTable != null) {
105            for (int i = 0; i < mLocalVariableTable.getLength(); i++) {
106                 if (!mLocalVariableBitSet.get(i)) {
107                     final LocalVariable localVariable =
108                         mLocalVariableTable.getLocalVariable(i);
109                     if (localVariable != null
110                         && !localVariable.getName().equals("this"))
111                     {
112                         log(0,
113                             "unread.variable",
114                             new Object JavaDoc[] {
115                                 mCurrentJavaClass.getClassName(),
116                                 mCurrentMethod.getName(),
117                                 localVariable.getName(),});
118                     }
119                 }
120             }
121         }
122      }
123
124     
125     /** @see com.puppycrawl.tools.checkstyle.bcel.AbstractCheckVisitor */
126     public IDeepVisitor getVisitor()
127     {
128         return mDeepVisitor;
129     }
130 }
131
Popular Tags