/** * Checks if the given value is defined as <code>Exception</code> and the * parent is either a method or constructor declaration. * * @param exception * to evaluate * @return true if <code>Exception</code> is declared and has proper parents */ private boolean hasDeclaredExceptionInSignature(ASTName exception) { return exception.hasImageEqualTo("Exception") && isParentSignatureDeclaration(exception); }
private boolean isFillInStackTraceCalled(final String target, final ASTVariableInitializer initializer) { final ASTName astName = initializer.getFirstDescendantOfType(ASTName.class); return astName != null && astName.hasImageEqualTo(target + FILL_IN_STACKTRACE); }
private boolean hasImports(ASTCompilationUnit cu, String className) { List<ASTImportDeclaration> imports = cu.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration importDeclaration : imports) { ASTName name = importDeclaration.getFirstChildOfType(ASTName.class); if (name != null && name.hasImageEqualTo(className)) { return true; } } return false; } }
private boolean hasOverrideAnnotation(ASTMethodDeclaration node) { int childIndex = node.jjtGetChildIndex(); for (int i = 0; i < childIndex; i++) { Node previousSibling = node.jjtGetParent().jjtGetChild(i); List<ASTMarkerAnnotation> annotations = previousSibling.findDescendantsOfType(ASTMarkerAnnotation.class); for (ASTMarkerAnnotation annotation : annotations) { ASTName name = annotation.getFirstChildOfType(ASTName.class); if (name != null && (name.hasImageEqualTo("Override") || name.hasImageEqualTo("java.lang.Override"))) { return true; } } } return false; } }
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; }
@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); }
public void check(Object ctx, ASTArguments node) { if (node.getArgumentCount() == argumentsCount && node.getNthParent(2) instanceof ASTPrimaryExpression) { ASTPrimaryPrefix primaryPrefix = node.getNthParent(2).getFirstChildOfType(ASTPrimaryPrefix.class); if (primaryPrefix != null) { ASTName name = primaryPrefix.getFirstChildOfType(ASTName.class); if (name != null && name.hasImageEqualTo(this.assertionName)) { if (isException(node)) { return; } JUnitAssertionsShouldIncludeMessageRule.this.addViolation(ctx, name); } } } }
private boolean hasClone(ASTReturnStatement ret, String varName) { List<ASTPrimaryExpression> expressions = ret.findDescendantsOfType(ASTPrimaryExpression.class); for (ASTPrimaryExpression e : expressions) { if (e.jjtGetChild(0) instanceof ASTPrimaryPrefix && e.jjtGetNumChildren() == 2 && e.jjtGetChild(1) instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) e.jjtGetChild(1)).isArguments() && ((ASTPrimarySuffix) e.jjtGetChild(1)).getArgumentCount() == 0) { ASTName name = e.getFirstDescendantOfType(ASTName.class); if (name != null && name.hasImageEqualTo(varName + ".clone")) { return true; } } } return false; }
if (primaryPrefix != null) { ASTName prefixName = primaryPrefix.getFirstChildOfType(ASTName.class); if (prefixName != null && prefixName.hasImageEqualTo(baseName)) {
ASTVariableDeclaratorId variableId = findFirstDegreeChildrenOfType(formalParameter, ASTVariableDeclaratorId.class).get(0); if (!argumentName.hasImageEqualTo(variableId.getImage())) {
if (!n.hasImageEqualTo(returnVariableName)) { continue;
/** * Checks if the given value is defined as <code>Exception</code> and the * parent is either a method or constructor declaration. * * @param exception * to evaluate * @return true if <code>Exception</code> is declared and has proper parents */ private boolean hasDeclaredExceptionInSignature(ASTName exception) { return exception.hasImageEqualTo("Exception") && isParentSignatureDeclaration(exception); }
private boolean isFillInStackTraceCalled(final String target, final ASTVariableInitializer initializer) { final ASTName astName = initializer.getFirstDescendantOfType(ASTName.class); return astName != null && astName.hasImageEqualTo(target + FILL_IN_STACKTRACE); }
private boolean hasImports(ASTCompilationUnit cu, String className) { List<ASTImportDeclaration> imports = cu.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration importDeclaration : imports) { ASTName name = importDeclaration.getFirstChildOfType(ASTName.class); if (name != null && name.hasImageEqualTo(className)) { return true; } } return false; } }
private boolean hasOverrideAnnotation(ASTMethodDeclaration node) { int childIndex = node.jjtGetChildIndex(); for (int i = 0; i < childIndex; i++) { Node previousSibling = node.jjtGetParent().jjtGetChild(i); List<ASTMarkerAnnotation> annotations = previousSibling.findDescendantsOfType(ASTMarkerAnnotation.class); for (ASTMarkerAnnotation annotation : annotations) { ASTName name = annotation.getFirstChildOfType(ASTName.class); if (name != null && (name.hasImageEqualTo("Override") || name.hasImageEqualTo("java.lang.Override"))) { return true; } } } return false; } }
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; }
@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); }
public void check(Object ctx, ASTArguments node) { if (node.getArgumentCount() == argumentsCount && node.getNthParent(2) instanceof ASTPrimaryExpression) { ASTPrimaryPrefix primaryPrefix = node.getNthParent(2).getFirstChildOfType(ASTPrimaryPrefix.class); if (primaryPrefix != null) { ASTName name = primaryPrefix.getFirstChildOfType(ASTName.class); if (name != null && name.hasImageEqualTo(this.assertionName)) { if (isException(node)) { return; } JUnitAssertionsShouldIncludeMessageRule.this.addViolation(ctx, name); } } } }
private boolean hasClone(ASTReturnStatement ret, String varName) { List<ASTPrimaryExpression> expressions = ret.findDescendantsOfType(ASTPrimaryExpression.class); for (ASTPrimaryExpression e : expressions) { if (e.jjtGetChild(0) instanceof ASTPrimaryPrefix && e.jjtGetNumChildren() == 2 && e.jjtGetChild(1) instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) e.jjtGetChild(1)).isArguments() && ((ASTPrimarySuffix) e.jjtGetChild(1)).getArgumentCount() == 0) { ASTName name = e.getFirstDescendantOfType(ASTName.class); if (name != null && name.hasImageEqualTo(varName + ".clone")) { return true; } } } return false; }
if (primaryPrefix != null) { ASTName prefixName = primaryPrefix.getFirstChildOfType(ASTName.class); if (prefixName != null && prefixName.hasImageEqualTo(baseName)) {