use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.
the class SignatureDeclareThrowsExceptionRule method visit.
@Override
public Object visit(ASTMethodDeclaration methodDeclaration, Object o) {
if (junitImported && isAllowedMethod(methodDeclaration)) {
return super.visit(methodDeclaration, o);
}
if (methodDeclaration.getMethodName().startsWith("test")) {
return super.visit(methodDeclaration, o);
}
// Ignore overridden methods, the issue should be marked on the method definition
final List<ASTAnnotation> methodAnnotations = methodDeclaration.jjtGetParent().findChildrenOfType(ASTAnnotation.class);
for (final ASTAnnotation annotation : methodAnnotations) {
final ASTName annotationName = annotation.getFirstDescendantOfType(ASTName.class);
if (annotationName.hasImageEqualTo("Override") || annotationName.hasImageEqualTo("java.lang.Override")) {
return super.visit(methodDeclaration, o);
}
}
checkExceptions(methodDeclaration, o);
return super.visit(methodDeclaration, o);
}
use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.
the class UseUtilityClassRule method skipAnnotations.
private Node skipAnnotations(Node p) {
int index = 0;
Node n = p.jjtGetChild(index++);
while (n instanceof ASTAnnotation && index < p.jjtGetNumChildren()) {
n = p.jjtGetChild(index++);
}
return n;
}
use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.
the class JUnitUseExpectedRule method visit.
@Override
public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
boolean inAnnotation = false;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
Node child = node.jjtGetChild(i);
if (child instanceof ASTAnnotation) {
ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
if ("Test".equals(annotationName.getImage())) {
inAnnotation = true;
continue;
}
}
if (child instanceof ASTMethodDeclaration) {
boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
if (inAnnotation || isJUnitMethod) {
List<Node> found = new ArrayList<>();
found.addAll((List<Node>) visit((ASTMethodDeclaration) child, data));
for (Node name : found) {
addViolation(data, name);
}
}
}
inAnnotation = false;
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.
the class AbstractJUnitRule method doesNodeContainJUnitAnnotation.
private boolean doesNodeContainJUnitAnnotation(Node node, String annotationTypeClassName) {
List<ASTAnnotation> annotations = node.findDescendantsOfType(ASTAnnotation.class);
for (ASTAnnotation annotation : annotations) {
Node annotationTypeNode = annotation.jjtGetChild(0);
TypeNode annotationType = (TypeNode) annotationTypeNode;
if (annotationType.getType() == null) {
ASTName name = annotationTypeNode.getFirstChildOfType(ASTName.class);
if (name != null && (name.hasImageEqualTo("Test") || name.hasImageEqualTo(annotationTypeClassName))) {
return true;
}
} else if (TypeHelper.isA(annotationType, annotationTypeClassName)) {
return true;
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.
the class AbstractLombokAwareRule method hasLombokAnnotation.
/**
* @deprecated As of release 6.2.0, replaced by {@link #hasLombokAnnotation(Annotatable)}
* Checks whether the given node is annotated with any lombok annotation.
* The node can be any node, e.g. class declaration or field declaration.
*
* @param node
* the node to check
* @return <code>true</code> if a lombok annotation has been found
*/
@Deprecated
protected boolean hasLombokAnnotation(Node node) {
boolean result = false;
Node parent = node.jjtGetParent();
List<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
for (ASTAnnotation annotation : annotations) {
ASTName name = annotation.getFirstDescendantOfType(ASTName.class);
if (name != null) {
String annotationName = name.getImage();
if (lombokImported) {
if (LOMBOK_ANNOTATIONS.contains(annotationName)) {
result = true;
}
} else {
if (annotationName.startsWith(LOMBOK_PACKAGE + ".")) {
String shortName = annotationName.substring(LOMBOK_PACKAGE.length() + 1);
if (LOMBOK_ANNOTATIONS.contains(shortName)) {
result = true;
}
}
}
}
}
return result;
}
Aggregations