From 8fa70a1120f796afaaba5b50694206cabf6b12e6 Mon Sep 17 00:00:00 2001 From: Christian Wulf <chw@informatik.uni-kiel.de> Date: Fri, 17 Apr 2015 23:47:06 +0200 Subject: [PATCH] added checkstyle rule --- conf/quality-config/cs-conf.xml | 2 + .../qa/PluralForCollectionVariableCheck.java | 58 +++++++++++++++++++ .../java/teetime/framework/AnalysisTest.java | 15 +++++ .../java/teetime/framework/TraversorTest.java | 15 +++++ 4 files changed, 90 insertions(+) create mode 100644 src/test/java/qa/PluralForCollectionVariableCheck.java diff --git a/conf/quality-config/cs-conf.xml b/conf/quality-config/cs-conf.xml index abc40250..d2051928 100644 --- a/conf/quality-config/cs-conf.xml +++ b/conf/quality-config/cs-conf.xml @@ -1210,6 +1210,8 @@ <property name="allowEmptyConstructors" value="true"/> <property name="allowEmptyMethods" value="true"/> </module> + + <module name="qa.PluralForCollectionVariableCheck" /> </module> diff --git a/src/test/java/qa/PluralForCollectionVariableCheck.java b/src/test/java/qa/PluralForCollectionVariableCheck.java new file mode 100644 index 00000000..91577379 --- /dev/null +++ b/src/test/java/qa/PluralForCollectionVariableCheck.java @@ -0,0 +1,58 @@ +/** + * Copyright (C) 2015 Christian Wulf, Nelson Tavares de Sousa (http://teetime.sourceforge.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package qa; + +import java.util.HashSet; +import java.util.Set; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +public class PluralForCollectionVariableCheck extends Check { + + private final Set<String> collectionNames = new HashSet<String>(); + + public PluralForCollectionVariableCheck() { + collectionNames.add("Collection"); + collectionNames.add("Set"); + collectionNames.add("List"); + } + + @Override + public int[] getDefaultTokens() { + return new int[] { TokenTypes.VARIABLE_DEF }; + } + + @Override + public void visitToken(final DetailAST ast) { + DetailAST typeAst = ast.findFirstToken(TokenTypes.TYPE); + DetailAST identifierAst = ast.findFirstToken(TokenTypes.IDENT); + + DetailAST actualTypeAst = typeAst.getFirstChild(); + DetailAST typeArgumentsAst = actualTypeAst.getNextSibling(); + String actualTypeName = actualTypeAst.getText(); + if (collectionNames.contains(actualTypeName)) { + String identifierName = identifierAst.getText(); + if (!identifierName.endsWith("s")) { + String message = "The variable '" + identifierName + "' should be named in plural since it represents a " + actualTypeName + " of " + + typeArgumentsAst.getText(); + log(ast.getLineNo(), message); + } + } + } + +} diff --git a/src/test/java/teetime/framework/AnalysisTest.java b/src/test/java/teetime/framework/AnalysisTest.java index 24c3341b..61929e18 100644 --- a/src/test/java/teetime/framework/AnalysisTest.java +++ b/src/test/java/teetime/framework/AnalysisTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (C) 2015 Christian Wulf, Nelson Tavares de Sousa (http://teetime.sourceforge.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package teetime.framework; import static org.hamcrest.Matchers.greaterThanOrEqualTo; diff --git a/src/test/java/teetime/framework/TraversorTest.java b/src/test/java/teetime/framework/TraversorTest.java index 209fb6c8..8427febd 100644 --- a/src/test/java/teetime/framework/TraversorTest.java +++ b/src/test/java/teetime/framework/TraversorTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (C) 2015 Christian Wulf, Nelson Tavares de Sousa (http://teetime.sourceforge.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package teetime.framework; import static org.junit.Assert.assertTrue; -- GitLab