added scope property to BCEL reference checks

This commit is contained in:
Rick Giles 2003-06-23 21:55:07 +00:00
parent 731b581a1c
commit db428ff350
5 changed files with 52 additions and 73 deletions

View File

@ -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.

View File

@ -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());
}
}

View File

@ -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));
}
}

View File

@ -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("<init>")
|| aMethodName.equals("<clinit>"));
final String methodName = aMethod.getName();
return (super.ignore(aClassName, aMethod)
|| methodName.equals("<init>")
|| methodName.equals("<clinit>"));
}
}

View File

@ -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));
}
}