@Override public Object visit(ASTMethodDeclaration method, Object data) { if (isJUnitMethod(method, data)) { if (!isExpectAnnotated(method.jjtGetParent())) { Map<String, VariableNameDeclaration> variables = getVariables(method); Scope classScope = method.getScope().getParent(); Map<String, List<NameOccurrence>> expectables = getRuleAnnotatedExpectedExceptions(classScope); if (!containsExpectOrAssert(method.getBlock(), expectables, variables)) { addViolation(data, method); } } } return data; }
/** * Returns the exception names listed in the {@code throws} clause * of this method declaration, or null if there are none. */ public ASTNameList getThrows() { return getFirstChildOfType(ASTNameList.class); }
@Override public Object visit(ASTMethodDeclaration meth, Object data) { // skip void/abstract/native method if (meth.isVoid() || meth.isAbstract() || meth.isNative()) { return data; } return super.visit(meth, data); }
private boolean isJUnit3Method(ASTMethodDeclaration method) { return isJUnit3Class && method.isVoid() && method.getMethodName().startsWith("test"); }
/** Attempts to determine if the method is a setter. */ private static boolean isSetter(ASTMethodDeclaration node, Map<String, String> fieldNames) { if (node.getFirstDescendantOfType(ASTFormalParameters.class).getParameterCount() != 1 || !node.getFirstDescendantOfType(ASTResultType.class).isVoid()) { return false; } if (node.getName().startsWith("set")) { return containsIgnoreCase(fieldNames.keySet(), node.getName().substring(3)); } return fieldNames.containsKey(node.getName()); }
if (node.isAbstract() || node.isFinal() || node.isNative() || node.isSynchronized()) { return super.visit(node, data); if (CLONE.equals(node.getMethodName()) && node.isPublic() && !this.hasArguments(node) && this.isMethodType(node, OBJECT) && this.isMethodThrowingType(node, exceptions)) { return super.visit(node, data); ASTBlock block = node.getBlock(); if (block == null) { return super.visit(node, data); ASTClassOrInterfaceBodyDeclaration parent = (ASTClassOrInterfaceBodyDeclaration) node.jjtGetParent(); for (int i = 0; i < parent.jjtGetNumChildren(); i++) { Node n = parent.jjtGetChild(i);
@Override public Object visit(final ASTMethodDeclaration node, final Object data) { final ASTMethodDeclarator methodDeclarator = node.getFirstChildOfType(ASTMethodDeclarator.class); if (!isCloneMethod(methodDeclarator)) { return data; final ASTClassOrInterfaceDeclaration classOrInterface = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class); if (classOrInterface != null && //Don't analyze enums, which cannot subclass clone() (node.isFinal() || classOrInterface.isFinal())) { if (node.findDescendantsOfType(ASTBlock.class).size() == 1) { final List<ASTBlockStatement> blocks = node.findDescendantsOfType(ASTBlockStatement.class); if (blocks.size() == 1) { final ASTBlockStatement block = blocks.get(0);
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (!node.isPrivate() && !getProperty(CHECKALL_DESCRIPTOR)) { return data; } if (!node.isNative() && !node.isAbstract() && !isSerializationMethod(node) && !hasOverrideAnnotation(node)) { check(node, data); } return data; }
public boolean isJUnitMethod(ASTMethodDeclaration method, Object data) { if (method.isAbstract() || method.isNative() || method.isStatic()) { return false; // skip various inapplicable method variations } if (!isJUnit5Class && !method.isPublic()) { // junit5 class doesn't require test methods to be public anymore return false; } boolean result = false; result |= isJUnit3Method(method); result |= isJUnit4Method(method); result |= isJUnit5Method(method); return result; }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (currentLookup.peek() == null) { return super.visit(node, data); } for (ASTAnnotation annot : node.getDeclaredAnnotations()) { if (Override.class.equals(annot.getType())) { // we assume the compiler has already checked it, so it's correct return super.visit(node, data); } } try { boolean overridden = currentLookup.peek().isOverridden(node.getName(), node.getFormalParameters()); if (overridden) { addViolation(data, node, new Object[]{node.getQualifiedName().getOperation()}); } } catch (NoSuchMethodException e) { // may happen in the body of an enum constant, // because the method lookup used is the one of // the parent class. LOG.warning("MissingOverride encountered unexpected method " + node.getMethodName()); // throw new RuntimeException(e); // uncomment when enum constants are handled by typeres } return super.visit(node, data); }
@Override public Object visit(ASTMethodDeclaration method, Object data) { if (!method.getResultType().returnsArray() || method.isPrivate()) { return data; List<ASTReturnStatement> returns = method.findDescendantsOfType(ASTReturnStatement.class); ASTTypeDeclaration td = method.getFirstParentOfType(ASTTypeDeclaration.class); for (ASTReturnStatement ret : returns) { final String vn = getReturnedVariableName(ret);
@Override public Object visit(ASTMethodDeclaration meth, Object data) { if (meth.isNative() || meth.isAbstract()) { return data; } this.lookForViolation(meth.getScope(), data); return super.visit(meth, data); }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (!node.isAbstract()) { if (node.getFirstParentOfType(ASTAnyTypeDeclaration.class) == exploredClass) { currentMethodName = node.getQualifiedName().getOperation(); methodAttributeAccess.put(currentMethodName, new HashSet<String>()); super.visit(node, data); currentMethodName = null; } else { super.visit(node, data); } } return null; }
private boolean isSerializationMethod(ASTMethodDeclaration node) { ASTMethodDeclarator declarator = node.getFirstDescendantOfType(ASTMethodDeclarator.class); List<ASTFormalParameter> parameters = declarator.findDescendantsOfType(ASTFormalParameter.class); if (node.isPrivate() && "readObject".equals(node.getMethodName()) && parameters.size() == 1 && throwsOneException(node, InvalidObjectException.class)) { ASTType type = parameters.get(0).getTypeNode(); if (type.getType() == ObjectInputStream.class || ObjectInputStream.class.getSimpleName().equals(type.getTypeImage()) || ObjectInputStream.class.getName().equals(type.getTypeImage())) { return true; } } return false; }
public void checkMethodCommentFormat(ASTMethodDeclaration method, Object data) { Comment comment = method.comment(); String commentContent = comment.getImage(); ViolationUtils.addViolationWithPrecisePosition(this, method, data, I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".desc", method.getMethodName())); variableDeclaratorIds = method.findChildNodesWithXPath(METHOD_VARIABLE_DECLARATOR_XPATH); } catch (JaxenException e) { throw new RuntimeException( ViolationUtils.addViolationWithPrecisePosition(this, method, data, I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".parameter", method.getMethodName(), paramName)); if (!method.isVoid() && !RETURN_PATTERN.matcher(commentContent).matches()) { method.getMethodName())); ASTNameList nameList = method.getThrows(); if (null != nameList) { List<ASTName> exceptions = nameList.findDescendantsOfType(ASTName.class); ViolationUtils.addViolationWithPrecisePosition(this, method, data, I18nResources.getMessage(MESSAGE_KEY_PREFIX + ".exception", method.getMethodName(), exceptionName));
@Override public Object visit(ASTMethodDeclaration node, Object data) { String opname = getOperationName(node.getMethodName(), node.getFirstDescendantOfType(ASTFormalParameters.class)); node.setQualifiedName(contextOperationQName(opname, false)); return super.visit(node, data); }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (node.isAbstract()) { return data; } List<ASTReturnStatement> returnNodes = node.findDescendantsOfType(ASTReturnStatement.class); if (returnNodes.size() > 1) { for (Iterator<ASTReturnStatement> i = returnNodes.iterator(); i.hasNext();) { Node problem = i.next(); // skip the last one, it's OK if (!i.hasNext()) { continue; } addViolation(data, problem); } } return data; } }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) { return super.visit(node, data); List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class); for (ASTIfStatement ifStatement : ifStatements) { if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
@Override public Object visit(final ASTMethodDeclaration meth, final Object data) { Object ndata = data; if (!meth.isAbstract() && !meth.isNative()) { ndata = super.visit(meth, data); } return ndata; }
if (!m.isPrivate()) { methodCount++; if (!m.isStatic()) { isOK = true; break; if (m.getMethodName().equals("suite")) { ASTResultType res = m.getResultType(); ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (c != null && c.hasImageEqualTo("Test")) {