KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.bcel.classfile.FieldOrMethod;
7 import org.apache.bcel.classfile.JavaClass;
8 import org.apache.commons.beanutils.ConversionException;
9 import java.util.regex.Pattern JavaDoc;
10 import java.util.regex.PatternSyntaxException JavaDoc;
11
12 import com.puppycrawl.tools.checkstyle.api.Scope;
13 import com.puppycrawl.tools.checkstyle.api.Utils;
14 import com.puppycrawl.tools.checkstyle.bcel.AbstractCheckVisitor;
15 import com.puppycrawl.tools.checkstyle.bcel.IDeepVisitor;
16 import com.puppycrawl.tools.checkstyle.bcel.ReferenceVisitor;
17 import com.puppycrawl.tools.checkstyle.bcel.classfile.JavaClassDefinition;
18 import com.puppycrawl.tools.checkstyle.bcel.classfile.ReferenceDAO;
19
20 /**
21  * Abstract class for checks that require reference information.
22  * @author Rick Giles
23  */

24 public abstract class AbstractReferenceCheck
25     extends AbstractCheckVisitor
26 {
27     /** the scope for recorded references */
28     private Scope mScope = Scope.PRIVATE;
29
30     /** the regexp to match class names against */
31     private Pattern JavaDoc mIgnoreClassNameRegexp;
32
33     /** the regexp to match names against */
34     private Pattern JavaDoc mIgnoreNameRegexp;
35
36     /**
37      * Creates a <code>AbstractReferenceCheck</code>.
38      *
39      */

40     public AbstractReferenceCheck()
41     {
42         setIgnoreClassName("^$");
43         setIgnoreName("^$");
44     }
45
46     /**
47      * Sets the scope for recorded references.
48      * @param aScopeName the scope for recorded references.
49      */

50     public void setScope(String JavaDoc aScopeName)
51     {
52         mScope = Scope.getInstance(aScopeName);
53     }
54
55     /**
56      * Determines whether a class name, and field or method should be ignored.
57      * @param aClassName the class name to consider.
58      * @param aFieldOrMethod the field or method to consider.
59      * @return true if aClassName, and field or method should be ignored.
60      */

61     protected boolean ignore(String JavaDoc aClassName, FieldOrMethod aFieldOrMethod)
62     {
63         final String JavaDoc fieldOrMethodName = aFieldOrMethod.getName();
64         return (!equalScope(aFieldOrMethod)
65                 || mIgnoreClassNameRegexp.matcher(aClassName).matches()
66             || mIgnoreNameRegexp.matcher(fieldOrMethodName).matches());
67     }
68
69     /**
70      * Tests whether the scope of a field or method is compatible
71      * with the scope of this check. References for compatible
72      * fields or methods should be checked.
73      * @param aFieldOrMethod the field or method to check.
74      * @return true if the scope of aFieldOrMethod is compatible
75      * with the scope of this check.
76      */

77     private boolean equalScope(FieldOrMethod aFieldOrMethod)
78     {
79         if (aFieldOrMethod.isPrivate()) {
80             return (mScope == Scope.PRIVATE);
81         }
82         else if (aFieldOrMethod.isProtected()) {
83             return (mScope == Scope.PROTECTED);
84         }
85         else if (aFieldOrMethod.isPublic()) {
86             return (mScope == Scope.PUBLIC);
87         }
88         else {
89             return (mScope == Scope.PACKAGE);
90         }
91     }
92
93
94     /**
95      * Set the ignore class name to the specified regular expression.
96      * @param aFormat a <code>String</code> value
97      * @throws ConversionException unable to parse aFormat
98      */

99     public void setIgnoreClassName(String JavaDoc aFormat)
100         throws ConversionException
101     {
102         try {
103             mIgnoreClassNameRegexp = Utils.getPattern(aFormat);
104         }
105         catch (PatternSyntaxException JavaDoc e) {
106             throw new ConversionException("unable to parse " + aFormat, e);
107         }
108     }
109
110     /**
111      * Set the ignore name format to the specified regular expression.
112      * @param aFormat a <code>String</code> value
113      * @throws ConversionException unable to parse aFormat
114      */

115     public void setIgnoreName(String JavaDoc aFormat)
116         throws ConversionException
117     {
118         try {
119             mIgnoreNameRegexp = Utils.getPattern(aFormat);
120         }
121         catch (PatternSyntaxException JavaDoc e) {
122             throw new ConversionException("unable to parse " + aFormat, e);
123         }
124     }
125
126     /** @see com.puppycrawl.tools.checkstyle.bcel.AbstractCheckVisitor */
127     public IDeepVisitor getVisitor()
128     {
129         return ReferenceVisitor.getInstance();
130     }
131
132     /**
133      * Gets the DAO that manages references for this check.
134      * @return the the DAO that manages references for this check.
135      */

136     public ReferenceDAO getReferenceDAO()
137     {
138         return ((ReferenceVisitor) getVisitor()).getReferenceDAO();
139     }
140
141     /**
142      * Finds a JavaClassDefinition for a JavaClass.
143      * @param aJavaClass the JavaClass.
144      * @return the JavaClassDefinition for aJavaClass.
145      */

146     protected JavaClassDefinition findJavaClassDef(JavaClass aJavaClass)
147     {
148         return getReferenceDAO().findJavaClassDef(aJavaClass);
149     }
150 }
151
Popular Tags