-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #461: Field single declaration check implemented
- Loading branch information
Showing
12 changed files
with
317 additions
and
1 deletion.
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
108 changes: 108 additions & 0 deletions
108
...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,108 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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; | ||
|
||
/** | ||
* 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 }; | ||
} | ||
|
||
@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 (fullyQualifiedClassName == null) { | ||
throw new IllegalStateException("Must set mandatory fullyQualifiedClassName property in" | ||
+ getClass().getSimpleName()); | ||
} | ||
if (SevntuUtil.matchesFullyQualifiedName(ast, fullyQualifiedClassName)) { | ||
if (hasPreviouslySeenClass) { | ||
log(ast.getLineNo(), MSG_KEY, fullyQualifiedClassName); | ||
} | ||
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
103 changes: 103 additions & 0 deletions
103
...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,103 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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; | ||
import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | ||
|
||
/** | ||
* LoggerDeclarationsCountCheck test. | ||
* | ||
* @author <a href="mailto:[email protected]">Michael Vorburger</a> - completed sevntu integration | ||
*/ | ||
public class FieldSingleDeclarationCheckTest extends AbstractModuleTestSupport { | ||
|
||
private static final String CLASS_NAME_KEY = "fullyQualifiedClassName"; | ||
private static final String CLASS_NAME_VALUE = "org.slf4j.Logger"; | ||
|
||
private final DefaultConfiguration checkConfig = | ||
createModuleConfig(FieldSingleDeclarationCheck.class); | ||
|
||
@Override | ||
protected String getPackageLocation() { | ||
return "com/github/sevntu/checkstyle/checks/coding"; | ||
} | ||
|
||
@Test | ||
public void testFieldSingleDeclarationOk() throws Exception { | ||
checkConfig.addAttribute(CLASS_NAME_KEY, CLASS_NAME_VALUE); | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testFieldSingleDeclarationNok() throws Exception { | ||
checkConfig.addAttribute(CLASS_NAME_KEY, CLASS_NAME_VALUE); | ||
|
||
final String[] expected = { | ||
"10: " + getCheckMessage(FieldSingleDeclarationCheck.MSG_KEY, CLASS_NAME_VALUE), | ||
}; | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckNok.java"), expected); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testFieldSingleDeclarationFqnNok() throws Exception { | ||
checkConfig.addAttribute(CLASS_NAME_KEY, CLASS_NAME_VALUE); | ||
|
||
final String[] expected = { | ||
"7: " + getCheckMessage(FieldSingleDeclarationCheck.MSG_KEY, CLASS_NAME_VALUE), | ||
}; | ||
|
||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckFqnNok.java"), expected); | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test(expected = CheckstyleException.class) | ||
public void testNoClassNameConfigured() throws Exception { | ||
verify(checkConfig, getPath("InputFieldSingleDeclarationCheckOk.java"), new String[] {}); | ||
} | ||
|
||
@Test | ||
public void testCustomTokens() throws Exception { | ||
checkConfig.addAttribute(CLASS_NAME_KEY, CLASS_NAME_VALUE); | ||
// 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 { | ||
checkConfig.addAttribute(CLASS_NAME_KEY, "logger"); | ||
// 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