KickJava   Java API By Example, From Geeks To Geeks.

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


1 //Tested with BCEL-5.1
2
//http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
3

4 package com.puppycrawl.tools.checkstyle.bcel.checks;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import org.apache.bcel.classfile.Field;
10 import org.apache.bcel.classfile.JavaClass;
11
12 import com.puppycrawl.tools.checkstyle.api.Scope;
13 import com.puppycrawl.tools.checkstyle.bcel.ReferenceVisitor;
14 import com.puppycrawl.tools.checkstyle.bcel.classfile.FieldDefinition;
15 import com.puppycrawl.tools.checkstyle.bcel.classfile.JavaClassDefinition;
16
17 /**
18  * Checks for unread, non-final fields
19  * @author Rick Giles
20  */

21 public class UnreadFieldCheck
22     extends AbstractReferenceCheck
23 {
24     /** @see AbstractReferenceCheck */
25     public void setScope(String JavaDoc aFrom)
26     {
27         super.setScope(aFrom);
28         ((ReferenceVisitor) getVisitor()).addFieldScope(
29             Scope.getInstance(aFrom));
30     }
31
32     /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
33     public void leaveSet(Set JavaDoc aJavaClasses)
34     {
35         final Iterator JavaDoc it = aJavaClasses.iterator();
36         while (it.hasNext()) {
37             final JavaClass javaClass = (JavaClass) it.next();
38             final String JavaDoc className = javaClass.getClassName();
39             final JavaClassDefinition classDef = findJavaClassDef(javaClass);
40             final FieldDefinition[] fieldDefs = classDef.getFieldDefs();
41             for (int i = 0; i < fieldDefs.length; i++) {
42                 if (fieldDefs[i].getReadReferenceCount() == 0) {
43                     final Field field = fieldDefs[i].getField();
44                     if (!field.isFinal()
45                         && (!ignore(className, field))
46                         )
47                     {
48                         log(
49                             javaClass,
50                             0,
51                             "unread.field",
52                             new Object JavaDoc[] {fieldDefs[i]});
53                     }
54                 }
55             }
56         }
57     }
58 }
59
Popular Tags