-
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.
- Loading branch information
1 parent
ac19743
commit 14e88b8
Showing
6 changed files
with
59 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package gradestyle.validator.pmd; | ||
|
||
import net.sourceforge.pmd.lang.java.ast.ASTBlock; | ||
import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement; | ||
import net.sourceforge.pmd.lang.java.ast.ASTIfStatement; | ||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; | ||
import net.sourceforge.pmd.lang.java.ast.ASTStatement; | ||
import net.sourceforge.pmd.lang.java.ast.JavaNode; | ||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; | ||
|
||
public class EarlyReturn extends AbstractJavaRule { | ||
public EarlyReturn() { | ||
addRuleChainVisit(ASTMethodDeclaration.class); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTMethodDeclaration method, Object data) { | ||
ASTBlock body = method.getBody(); | ||
|
||
if (body == null || body.getNumChildren() == 0) { | ||
return data; | ||
} | ||
|
||
JavaNode blockNode = body.getChild(body.getNumChildren() - 1); | ||
|
||
if (blockNode instanceof ASTBlockStatement blockStmt) { | ||
JavaNode stmtNode = blockStmt.getChild(0); | ||
|
||
if (stmtNode instanceof ASTStatement stmt) { | ||
if (stmt.getChild(0) instanceof ASTIfStatement conditional) { | ||
asCtx(data).addViolation(conditional); | ||
} | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
} |
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