@Override public String getName() { return getMethodName(); }
@Override String nameExtractor(ASTMethodDeclaration node) { return node.getMethodName(); }
private boolean isAllowedMethod(ASTMethodDeclaration methodDeclaration) { if (getProperty(IGNORE_JUNIT_COMPLETELY_DESCRIPTOR)) { return true; } else { return methodDeclaration.getMethodName().equals("setUp") || methodDeclaration.getMethodName().equals("tearDown"); } }
private boolean isJUnit3Method(ASTMethodDeclaration method) { return isJUnit3Class && method.isVoid() && method.getMethodName().startsWith("test"); }
@Override public Object[] getViolationParameters(DataPoint point) { return new String[] { ((ASTMethodDeclaration) point.getNode()).getMethodName(), String.valueOf((int) point.getScore()), }; } }
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; }
private boolean isJunit3Test(ASTMethodDeclaration node) { if (!node.getMethodName().startsWith("test")) { return false; } // Considers anonymous classes, TODO with #905 this will be easier Node parent = node.getFirstParentOfAnyType(ASTEnumConstant.class, ASTAllocationExpression.class, ASTAnyTypeDeclaration.class); if (!(parent instanceof ASTClassOrInterfaceDeclaration) || ((ASTClassOrInterfaceDeclaration) parent).isInterface()) { return false; } ASTClassOrInterfaceType superClass = ((ASTClassOrInterfaceDeclaration) parent).getSuperClassTypeNode(); return superClass != null && TypeHelper.isA(superClass, "junit.framework.TestCase"); }
@Override public Object visit(ASTClassOrInterfaceBody node, Object data) { int n = node.jjtGetNumChildren(); List<ASTFieldDeclaration> fields = new ArrayList<>(); Set<String> methodNames = new HashSet<>(); for (int i = 0; i < n; i++) { Node child = node.jjtGetChild(i); if (child.jjtGetNumChildren() == 0) { continue; } child = child.jjtGetChild(child.jjtGetNumChildren() - 1); if (child instanceof ASTFieldDeclaration) { fields.add((ASTFieldDeclaration) child); } else if (child instanceof ASTMethodDeclaration) { methodNames.add(((ASTMethodDeclaration) child).getMethodName().toLowerCase(Locale.ROOT)); } } for (ASTFieldDeclaration field : fields) { String varName = field.getVariableName().toLowerCase(Locale.ROOT); if (methodNames.contains(varName)) { addViolation(data, field, field.getVariableName()); } } return super.visit(node, data); }
private boolean isMainMethod(ASTAnyTypeBodyDeclaration bodyDeclaration) { if (DeclarationKind.METHOD != bodyDeclaration.getKind()) { return false; } ASTMethodDeclaration decl = (ASTMethodDeclaration) bodyDeclaration.getDeclarationNode(); return decl.isStatic() && "main".equals(decl.getMethodName()) && decl.getResultType().isVoid() && decl.getFormalParameters().getParameterCount() == 1 && String[].class.equals(decl.getFormalParameters().iterator().next().getType()); }
@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); }
return ((ASTMethodDeclaration) parent).getMethodName();
@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); }
private String getNodeName(Node node) { // constructors are differentiated by their parameters, while we only use method name for methods if (node instanceof ASTMethodDeclaration) { return ((ASTMethodDeclaration) node).getMethodName(); } else if (node instanceof ASTMethodOrConstructorDeclaration) { // constructors are differentiated by their parameters, while we only use method name for methods return ((ASTConstructorDeclaration) node).getQualifiedName().getOperation(); } else if (node instanceof ASTFieldDeclaration) { return ((ASTFieldDeclaration) node).getVariableName(); } else if (node instanceof ASTResource) { return ((ASTResource) node).getVariableDeclaratorId().getImage(); } else { return node.getImage(); } }
if ("getInstance".equals(node.getMethodName())) { List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class); if (rsl.isEmpty()) {
@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); }
/** * Create a MethodHolder to hold the method. Store the MethodHolder in the * Map as the key Store each method called by the current method as a List * in the Map as the Object */ @Override public Object visit(ASTMethodDeclarator node, Object data) { if (!(getCurrentEvalPackage() instanceof NullEvalPackage)) { // only evaluate if we have an eval package for this class AccessNode parent = (AccessNode) node.jjtGetParent(); MethodHolder h = new MethodHolder(node); if (!parent.isAbstract() && !parent.isPrivate() && !parent.isStatic() && !parent.isFinal()) { // Skip abstract methods, have a separate rule for that h.setDangerous(); // this method is overridable ASTMethodDeclaration decl = node.getFirstParentOfType(ASTMethodDeclaration.class); h.setCalledMethod(decl.getMethodName()); } List<MethodInvocation> l = new ArrayList<>(); addCalledMethodsOfNode(parent, l, getCurrentEvalPackage().className); getCurrentEvalPackage().allMethodsOfClass.put(h, l); } return super.visit(node, data); }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (!hasIgnoredAnnotation(node)) { String nameOfMethod = node.getMethodName(); if (getProperty(CHECK_BOOLEAN_METHODS)) { checkBooleanMethods(node, data, nameOfMethod); } if (getProperty(CHECK_SETTERS)) { checkSetters(node, data, nameOfMethod); } if (getProperty(CHECK_GETTERS)) { checkGetters(node, data, nameOfMethod); } if (getProperty(CHECK_PREFIXED_TRANSFORM_METHODS)) { checkPrefixedTransformMethods(node, data, nameOfMethod); } if (getProperty(CHECK_TRANSFORM_METHODS)) { checkTransformMethods(node, data, nameOfMethod); } } return data; }
if (m.getMethodName().equals("suite")) { ASTResultType res = m.getResultType(); ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (CLONE.equals(node.getMethodName()) && node.isPublic() && !this.hasArguments(node) && this.isMethodType(node, OBJECT) && this.isMethodThrowingType(node, exceptions)) { return super.visit(node, data);
private boolean isJunit3Test(ASTMethodDeclaration node) { if (!node.getMethodName().startsWith("test")) { return false; } // Considers anonymous classes, TODO with #905 this will be easier Node parent = node.getFirstParentOfAnyType(ASTEnumConstant.class, ASTAllocationExpression.class, ASTAnyTypeDeclaration.class); if (!(parent instanceof ASTClassOrInterfaceDeclaration) || ((ASTClassOrInterfaceDeclaration) parent).isInterface()) { return false; } ASTClassOrInterfaceType superClass = ((ASTClassOrInterfaceDeclaration) parent).getSuperClassTypeNode(); return superClass != null && TypeHelper.isA(superClass, "junit.framework.TestCase"); }