forked from sevntu-checkstyle/sevntu.checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new Check UselessSuperCtorCallCheck implemeted. Fixes sevntu-checksty…
- Loading branch information
Showing
12 changed files
with
362 additions
and
2 deletions.
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
174 changes: 174 additions & 0 deletions
174
...s/src/main/java/com/github/sevntu/checkstyle/checks/coding/UselessSuperCtorCallCheck.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,174 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2014 Oliver Burn | ||
// | ||
// 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.puppycrawl.tools.checkstyle.api.Check; | ||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
||
/** | ||
* <p> | ||
* Checks for useless "super()" calls in c-tors. | ||
* </p> | ||
* <p> | ||
* "super()" call could be considered by Check as "useless" in two cases: | ||
* </p> | ||
* <p> | ||
* Case 1. Example: | ||
* <pre> | ||
* class Dummy { | ||
* Dummy() { | ||
* super(); | ||
* } | ||
* } | ||
* </pre> | ||
* "super()" call is redundant. This class does not extend | ||
* anything. | ||
* </p> | ||
* | ||
* <p> | ||
* Case 2. Example: | ||
* <pre> | ||
* class Derived extends Base { | ||
* Derived() { | ||
* super(); | ||
* } | ||
* } | ||
* </pre> | ||
* "super()" is called without parameters. Java compiler automatically inserts | ||
* a call to the no-argument constructor of the superclass. Check has an option | ||
* "allowCallToNoArgsSuperCtorFromDerivedClass" to skip this case. | ||
* </p> | ||
* <p> | ||
* Xml checkstyle configuration example with "allowCallToNoArgsSuperCtorFromDerivedClass" | ||
* option enabled: | ||
* <pre> | ||
* {@code | ||
* <module name="UselessSuperCtorCallCheck"> | ||
* <property name="severity" value="warning"/> | ||
* <property name="allowCallToNoArgsSuperCtorFromDerivedClass" value="true"/> | ||
* </module> | ||
* } | ||
* </pre> | ||
* </p> | ||
* | ||
* @author <a href="mailto:zuy_alexey@mail.ru">Zuy Alexey</a> | ||
*/ | ||
public class UselessSuperCtorCallCheck extends Check | ||
{ | ||
/** | ||
* Violation message key. | ||
*/ | ||
public static final String MSG_IN_NOT_DERIVED_CLASS = | ||
"useless.super.ctor.call.in.not.derived.class"; | ||
|
||
/** | ||
* Violation message key. | ||
*/ | ||
public static final String MSG_WITHOUT_ARGS = | ||
"useless.super.ctor.call.without.args"; | ||
|
||
/** | ||
* Used to allow calls to no-arguments super constructor from derived class. | ||
* By default check will log this case. | ||
*/ | ||
private boolean mAllowCallToNoArgsSuperCtorFromDerivedClass = false; | ||
|
||
/** | ||
* Sets flag to allowCallToNoArgsSuperCtor. | ||
* @param aAllowCallToNoArgsSuperCtor | ||
* if true, check will allow super() calls without arguments | ||
*/ | ||
public void setAllowCallToNoArgsSuperCtorFromDerivedClass(boolean aAllowCallToNoArgsSuperCtor) | ||
{ | ||
mAllowCallToNoArgsSuperCtorFromDerivedClass = aAllowCallToNoArgsSuperCtor; | ||
} | ||
|
||
@Override | ||
public int[] getDefaultTokens() | ||
{ | ||
return new int[] { TokenTypes.SUPER_CTOR_CALL }; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST aSuperCallNode) | ||
{ | ||
if (getMethodCallArgsCount(aSuperCallNode) == 0) | ||
{ | ||
DetailAST classDefNode = getClassDefinitionNode(aSuperCallNode); | ||
String className = classDefNode.findFirstToken(TokenTypes.IDENT).getText(); | ||
|
||
if (isClassDerived(classDefNode)) | ||
{ | ||
if (!mAllowCallToNoArgsSuperCtorFromDerivedClass) | ||
{ | ||
log(aSuperCallNode.getLineNo(), MSG_WITHOUT_ARGS, className); | ||
} | ||
} | ||
else | ||
{ | ||
log(aSuperCallNode.getLineNo(), MSG_IN_NOT_DERIVED_CLASS, className); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns arguments count for method call. | ||
* @param aMethodCallNode | ||
* node of type TokenTypes.METHOD_CALL | ||
* @return arguments count for method call | ||
*/ | ||
private static int getMethodCallArgsCount(DetailAST aMethodCallNode) | ||
{ | ||
DetailAST argsListNode = aMethodCallNode.findFirstToken(TokenTypes.ELIST); | ||
|
||
return argsListNode.getChildCount(); | ||
} | ||
|
||
/** | ||
* Returns class definition node for class, which contains given AST node. | ||
* @param aNode | ||
* AST node inside class | ||
* @return class definition node | ||
*/ | ||
private static DetailAST getClassDefinitionNode(DetailAST aNode) | ||
{ | ||
DetailAST result = aNode; | ||
|
||
while (result.getType() != TokenTypes.CLASS_DEF) | ||
{ | ||
result = result.getParent(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Checks whether this class is derived from other class. | ||
* @param aClassDefNode | ||
* class definition node | ||
* @return true, if this class extends anything | ||
*/ | ||
private static boolean isClassDerived(DetailAST aClassDefNode) | ||
{ | ||
return aClassDefNode.findFirstToken(TokenTypes.EXTENDS_CLAUSE) != null; | ||
} | ||
|
||
} |
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
84 changes: 84 additions & 0 deletions
84
...c/test/java/com/github/sevntu/checkstyle/checks/coding/UselessSuperCtorCallCheckTest.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,84 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
import static com.github.sevntu.checkstyle.checks.coding.UselessSuperCtorCallCheck.*; | ||
|
||
import org.junit.Test; | ||
|
||
import com.github.sevntu.checkstyle.BaseCheckTestSupport; | ||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
|
||
public class UselessSuperCtorCallCheckTest extends BaseCheckTestSupport | ||
{ | ||
private final DefaultConfiguration mDefaultConfig = createCheckConfig(UselessSuperCtorCallCheck.class); | ||
|
||
@Test | ||
public void testSingleCtorWithSuperWithinNotDerivedClass() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"7: " + getCheckMessage(MSG_IN_NOT_DERIVED_CLASS, | ||
"InputUselessSuperCtorCall1"), | ||
}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall1.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testSingleCtorWithSuperWithinDerivedClass() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"7: " + getCheckMessage(MSG_WITHOUT_ARGS, | ||
"InputUselessSuperCtorCall2"), | ||
}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall2.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testMultipleCtorsWithSuperWithinNonsubclass() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"7: " + getCheckMessage(MSG_IN_NOT_DERIVED_CLASS, | ||
"InputUselessSuperCtorCall3"), | ||
"12: " + getCheckMessage(MSG_IN_NOT_DERIVED_CLASS, | ||
"InputUselessSuperCtorCall3"), | ||
}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall3.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testInnerClassWithCtorWithSuper() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"9: " + getCheckMessage(MSG_IN_NOT_DERIVED_CLASS, | ||
"Inner"), | ||
}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall4.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testClassWithSuperCtorWithArgs() | ||
throws Exception | ||
{ | ||
final String expected[] = {}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall5.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testClassWithSuperCtorWithoutArgsAndIgnoreSuperCtorCallOptionSetToTrue() | ||
throws Exception | ||
{ | ||
final DefaultConfiguration checkConfig = createCheckConfig(UselessSuperCtorCallCheck.class); | ||
checkConfig.addAttribute("allowCallToNoArgsSuperCtorFromDerivedClass", "true"); | ||
|
||
final String expected[] = {}; | ||
|
||
verify(checkConfig, getPath("InputUselessSuperCtorCall6.java"), expected); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...test/resources/com/github/sevntu/checkstyle/checks/coding/InputUselessSuperCtorCall1.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 InputUselessSuperCtorCall1 | ||
{ | ||
public InputUselessSuperCtorCall1() | ||
{ | ||
super(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...test/resources/com/github/sevntu/checkstyle/checks/coding/InputUselessSuperCtorCall2.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 InputUselessSuperCtorCall2 extends java.lang.Object | ||
{ | ||
public InputUselessSuperCtorCall2() | ||
{ | ||
super(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...test/resources/com/github/sevntu/checkstyle/checks/coding/InputUselessSuperCtorCall3.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,14 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
public class InputUselessSuperCtorCall3 | ||
{ | ||
public InputUselessSuperCtorCall3() | ||
{ | ||
super(); | ||
} | ||
|
||
public InputUselessSuperCtorCall3(int i) | ||
{ | ||
super(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...test/resources/com/github/sevntu/checkstyle/checks/coding/InputUselessSuperCtorCall4.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,12 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
public class InputUselessSuperCtorCall4 extends java.lang.Object | ||
{ | ||
public class Inner | ||
{ | ||
public Inner() | ||
{ | ||
super(); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...test/resources/com/github/sevntu/checkstyle/checks/coding/InputUselessSuperCtorCall5.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,23 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
import java.io.File; | ||
import java.io.PrintWriter; | ||
|
||
public class InputUselessSuperCtorCall5 | ||
{ | ||
private static class Base | ||
{ | ||
public Base(int i) | ||
{ | ||
|
||
} | ||
} | ||
|
||
private static class Derived extends Base | ||
{ | ||
public Derived() | ||
{ | ||
super(2); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...test/resources/com/github/sevntu/checkstyle/checks/coding/InputUselessSuperCtorCall6.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,8 @@ | ||
package com.github.sevntu.checkstyle.checks.coding; | ||
|
||
public class InputUselessSuperCtorCall6 extends java.lang.Object | ||
{ | ||
public InputUselessSuperCtorCall6() { | ||
super(); | ||
} | ||
} |
Oops, something went wrong.