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
11 changed files
with
322 additions
and
0 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
146 changes: 146 additions & 0 deletions
146
...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,146 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// 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; | ||
|
||
/** | ||
* Checks for useless "super()" calls in c-tors. | ||
* <p> | ||
* "super()" call could be considered by Check as "useless" in two cases: | ||
* <p> | ||
* Case 1. Example:<br> | ||
* <pre> | ||
* class Dummy {<br> Dummy() {<br> super();<br> }<br> }<br> | ||
* "super()" call is redundant. This class does not extend | ||
* anything. | ||
* </pre> | ||
* <p> | ||
* Case 2. Example:<br> | ||
* <pre> | ||
* class Derived extends Base {<br> Derived() {<br> super();<br> }<br> }<br> | ||
* "super()" is called without parameters. Java compiler | ||
* automatically inserts a call to the no-argument constructor of the | ||
* superclass. Check has an option "ignoreSuperCtorCallWithoutArgs" to skip this | ||
* case. | ||
* </pre> | ||
*/ | ||
public class UselessSuperCtorCallCheck extends Check | ||
{ | ||
/** | ||
* Warning message key. | ||
*/ | ||
public static final String MSG_KEY_SUPER_CALL_IN_NOT_DERIVED_CLASS = "useless.super.ctor.call.in.not.derived.class"; | ||
|
||
/** | ||
* Warning message key. | ||
*/ | ||
public static final String MSG_KEY_SUPER_CALL_WITHOUT_ARGS = "useless.super.ctor.call.without.args"; | ||
|
||
/** | ||
* Used to ignore "super()" calls without arguments in derived class. By | ||
* default check will log this case. | ||
*/ | ||
private boolean mIgnoreSuperCtorCallWithoutArgs = false; | ||
|
||
/** | ||
* Sets flag to IgnoreSuperCallWithoutArgs. | ||
* @param aIgnoreSuperCtorCallWithoutArgs | ||
* if true, check will ignore super() calls without arguments | ||
*/ | ||
public void setIgnoreSuperCtorCallWithoutArgs(boolean aIgnoreSuperCtorCallWithoutArgs) | ||
{ | ||
mIgnoreSuperCtorCallWithoutArgs = aIgnoreSuperCtorCallWithoutArgs; | ||
} | ||
|
||
@Override | ||
public int[] getDefaultTokens() | ||
{ | ||
return new int[] { TokenTypes.SUPER_CTOR_CALL }; | ||
} | ||
|
||
@Override | ||
public void visitToken(DetailAST aSuperCallNode) | ||
{ | ||
if (isSuperArgListEmpty(aSuperCallNode)) | ||
{ | ||
DetailAST classDefNode = getClassDefinitionNode(aSuperCallNode); | ||
|
||
if (isClassDerived(classDefNode)) | ||
{ | ||
if (!mIgnoreSuperCtorCallWithoutArgs) | ||
{ | ||
log(aSuperCallNode.getLineNo(), MSG_KEY_SUPER_CALL_WITHOUT_ARGS); | ||
} | ||
} | ||
else | ||
{ | ||
String className = classDefNode.findFirstToken(TokenTypes.IDENT).getText(); | ||
log(aSuperCallNode.getLineNo(), MSG_KEY_SUPER_CALL_IN_NOT_DERIVED_CLASS, className); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether this super c-tor call has no arguments. | ||
* @param aSuperCallNode | ||
* node of type TokenTypes.SUPER_CTOR_CALL | ||
* @return true, if super constructor call has no arguments | ||
*/ | ||
private static boolean isSuperArgListEmpty(DetailAST aSuperCallNode) | ||
{ | ||
DetailAST argsListNode = aSuperCallNode.findFirstToken(TokenTypes.ELIST); | ||
|
||
return argsListNode.getChildCount() == 0; | ||
} | ||
|
||
/** | ||
* Returns class definition node which contains c-tor with given super | ||
* c-tor. call node | ||
* @param aSuperCallNode | ||
* node of type TokenTypes.SUPER_CTOR_CALL | ||
* @return class definition node | ||
*/ | ||
private static DetailAST getClassDefinitionNode(DetailAST aSuperCallNode) | ||
{ | ||
DetailAST result = aSuperCallNode; | ||
|
||
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
85 changes: 85 additions & 0 deletions
85
...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,85 @@ | ||
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 String msgSuperWithoutArgs = getCheckMessage(MSG_KEY_SUPER_CALL_WITHOUT_ARGS); | ||
|
||
private final DefaultConfiguration mDefaultConfig = createCheckConfig(UselessSuperCtorCallCheck.class); | ||
|
||
@Test | ||
public void testSingleCtorWithSuperWithinNotDerivedClass() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"7: " + getCheckMessage(MSG_KEY_SUPER_CALL_IN_NOT_DERIVED_CLASS, | ||
"InputUselessSuperCtorCall1"), | ||
}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall1.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testSingleCtorWithSuperWithinDerivedClass() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"7: " + msgSuperWithoutArgs, | ||
}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall2.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testMultipleCtorsWithSuperWithinNonsubclass() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"7: " + getCheckMessage(MSG_KEY_SUPER_CALL_IN_NOT_DERIVED_CLASS, | ||
"InputUselessSuperCtorCall3"), | ||
"12: " + getCheckMessage(MSG_KEY_SUPER_CALL_IN_NOT_DERIVED_CLASS, | ||
"InputUselessSuperCtorCall3"), | ||
}; | ||
|
||
verify(mDefaultConfig, getPath("InputUselessSuperCtorCall3.java"), expected); | ||
} | ||
|
||
@Test | ||
public void testInnerClassWithCtorWithSuper() | ||
throws Exception | ||
{ | ||
final String expected[] = { | ||
"9: " + getCheckMessage(MSG_KEY_SUPER_CALL_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("ignoreSuperCtorCallWithoutArgs", "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(); | ||
} | ||
} |