Update of /cvsroot/checkstyle/checkstyle/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding
In directory sc8-pr-cvs1:/tmp/cvs-serv10829/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding
Modified Files:
HiddenFieldCheck.java
Log Message:
Fixed HiddenFieldCheck error for a local variable of a static
method with same name as an instance field(bug 827713)
Index: HiddenFieldCheck.java
===================================================================
RCS file: /cvsroot/checkstyle/checkstyle/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** HiddenFieldCheck.java 8 Oct 2003 17:46:57 -0000 1.7
--- HiddenFieldCheck.java 27 Oct 2003 00:31:16 -0000 1.8
***************
*** 22,25 ****
--- 22,26 ----
import java.util.Iterator;
import java.util.LinkedList;
+ import java.util.Set;
import org.apache.commons.beanutils.ConversionException;
***************
*** 129,133 ****
if (aAST.getType() == TokenTypes.CLASS_DEF) {
//find and push fields
! final HashSet fieldSet = new HashSet(); //fields container
//add fields to container
final DetailAST objBlock =
--- 130,134 ----
if (aAST.getType() == TokenTypes.CLASS_DEF) {
//find and push fields
! final FieldFrame frame = new FieldFrame();
//add fields to container
final DetailAST objBlock =
***************
*** 138,146 ****
final String name =
child.findFirstToken(TokenTypes.IDENT).getText();
! fieldSet.add(name);
}
child = (DetailAST) child.getNextSibling();
}
! mFieldsStack.addLast(fieldSet); //push container
}
else {
--- 139,154 ----
final String name =
child.findFirstToken(TokenTypes.IDENT).getText();
! final DetailAST mods =
! child.findFirstToken(TokenTypes.MODIFIERS);
! if (mods.branchContains(TokenTypes.LITERAL_STATIC)) {
! frame.addStaticField(name);
! }
! else {
! frame.addInstanceField(name);
! }
}
child = (DetailAST) child.getNextSibling();
}
! mFieldsStack.addLast(frame); //push container
}
else {
***************
*** 174,181 ****
final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
final String name = nameAST.getText();
final Iterator it = mFieldsStack.iterator();
while (it.hasNext()) {
! final HashSet aFieldsSet = (HashSet) it.next();
! if (aFieldsSet.contains(name)
&& ((mRegexp == null) || (!getRegexp().match(name)))
&& !isIgnoredSetterParam(aAST, name)
--- 182,191 ----
final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
final String name = nameAST.getText();
+ final boolean inStatic = inStatic(aAST);
final Iterator it = mFieldsStack.iterator();
while (it.hasNext()) {
! final FieldFrame frame = (FieldFrame) it.next();
! if ((frame.containsStaticField(name)
! || (!inStatic && frame.containsInstanceField(name)))
&& ((mRegexp == null) || (!getRegexp().match(name)))
&& !isIgnoredSetterParam(aAST, name)
***************
*** 192,195 ****
--- 202,229 ----
/**
+ * Determines whether an AST node is in a static method or static
+ * initializer.
+ * @param aAST the node to check.
+ * @return true if aAST is in a static method or a static block;
+ */
+ private boolean inStatic(DetailAST aAST)
+ {
+ DetailAST parent = aAST.getParent();
+ while (parent != null) {
+ switch (parent.getType()) {
+ case TokenTypes.STATIC_INIT:
+ return true;
+ case TokenTypes.METHOD_DEF:
+ final DetailAST mods =
+ parent.findFirstToken(TokenTypes.MODIFIERS);
+ return mods.branchContains(TokenTypes.LITERAL_STATIC);
+ default:
+ parent = parent.getParent();
+ }
+ }
+ return false;
+ }
+
+ /**
* Decides whether to ignore an AST node that is the parameter of a
* setter method, where the property setter method for field 'xyz' has
***************
*** 291,294 ****
--- 325,382 ----
{
return mRegexp;
+ }
+
+ /**
+ * Holds the names of static and instance fields of a class.
+ * @author Rick Giles
+ * Describe class FieldFrame
+ * @author Rick Giles
+ * @version Oct 26, 2003
+ */
+ private class FieldFrame
+ {
+ /** set of instance field names */
+ private Set mInstanceFields = new HashSet();
+
+ /** set of static field names */
+ private Set mStaticFields = new HashSet();
+
+ /**
+ * Adds an instance field to this FieldFrame.
+ * @param aField the name of the instance field.
+ */
+ public void addInstanceField(String aField)
+ {
+ mInstanceFields.add(aField);
+ }
+
+ /**
+ * Adds a static field to this FieldFrame.
+ * @param aField the name of the instance field.
+ */
+ public void addStaticField(String aField)
+ {
+ mStaticFields.add(aField);
+ }
+
+ /**
+ * Determines whether this FieldFrame contains an instance field.
+ * @param aField the field to check.
+ * @return true if this FieldFrame contains instance field aField.
+ */
+ public boolean containsInstanceField(String aField)
+ {
+ return mInstanceFields.contains(aField);
+ }
+
+ /**
+ * Determines whether this FieldFrame contains a static field.
+ * @param aField the field to check.
+ * @return true if this FieldFrame contains static field aField.
+ */
+ public boolean containsStaticField(String aField)
+ {
+ return mStaticFields.contains(aField);
+ }
}
}
|