private boolean hasElseOrElseIf(final Node parentIfNode) { return parentIfNode.getFirstChildOfType(ASTElseStatement.class) != null || parentIfNode.getFirstChildOfType(ASTElseIfStatement.class) != null; }
/** * Gets the first child, first grand child, ... of the given types. * The children must follow the given order of types * * @param root the node from where to start the search * @param childrenTypes the list of types * @param <N> should match the last type of childrenType, otherwise you'll get a ClassCastException * @return the found child node or <code>null</code> */ @SafeVarargs private static <N extends Node> N getFirstChild(Node root, Class<? extends Node> ... childrenTypes) { Node current = root; for (Class<? extends Node> clazz : childrenTypes) { Node child = current.getFirstChildOfType(clazz); if (child != null) { current = child; } else { return null; } } @SuppressWarnings("unchecked") N result = (N) current; return result; }
public boolean isAnonymousClass() { return jjtGetParent().getFirstChildOfType(ASTClassOrInterfaceBody.class) != null; }
/** * Returns the the first Class declaration around the node. * * @param node The node with the enclosing Class declaration. * * @return The JavaTypeDefinition of the enclosing Class declaration. */ private TypeNode getEnclosingTypeDeclaration(Node node) { Node previousNode = null; while (node != null) { if (node instanceof ASTClassOrInterfaceDeclaration) { return (TypeNode) node; // anonymous class declaration } else if (node instanceof ASTAllocationExpression // is anonymous class declaration && node.getFirstChildOfType(ASTArrayDimsAndInits.class) == null // array cant be anonymous && !(previousNode instanceof ASTArguments)) { // we might come out of the constructor return (TypeNode) node; } previousNode = node; node = node.jjtGetParent(); } return null; }
/** * Search the list of thrown exceptions for Exception */ private void checkExceptions(Node method, Object o) { List<ASTName> exceptionList = Collections.emptyList(); ASTNameList nameList = method.getFirstChildOfType(ASTNameList.class); if (nameList != null) { exceptionList = nameList.findDescendantsOfType(ASTName.class); } if (!exceptionList.isEmpty()) { evaluateExceptions(exceptionList, o); } }
private ASTClassOrInterfaceType getTypeOfPrimaryPrefix(ASTPrimarySuffix node) { return node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class) .getFirstDescendantOfType(ASTClassOrInterfaceType.class); } }
private boolean isLocalVariableTypeInferred() { if (isResourceDeclaration()) { // covers "var" in try-with-resources return jjtGetParent().getFirstChildOfType(ASTType.class) == null; } else if (getNthParent(2) instanceof ASTLocalVariableDeclaration) { // covers "var" as local variables and in for statements return getNthParent(2).getFirstChildOfType(ASTType.class) == null; } return false; }
private boolean isLambdaTypeInferred() { return getNthParent(3) instanceof ASTLambdaExpression && jjtGetParent().getFirstChildOfType(ASTType.class) == null; }
public static List<JavaTypeDefinition> getMethodExplicitTypeArugments(Node node) { ASTMemberSelector memberSelector = node.getFirstChildOfType(ASTMemberSelector.class); if (memberSelector == null) { return Collections.emptyList(); } ASTTypeArguments typeArguments = memberSelector.getFirstChildOfType(ASTTypeArguments.class); if (typeArguments == null) { return Collections.emptyList(); } List<JavaTypeDefinition> result = new ArrayList<>(); for (int childIndex = 0; childIndex < typeArguments.jjtGetNumChildren(); ++childIndex) { result.add(((TypeNode) typeArguments.jjtGetChild(childIndex)).getTypeDefinition()); } return result; } }
public ASTDatatype getTypeNode() { if (jjtGetParent() instanceof ASTFormalParameter) { return ((ASTFormalParameter) jjtGetParent()).getTypeNode(); } else { Node n = jjtGetParent().jjtGetParent(); if (n instanceof ASTVariableOrConstantDeclaration || n instanceof ASTFieldDeclaration) { return n.getFirstChildOfType(ASTDatatype.class); } } throw new RuntimeException( "Don't know how to get the type for anything other than ASTLocalVariableDeclaration/ASTFormalParameter/ASTFieldDeclaration"); }
/** * Gets a list of NameDeclarations for all the fields that have type * ExpectedException and have a Rule annotation. * * @param classScope * The class scope to search for * @return See description */ private Map<String, List<NameOccurrence>> getRuleAnnotatedExpectedExceptions(Scope classScope) { Map<String, List<NameOccurrence>> result = new HashMap<>(); Map<NameDeclaration, List<NameOccurrence>> decls = classScope.getDeclarations(); for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) { Node parent = entry.getKey().getNode().jjtGetParent().jjtGetParent().jjtGetParent(); if (parent.hasDescendantOfType(ASTMarkerAnnotation.class) && parent.getFirstChildOfType(ASTFieldDeclaration.class) != null) { String annot = parent.getFirstDescendantOfType(ASTMarkerAnnotation.class).jjtGetChild(0).getImage(); if (!"Rule".equals(annot) && !"org.junit.Rule".equals(annot)) { continue; } Node type = parent.getFirstDescendantOfType(ASTReferenceType.class); if (!"ExpectedException".equals(type.jjtGetChild(0).getImage())) { continue; } result.put(entry.getKey().getName(), entry.getValue()); } } return result; }
/** * Report usages of assignments. * * @param checkIncrement true: check only '+=' and '-=', * false: check all other assignments * @param ignoreFlags which statements should be ignored */ private void checkAssignments(Object data, Set<String> loopVariables, ASTStatement loopBody, boolean checkIncrement, IgnoreFlags... ignoreFlags) { for (ASTAssignmentOperator operator : loopBody.findDescendantsOfType(ASTAssignmentOperator.class)) { // check if the current operator is an assign-increment or assign-decrement operator final String operatorImage = operator.getImage(); final boolean isIncrement = "+=".equals(operatorImage) || "-=".equals(operatorImage); if (isIncrement != checkIncrement) { // wrong type of operator continue; } if (ignoreNode(operator, loopBody, ignoreFlags)) { continue; } final ASTPrimaryExpression primaryExpression = operator.jjtGetParent().getFirstChildOfType(ASTPrimaryExpression.class); checkVariable(data, loopVariables, singleVariableName(primaryExpression)); } }
/** * This method can be called on a prefix */ private ASTArguments getSuffixMethodArgs(Node node) { Node prefix = node.jjtGetParent(); if (prefix instanceof ASTPrimaryPrefix && prefix.jjtGetParent().jjtGetNumChildren() >= 2) { return prefix.jjtGetParent().jjtGetChild(1).getFirstChildOfType(ASTArguments.class); } return null; }
private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) { ASTClassOrInterfaceType type = null; ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class) .getFirstChildOfType(ASTName.class); if (methodName != null) { ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class); Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations(); for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) { if (e.getKey().getName().equals(methodName.getImage())) { type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class) .getFirstChildOfType(ASTResultType.class) .getFirstDescendantOfType(ASTClassOrInterfaceType.class); break; } } } return type; }
private ASTTypeParameter getTypeParameterDeclaration(Node startNode, String image) { for (Node parent = startNode.jjtGetParent(); parent != null; parent = parent.jjtGetParent()) { ASTTypeParameters typeParameters = null; if (parent instanceof ASTTypeParameters) { // if type parameter defined in the same < > typeParameters = (ASTTypeParameters) parent; } else if (parent instanceof ASTConstructorDeclaration || parent instanceof ASTMethodDeclaration || parent instanceof ASTClassOrInterfaceDeclaration) { typeParameters = parent.getFirstChildOfType(ASTTypeParameters.class); } if (typeParameters != null) { for (int index = 0; index < typeParameters.jjtGetNumChildren(); ++index) { String imageToCompareTo = typeParameters.jjtGetChild(index).getImage(); if (imageToCompareTo != null && imageToCompareTo.equals(image)) { return (ASTTypeParameter) typeParameters.jjtGetChild(index); } } } } return null; }
private void setVariableNameIfExists(Node node) { if (node instanceof ASTFieldDeclaration) { variableName = getVariableNames((ASTFieldDeclaration) node); } else if (node instanceof ASTLocalVariableDeclaration) { variableName = getVariableNames((ASTLocalVariableDeclaration) node); } else if (node instanceof ASTVariableDeclarator) { variableName = node.jjtGetChild(0).getImage(); } else if (node instanceof ASTVariableDeclaratorId) { variableName = node.getImage(); } else if (node instanceof ASTFormalParameter) { setVariableNameIfExists(node.getFirstChildOfType(ASTVariableDeclaratorId.class)); } else { variableName = ""; } } }
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; }
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 int getConstructorAppendsLength(final Node node) { final Node parent = node.getFirstParentOfType(ASTVariableDeclarator.class); int size = 0; if (parent != null) { final Node initializer = parent.getFirstChildOfType(ASTVariableInitializer.class); if (initializer != null) { final Node primExp = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class); if (primExp != null) { for (int i = 0; i < primExp.jjtGetNumChildren(); i++) { final Node sn = primExp.jjtGetChild(i); if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) { continue; } size += processNode(sn); } } } } return size; }
@Override public Object visit(ASTVariableDeclarator node, Object data) { if (count > 1) { return super.visit(node, data); } ASTType type = node.jjtGetParent().getFirstChildOfType(ASTType.class); if (type != null) { Node reftypeNode = type.jjtGetChild(0); if (reftypeNode instanceof ASTReferenceType) { Node classOrIntType = reftypeNode.jjtGetChild(0); if (classOrIntType instanceof ASTClassOrInterfaceType) { Class<?> clazzType = ((ASTClassOrInterfaceType) classOrIntType).getType(); if (clazzType != null && (TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, LOG4J_LOGGER_NAME) || TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, JAVA_LOGGER_NAME) || TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, SLF4J_LOGGER_NAME)) || clazzType == null && "Logger".equals(classOrIntType.getImage())) { ++count; } } } } return super.visit(node, data); }