From db428ff350e76d0609e192fc48314cf226677fde Mon Sep 17 00:00:00 2001 From: Rick Giles Date: Mon, 23 Jun 2003 21:55:07 +0000 Subject: [PATCH] added scope property to BCEL reference checks --- .../bcel/checks/AbstractReferenceCheck.java | 49 +++++++++++++++++-- .../bcel/checks/UnreadFieldCheck.java | 15 +----- .../bcel/checks/UnreadPrivateFieldCheck.java | 20 -------- .../bcel/checks/UnusedMethodCheck.java | 21 ++------ .../bcel/checks/UnusedPrivateMethodCheck.java | 20 -------- 5 files changed, 52 insertions(+), 73 deletions(-) delete mode 100644 contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadPrivateFieldCheck.java delete mode 100644 contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedPrivateMethodCheck.java diff --git a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/AbstractReferenceCheck.java b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/AbstractReferenceCheck.java index a8566c922..df6f6560d 100644 --- a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/AbstractReferenceCheck.java +++ b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/AbstractReferenceCheck.java @@ -3,11 +3,13 @@ package com.puppycrawl.tools.checkstyle.bcel.checks; +import org.apache.bcel.classfile.FieldOrMethod; import org.apache.bcel.classfile.JavaClass; import org.apache.commons.beanutils.ConversionException; import org.apache.regexp.RE; import org.apache.regexp.RESyntaxException; +import com.puppycrawl.tools.checkstyle.api.Scope; import com.puppycrawl.tools.checkstyle.api.Utils; import com.puppycrawl.tools.checkstyle.bcel.AbstractCheckVisitor; import com.puppycrawl.tools.checkstyle.bcel.IDeepVisitor; @@ -22,6 +24,9 @@ import com.puppycrawl.tools.checkstyle.bcel.classfile.ReferenceDAO; public abstract class AbstractReferenceCheck extends AbstractCheckVisitor { + /** the scope for recorded references */ + private Scope mScope = Scope.PRIVATE; + /** the regexp to match class names against */ private RE mIgnoreClassNameRegexp; @@ -39,16 +44,52 @@ public abstract class AbstractReferenceCheck } /** - * Determines whether a class name and name should be ignored. + * Sets the scope for recorded references. + * @param aScopeName the scope for recorded references. + */ + public void setScope(String aScopeName) + { + mScope = Scope.getInstance(aScopeName); + } + + /** + * Determines whether a class name and field or method should be ignored. * @param aClassName the class name. * @param aName the name. * @return true if aClassName and aName should be ignored. */ - protected boolean ignore(String aClassName, String aName) + protected boolean ignore(String aClassName, FieldOrMethod aFieldOrMethod) { - return (mIgnoreClassNameRegexp.match(aClassName) - || mIgnoreNameRegexp.match(aName)); + final String fieldOrMethodName = aFieldOrMethod.getName(); + return (!equalScope(aFieldOrMethod) + || mIgnoreClassNameRegexp.match(aClassName) + || mIgnoreNameRegexp.match(fieldOrMethodName)); } + + /** + * Tests whether the scope of a field or method is compatible + * with the scope of this check. References for compatible + * fields or methods should be checked. + * @param aFieldOrMethod the field or method to check. + * @return true if the scope of aFieldOrMethod is compatible + * with the scope of this check. + */ + private boolean equalScope(FieldOrMethod aFieldOrMethod) + { + if (aFieldOrMethod.isPrivate()) { + return (mScope == Scope.PRIVATE); + } + else if (aFieldOrMethod.isProtected()) { + return (mScope == Scope.PROTECTED); + } + else if (aFieldOrMethod.isPublic()) { + return (mScope == Scope.PUBLIC); + } + else { + return (mScope == Scope.PACKAGE); + } + } + /** * Set the ignore class name to the specified regular expression. diff --git a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadFieldCheck.java b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadFieldCheck.java index f589749c6..ae341750c 100644 --- a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadFieldCheck.java +++ b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadFieldCheck.java @@ -32,7 +32,8 @@ public class UnreadFieldCheck if (fieldDefs[i].getReadReferenceCount() == 0) { final Field field = fieldDefs[i].getField(); if (!field.isFinal() - && (!ignore(className, field))) + && (!ignore(className, field)) + ) { log( 0, @@ -43,16 +44,4 @@ public class UnreadFieldCheck } } } - - /** - * Determines whether a class name and Field should be ignored. - * Normally the Field is a Field of the named class. - * @param aClassName the class name. - * @param aField the Field. - * @return true if aClassName and aField should be ignored. - */ - protected boolean ignore(String aClassName, Field aField) - { - return ignore(aClassName, aField.getName()); - } } \ No newline at end of file diff --git a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadPrivateFieldCheck.java b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadPrivateFieldCheck.java deleted file mode 100644 index 2045cc0b1..000000000 --- a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnreadPrivateFieldCheck.java +++ /dev/null @@ -1,20 +0,0 @@ -//Tested with BCEL-5.1 -//http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/ - -package com.puppycrawl.tools.checkstyle.bcel.checks; - -import org.apache.bcel.classfile.Field; - -/** - * Checks for unread, non-final private fields - * @author Rick Giles - */ -public class UnreadPrivateFieldCheck - extends UnreadFieldCheck -{ - /** @see UnreadFieldCheck */ - protected boolean ignore(String aClassName, Field aField) - { - return (!aField.isPrivate() || super.ignore(aClassName, aField)); - } -} \ No newline at end of file diff --git a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedMethodCheck.java b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedMethodCheck.java index cc0f2f464..f432e0791 100644 --- a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedMethodCheck.java +++ b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedMethodCheck.java @@ -44,23 +44,12 @@ public class UnusedMethodCheck } } - /** - * Determines whether a class name and Method should be ignored. - * Normally the Method is a Method of the named class. - * @param aClassName the class name. - * @param aMethod the Method. - * @return true if aClassName and aMethod should be ignored. - */ - protected boolean ignore(String aClassName, Method aMethod) - { - return ignore(aClassName, aMethod.getName()); - } - /** @see AbstractReferenceCheck */ - public boolean ignore(String aClassName, String aMethodName) + public boolean ignore(String aClassName, Method aMethod) { - return (super.ignore(aClassName, aMethodName) - || aMethodName.equals("") - || aMethodName.equals("")); + final String methodName = aMethod.getName(); + return (super.ignore(aClassName, aMethod) + || methodName.equals("") + || methodName.equals("")); } } \ No newline at end of file diff --git a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedPrivateMethodCheck.java b/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedPrivateMethodCheck.java deleted file mode 100644 index ac1209f3b..000000000 --- a/contrib/bcel/src/checkstyle/com/puppycrawl/tools/checkstyle/bcel/checks/UnusedPrivateMethodCheck.java +++ /dev/null @@ -1,20 +0,0 @@ -//Tested with BCEL-5.1 -//http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/ - -package com.puppycrawl.tools.checkstyle.bcel.checks; - -import org.apache.bcel.classfile.Method; - -/** - * Checks for unused private methods - * @author Rick Giles - */ -public class UnusedPrivateMethodCheck - extends UnusedMethodCheck -{ - /** @see UnusedMethodCheck */ - protected boolean ignore(String aClassName, Method aMethod) - { - return (!aMethod.isPrivate() || super.ignore(aClassName, aMethod)); - } -} \ No newline at end of file