Created
October 10, 2014 21:34
-
-
Save kevints/3638383e1b10f1081c44 to your computer and use it in GitHub Desktop.
PMD rule to enforce Guice AOP limitations (untested)
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
package org.apache.aurora.pmd; | |
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; | |
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; | |
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; | |
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; | |
import net.sourceforge.pmd.lang.java.ast.ASTName; | |
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; | |
import net.sourceforge.pmd.lang.symboltable.NameDeclaration; | |
/** | |
* Check for appropriate use of the timed interceptor. | |
*/ | |
public class TimedRule extends AbstractJavaRule { | |
@Override | |
public Object visit(ASTMethodDeclaration node, Object data) { | |
boolean hasTimedAnnotation = false; | |
ASTClassOrInterfaceBodyDeclaration declaration = | |
node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); | |
for (ASTAnnotation astAnnotation : declaration.findChildrenOfType(ASTAnnotation.class)) { | |
ASTName name = astAnnotation.getFirstDescendantOfType(ASTName.class); | |
if (name != null) { | |
NameDeclaration nameDeclaration = name.getNameDeclaration(); | |
if (nameDeclaration != null && "Timed".equals(name.getNameDeclaration().getName())) { | |
hasTimedAnnotation = true; | |
break; | |
} | |
} | |
} | |
if (hasTimedAnnotation && node.isPrivate()) { | |
addViolationWithMessage(data, node, "@Timed annotation is not allowed on private methods."); | |
} | |
if (hasTimedAnnotation && node.isStatic()) { | |
addViolationWithMessage(data, node, "@Timed annotation is not allowed on static methods."); | |
} | |
if (hasTimedAnnotation && node.isFinal()) { | |
addViolationWithMessage(data, node, "@Timed annotation is not allowed on final methods."); | |
} | |
if (hasTimedAnnotation) { | |
ASTClassOrInterfaceDeclaration classDeclaration = | |
node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class); | |
if (classDeclaration != null) { | |
if (classDeclaration.isFinal()) { | |
addViolationWithMessage(data, classDeclaration, | |
"@Timed annotation is not allowed on final classes."); | |
} | |
if (!(classDeclaration.isPublic() || classDeclaration.isPackagePrivate())) { | |
addViolationWithMessage(data, classDeclaration, | |
"@Timed annotation is only allowed on methods of public or package-private classes."); | |
} | |
} | |
} | |
return super.visit(node, data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment