-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #461: Field single declaration check implemented #811
Open
yaziza
wants to merge
1
commit into
sevntu-checkstyle:master
Choose a base branch
from
yaziza:461-field-declaration-count
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...src/main/java/com/github/sevntu/checkstyle/checks/coding/FieldSingleDeclarationCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2020 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
import com.github.sevntu.checkstyle.SevntuUtil; | ||
import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
/** | ||
* <p>This checks ensures that classes have at most 1 field of the given className.</p> | ||
* | ||
* <p>This can be useful for example to ensure that only one Logger is used:</p> | ||
* | ||
* <pre> | ||
* <module name="TreeWalker"> | ||
* <module name="com.github.sevntu.checkstyle.checks.coding.FieldSingleDeclarationCheck"> | ||
* <property name="fullyQualifiedClassName" value="org.slf4j.Logger" /> | ||
* </module> | ||
* </module> | ||
* | ||
* class Example { | ||
* private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Example.class); // OK | ||
* private static org.slf4j.Logger logger2; // NOK! | ||
* } | ||
* </pre> | ||
* | ||
* @author Milos Fabian, Pantheon Technologies - original author (in OpenDaylight.org) | ||
* @author Michael Vorburger.ch - refactored and made more generalized for contribution to Sevntu | ||
* @author <a href="mailto:[email protected]">Yasser Aziza</a> - completed sevntu integration | ||
*/ | ||
public class FieldSingleDeclarationCheck extends AbstractCheck { | ||
|
||
/** | ||
* Violation message key. | ||
*/ | ||
public static final String MSG_KEY = "field.count"; | ||
|
||
/** | ||
* Configuration property with class name check for. | ||
*/ | ||
private String fullyQualifiedClassName = "java.util.logging.Logger"; | ||
|
||
/** | ||
* Field to remember if class was previously seen in current File. | ||
*/ | ||
private boolean hasPreviouslySeenClass; | ||
|
||
/** | ||
* Set Class of which there should only be max. 1 field per class. | ||
* @param className the fully qualified name (FQN) of the class | ||
*/ | ||
public void setFullyQualifiedClassName(String className) { | ||
this.fullyQualifiedClassName = className; | ||
} | ||
|
||
@Override | ||
public int[] getDefaultTokens() { | ||
return new int[] {TokenTypes.VARIABLE_DEF }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this being enough. You are not checking imports so your implementation is very susceptible to name conflicts. |
||
} | ||
|
||
@Override | ||
public int[] getRequiredTokens() { | ||
return getDefaultTokens(); | ||
} | ||
|
||
@Override | ||
public int[] getAcceptableTokens() { | ||
return getDefaultTokens(); | ||
} | ||
|
||
@Override | ||
public void beginTree(DetailAST rootAST) { | ||
this.hasPreviouslySeenClass = false; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST ast) { | ||
if (SevntuUtil.matchesFullyQualifiedName(ast, fullyQualifiedClassName)) { | ||
if (hasPreviouslySeenClass) { | ||
log(ast.getLineNo(), MSG_KEY, fullyQualifiedClassName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please give it the full AST. |
||
} | ||
this.hasPreviouslySeenClass = true; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...test/java/com/github/sevntu/checkstyle/checks/coding/FieldSingleDeclarationCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2020 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
import org.junit.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
|
||
/** | ||
* LoggerDeclarationsCountCheck test. | ||
* | ||
* @author <a href="mailto:[email protected]">Michael Vorburger</a> - completed sevntu integration | ||
*/ | ||
public class FieldSingleDeclarationCheckTest extends AbstractModuleTestSupport { | ||
|
||
@Override | ||
protected String getPackageLocation() { | ||
return "com/github/sevntu/checkstyle/checks/coding"; | ||
} | ||
|
||
@Test | ||
public void testFieldSingleDeclarationOk() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(FieldSingleDeclarationCheck.class); | ||
|
||
checkConfig.addAttribute("fullyQualifiedClassName", "org.slf4j.Logger"); | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testFieldSingleDeclarationNok() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(FieldSingleDeclarationCheck.class); | ||
|
||
checkConfig.addAttribute("fullyQualifiedClassName", "org.slf4j.Logger"); | ||
|
||
final String[] expected = { | ||
"10: " + getCheckMessage(FieldSingleDeclarationCheck.MSG_KEY, "org.slf4j.Logger"), | ||
}; | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckNok.java"), expected); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testFieldSingleDeclarationFqnNok() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(FieldSingleDeclarationCheck.class); | ||
|
||
checkConfig.addAttribute("fullyQualifiedClassName", "org.slf4j.Logger"); | ||
|
||
final String[] expected = { | ||
"7: " + getCheckMessage(FieldSingleDeclarationCheck.MSG_KEY, "org.slf4j.Logger"), | ||
}; | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckFqnNok.java"), expected); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testNoClassNameConfigured() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(FieldSingleDeclarationCheck.class); | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckNok.java"), new String[] {}); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckFqnNok.java"), | ||
new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testCustomTokens() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(FieldSingleDeclarationCheck.class); | ||
|
||
checkConfig.addAttribute("fullyQualifiedClassName", "org.slf4j.Logger"); | ||
// This is required just so that getAcceptableTokens() gets coverage | ||
checkConfig.addAttribute("tokens", "VARIABLE_DEF"); | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testUnknownClassName() throws Exception { | ||
final DefaultConfiguration checkConfig = | ||
createModuleConfig(FieldSingleDeclarationCheck.class); | ||
|
||
checkConfig.addAttribute("fullyQualifiedClassName", "SomeClass"); | ||
// This is required just so that getAcceptableTokens() gets coverage | ||
checkConfig.addAttribute("tokens", "VARIABLE_DEF"); | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckNok.java"), new String[] {}); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckFqnNok.java"), | ||
new String[] {}); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...es/com/github/sevntu/checkstyle/checks/coding/InputFieldSingleDeclarationCheckFqnNok.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
public class InputFieldSingleDeclarationCheckFqnNok { | ||
|
||
// even twice a FQN type names work: | ||
org.slf4j.Logger logger1; | ||
org.slf4j.Logger logger2; // <= Checkstyle violation raised here! | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...urces/com/github/sevntu/checkstyle/checks/coding/InputFieldSingleDeclarationCheckNok.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
import java.io.File; | ||
import java.util.logging.Logger; | ||
|
||
public class InputFieldSingleDeclarationCheckNok { | ||
|
||
// both FQN and import'ed type names work: | ||
Logger logger1; | ||
org.slf4j.Logger logger2; // <= Checkstyle violation raised here! | ||
|
||
// some field of another type (req. for test coverage) | ||
File file; | ||
|
||
// a method, so that there are not only fields here (req. for test coverage) | ||
void foo() { } | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...ources/com/github/sevntu/checkstyle/checks/coding/InputFieldSingleDeclarationCheckOk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
public class InputFieldSingleDeclarationCheckOk { | ||
|
||
org.slf4j.Logger logger; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we should test
findFirstToken
andident
against NPE. A variable definition should always have a TYPE and IDENT right ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regression would should if an exception is possible.