From 49d100f325752b15bbee56bfcf99dac223144639 Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Tue, 31 May 2005 16:16:29 +0000 Subject: [PATCH] small refactoring for HiddenFieldCheck --- .../checks/coding/HiddenFieldCheck.java | 107 +++++++++--------- .../tools/checkstyle/InputHiddenField.java | 5 + 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java index ffbb613bb..07a4f1d58 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java @@ -128,53 +128,49 @@ public class HiddenFieldCheck /** @see com.puppycrawl.tools.checkstyle.api.Check */ public void visitToken(DetailAST aAST) { + if (aAST.getType() == TokenTypes.VARIABLE_DEF + || aAST.getType() == TokenTypes.PARAMETER_DEF) + { + processVariable(aAST); + return; + } + //A more thorough check of enum constant class bodies is //possible (checking for hidden fields against the enum //class body in addition to enum constant class bodies) //but not attempted as it seems out of the scope of this //check. - if ((aAST.getType() == TokenTypes.CLASS_DEF) - || (aAST.getType() == TokenTypes.ENUM_DEF) - || (aAST.getType() == TokenTypes.ENUM_CONSTANT_DEF)) - { - final DetailAST typeMods = - aAST.findFirstToken(TokenTypes.MODIFIERS); - final boolean isStaticInnerType = - (typeMods == null) - ? false - : typeMods.branchContains(TokenTypes.LITERAL_STATIC); - final FieldFrame frame = - new FieldFrame(mCurrentFrame, isStaticInnerType); + final DetailAST typeMods = aAST.findFirstToken(TokenTypes.MODIFIERS); + final boolean isStaticInnerType = + (typeMods == null) + ? false + : typeMods.branchContains(TokenTypes.LITERAL_STATIC); + final FieldFrame frame = + new FieldFrame(mCurrentFrame, isStaticInnerType); - //add fields to container - final DetailAST objBlock = - aAST.findFirstToken(TokenTypes.OBJBLOCK); - // enum constants may not have bodies - if (objBlock != null) { - DetailAST child = (DetailAST) objBlock.getFirstChild(); - while (child != null) { - if (child.getType() == TokenTypes.VARIABLE_DEF) { - 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); - } + //add fields to container + final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK); + // enum constants may not have bodies + if (objBlock != null) { + DetailAST child = (DetailAST) objBlock.getFirstChild(); + while (child != null) { + if (child.getType() == TokenTypes.VARIABLE_DEF) { + 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(); } + child = (DetailAST) child.getNextSibling(); } - // push container - mCurrentFrame = frame; - } - else { - //must be VARIABLE_DEF or PARAMETER_DEF - processVariable(aAST); } + // push container + mCurrentFrame = frame; } /** @see com.puppycrawl.tools.checkstyle.api.Check */ @@ -197,24 +193,23 @@ public class HiddenFieldCheck */ private void processVariable(DetailAST aAST) { - if (!ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) { - if (ScopeUtils.isLocalVariableDef(aAST) - || (aAST.getType() == TokenTypes.PARAMETER_DEF)) - { - //local variable or parameter. Does it shadow a field? - final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT); - final String name = nameAST.getText(); - if ((mCurrentFrame.containsStaticField(name) - || (!inStatic(aAST) - && mCurrentFrame.containsInstanceField(name))) - && ((mRegexp == null) - || (!getRegexp().matcher(name).find())) - && !isIgnoredSetterParam(aAST, name) - && !isIgnoredConstructorParam(aAST)) - { - log(nameAST, "hidden.field", name); - } - } + if (ScopeUtils.inInterfaceOrAnnotationBlock(aAST) + || !ScopeUtils.isLocalVariableDef(aAST) + && (aAST.getType() != TokenTypes.PARAMETER_DEF)) + { + // do nothing + return; + } + //local variable or parameter. Does it shadow a field? + final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT); + final String name = nameAST.getText(); + if ((mCurrentFrame.containsStaticField(name) + || (!inStatic(aAST) && mCurrentFrame.containsInstanceField(name))) + && ((mRegexp == null) || (!getRegexp().matcher(name).find())) + && !isIgnoredSetterParam(aAST, name) + && !isIgnoredConstructorParam(aAST)) + { + log(nameAST, "hidden.field", name); } } @@ -291,7 +286,7 @@ public class HiddenFieldCheck */ private boolean isIgnoredConstructorParam(DetailAST aAST) { - if (!(aAST.getType() == TokenTypes.PARAMETER_DEF) + if ((aAST.getType() != TokenTypes.PARAMETER_DEF) || !mIgnoreConstructorParameter) { return false; diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputHiddenField.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputHiddenField.java index e33743647..76bbd9b74 100644 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/InputHiddenField.java +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputHiddenField.java @@ -224,3 +224,8 @@ enum HiddenEnum } } +// perhaps we should ignore this +// abstract class InputHiddenFieldBug1084512 { +// String x; +// public abstract void methodA(String x); +// }