1 package com.puppycrawl.tools.checkstyle.bcel.checks; 2 3 import java.io.File ; 4 import java.util.BitSet ; 5 import java.util.Set ; 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 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 mLocalVariableBitSet = new BitSet (); 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 aObj) 61 { 62 mCurrentJavaClass = (JavaClass) aObj; 63 } 64 65 public void leaveSet(Set 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 checkReferences(); 88 mCurrentMethod = aMethod; 89 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 [] { 115 mCurrentJavaClass.getClassName(), 116 mCurrentMethod.getName(), 117 localVariable.getName(),}); 118 } 119 } 120 } 121 } 122 } 123 124 125 126 public IDeepVisitor getVisitor() 127 { 128 return mDeepVisitor; 129 } 130 } 131 | Popular Tags |