org.sonar.plugins.java.api.tree.MethodInvocationTree Java Examples

The following examples show how to use org.sonar.plugins.java.api.tree.MethodInvocationTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    if (isExpressionAMethodInvocation(tree) && isVariableAnIdentifier(tree)) {
        final MethodInvocationTree methodInvocation = (MethodInvocationTree) tree.expression();
        final IdentifierTree identifier = (IdentifierTree) tree.variable();
        if (isManuallyCreatedResourceResolver(methodInvocation)) {
            resourceResolvers.add((VariableTree) identifier.symbol().declaration());
        } else if (isResourceResolver(methodInvocation) && methodInvocation.methodSelect().is(Kind.IDENTIFIER)) {
            MethodTree methodDeclaration = getMethodTree(methodInvocation);
            // variable 'methodDeclaration' can be null in case when method declaration isn't within the same file.
            if (methodDeclaration != null && isManuallyCreatedResourceResolver(methodDeclaration)) {
                resourceResolvers.add((VariableTree) getDeclaration(identifier));
            }
        }
    }
    super.visitAssignmentExpression(tree);
}
 
Example #2
Source File: SlingQueryImplicitStrategyCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    // Method invocation from right to left
    if (isFindMethod(tree) && (slingQueryWasNotUsed() || isThisANewSlingQuery())) {
        slingQueries.replace(currentSlingQueryVariableName, SlingQueryStates.FIND_USED_WITHOUT_STRATEGY);
    } else if (isSearchStrategyMethod(tree)) {
        slingQueries.replace(currentSlingQueryVariableName, SlingQueryStates.STRATEGY_USED);
        ignoreIssues = true;
    }
    if (DOLLAR_SIGN.equals(tree.methodSelect().firstToken().text()) && isDollarCase()) {
        context.reportIssue(this, tree, RULE_MESSAGE);
    } else if (isFindMethod(tree)) {
        findMethodUsed = true;
    } else if (isSearchStrategyMethod(tree)) {
        searchStrategyMethodUsed = true;
    }
    super.visitMethodInvocation(tree);
}
 
Example #3
Source File: HardCodedSleepCheck.java    From sonar-webdriver-plugin with MIT License 6 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (!tree.methodSelect().is(Tree.Kind.IDENTIFIER)) {
        MemberSelectExpressionTree memberSelectExpressionTree = ((MemberSelectExpressionTree) tree.methodSelect());

        String methodName = memberSelectExpressionTree.identifier().name();
        String fullyQualifiedNameOfExpression = memberSelectExpressionTree.expression().symbolType()
            .fullyQualifiedName();

        if (SLEEP_METHOD_NAME.equals(methodName) &&
            fullyQualifiedNameOfExpression.equals(THREAD)) {
            context.reportIssue(this, tree, "Avoid using hard coded sleeps, use explicit wait instead");
        }
    }
}
 
Example #4
Source File: ImplicitWaitCheck.java    From sonar-webdriver-plugin with MIT License 6 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (!tree.methodSelect().is(Tree.Kind.IDENTIFIER)) {
        MemberSelectExpressionTree memberSelectExpressionTree = ((MemberSelectExpressionTree) tree.methodSelect());

        String methodName = memberSelectExpressionTree.identifier().name();
        String fullyQualifiedNameOfExpression = memberSelectExpressionTree.expression().symbolType()
            .fullyQualifiedName();

        if (IMPLICITLY_WAIT_METHOD_NAME.equals(methodName) &&
            isPartOfWebDriverPackage(fullyQualifiedNameOfExpression)) {
            context.reportIssue(this, tree, "Avoid using implicit wait, use explicit wait instead");
        }
    }
}
 
Example #5
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
	ExpressionTree methodSelect = tree.methodSelect();
	if (!methodSelect.is(Tree.Kind.IDENTIFIER)) {
		// not interested in simple method invocations, we are targeting usage of method parameters
		scan(methodSelect);
	}
	scan(tree.typeArguments());
	scan(tree.arguments());
}
 
Example #6
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
void checkMethodInvocationLocators(MethodInvocationTree tree, LocatorStrategyCheckType locatorStrategyCheckType) {
    Map<String, String> locatorsInMethodInvocationTree = getLocatorValueMapInMethodInvocationTree(tree);

    for (Map.Entry<String, String> locator : locatorsInMethodInvocationTree.entrySet()) {
        checkLocator(tree, locator.getKey(), locator.getValue(), locatorStrategyCheckType);
    }
}
 
Example #7
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static boolean isMethodIdentifier(IdentifierTree identifier) {
	Tree parent = identifier.parent();
	while (parent != null && !parent.is(Tree.Kind.METHOD_INVOCATION, Tree.Kind.METHOD_REFERENCE)) {
		parent = parent.parent();
	}
	if (parent == null) {
		return false;
	}
	if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
		return identifier.equals(methodName((MethodInvocationTree) parent));
	} else {
		return identifier.equals(((MethodReferenceTree) parent).method());
	}
}
 
Example #8
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private boolean isGetContentResourceUsedOnPage(VariableTree tree) {
    return tree.initializer() instanceof MethodInvocationTree &&
        !((MethodInvocationTree) tree.initializer()).symbol().isUnknown() &&
        isPage(((MethodInvocationTree) tree.initializer()).symbol().owner().type()
            .fullyQualifiedName()) &&
        GET_CONTENT_RESOURCE_METHOD.equals(((MethodInvocationTree) tree.initializer()).symbol().name());
}
 
Example #9
Source File: FindSessionDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    if (isMethodInvocation(tree)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) tree.expression();
        if (isManuallyCreatedSession(methodInvocation)) {
            IdentifierTree variable = (IdentifierTree) tree.variable();
            sessions.add((VariableTree) getDeclaration(variable));
        } else if (isSession(methodInvocation) && methodInvocation.methodSelect().is(Kind.IDENTIFIER)) {
            findSessionsCreatedInMethods(tree, methodInvocation);
        }
    }
    super.visitAssignmentExpression(tree);
}
 
Example #10
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void chainedMethodsNullCheck(MethodInvocationTree tree) {
    if (tree.methodSelect() instanceof MemberSelectExpressionTree) {
        MemberSelectExpressionTree method = (MemberSelectExpressionTree) tree.methodSelect();
        if (method.expression() instanceof MethodInvocationTree) {
            MethodInvocationTree invocation = (MethodInvocationTree) method.expression();
            if (!invocation.symbol().isUnknown() &&
                isPage(invocation.symbol().owner().type().fullyQualifiedName()) &&
                isGetContentResourceUsedOnPage(invocation) &&
                !returnOccurredInsideEqualNullCheck) {
                context.reportIssue(this, tree, RULE_MESSAGE);
            }
        }
    }
}
 
Example #11
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void externalLibraryNullChecks(MethodInvocationTree tree) {
    if (isNonNullUsed(tree)) {
        contentResources.replace(tree.arguments().get(METHOD_FIRST_ARGUMENT).toString(), true);
    } else if (isAllNonNullUsed(tree)) {
        tree.arguments().forEach(contentResource -> contentResources.replace(contentResource.toString(), true));
    } else if (insideIfStatement && isThisIsNullMethod(tree)) {
        insideEqualNullCheck = true;
    }
}
 
Example #12
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void equalsNullCheck(MethodInvocationTree tree) {
    Boolean firstTokenText = contentResources.getOrDefault(tree.firstToken().text(), true);
    if (Boolean.FALSE.equals(firstTokenText) &&
        !returnOccurredInsideEqualNullCheck) {
        context.reportIssue(this, tree, RULE_MESSAGE);
    }
}
 
Example #13
Source File: FindSessionDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    if (isMethodInvocation(tree) && getDeclaration((IdentifierTree) tree.variable()).equals(declarationOfReturnedVariable)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) tree.expression();
        if (isManuallyCreatedSession(methodInvocation)) {
            this.createdManually = true;
        } else {
            CheckIfSessionCreatedManually sessionCreatedManually = new CheckIfSessionCreatedManually();
            getMethodTree(methodInvocation).accept(sessionCreatedManually);
            this.createdManually = sessionCreatedManually.isCreatedManually();
        }
    }
    super.visitAssignmentExpression(tree);
}
 
Example #14
Source File: AdministrativeAccessUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void checkInvocation(Tree tree, MethodMatcher invocationMatcher) {
    if (tree.is(Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
        if (invocationMatcher.matches(methodInvocationTree)) {
            this.onMethodInvocationFound(methodInvocationTree);
        }
    }
}
 
Example #15
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    externalLibraryNullChecks(tree);
    equalsNullCheck(tree);
    chainedMethodsNullCheck(tree);
    super.visitMethodInvocation(tree);
}
 
Example #16
Source File: ModifiableValueMapUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void visitMethodWithMVM(IdentifierTree modifiableValueMapUsageIdentifier, Tree usageOfMVM) {
    int argumentNumber = ((Arguments) usageOfMVM).indexOf(modifiableValueMapUsageIdentifier);
    MethodInvocationTree methodInvocationWithMVM = (MethodInvocationTree) usageOfMVM.parent();
    if (methodInvocationWithMVM != null) {
        MethodTree methodWithMVM = (MethodTree) methodInvocationWithMVM.symbol().declaration();
        if (methodWithMVM != null && methodWithMVM.is(Tree.Kind.METHOD)) {
            MethodWithMVMVisitor methodWithMVMVisitor = new MethodWithMVMVisitor(this, argumentNumber);
            methodWithMVM.accept(methodWithMVMVisitor);
        }
    }
}
 
Example #17
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    if (isExpressionAMethodInvocation(tree) && variableIsEqualToReturnedVariableIn(tree)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) tree.expression();
        if (isManuallyCreatedResourceResolver(methodInvocation)) {
            this.createdManually = true;
        } else {
            CheckIfRRCreatedManually rrCreatedManually = new CheckIfRRCreatedManually();
            getMethodTree(methodInvocation).accept(rrCreatedManually);
            this.createdManually = rrCreatedManually.isCreatedManually();
        }
    }
    super.visitAssignmentExpression(tree);
}
 
Example #18
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariable(VariableTree tree) {
    if (isAMethodInvocation(tree) && variableIsEqualToReturnedVariableIn(tree)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) tree.initializer();
        if (isManuallyCreatedResourceResolver(methodInvocation)) {
            this.createdManually = true;
        } else {
            CheckIfRRCreatedManually rrCreatedManually = new CheckIfRRCreatedManually();
            getMethodTree(methodInvocation).accept(rrCreatedManually);
            this.createdManually = rrCreatedManually.isCreatedManually();
        }
    }
    super.visitVariable(tree);
}
 
Example #19
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
	ExpressionTree methodSelect = tree.methodSelect();
	if (!methodSelect.is(Tree.Kind.IDENTIFIER)) {
		// not interested in simple method invocations, we are targeting usage of method parameters
		scan(methodSelect);
	}
	scan(tree.typeArguments());
	scan(tree.arguments());
}
 
Example #20
Source File: AssertionInNonTestCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (methodInvocationIsAssertion(tree)) {
        context.reportIssue(this, tree, "Should not use assertions in non-test classes.");
    }
}
 
Example #21
Source File: MethodMatcher.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private static IdentifierTree getIdentifier(MethodInvocationTree methodInvocationTree) {
    // methodSelect can only be the Tree.Kind.IDENTIFIER or the Tree.Kind.MEMBER_SELECT
    if (methodInvocationTree.methodSelect().is(Tree.Kind.IDENTIFIER)) {
        return (IdentifierTree) methodInvocationTree.methodSelect();
    }
    return ((MemberSelectExpressionTree) methodInvocationTree.methodSelect()).identifier();
}
 
Example #22
Source File: WebDriverCommandInTestCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    JavaFileScannerContext context = getContext();

    if (methodInvocationIsPartOfWebDriverPackage(tree)) {
        context.reportIssue(this, tree, "Should not use WebDriver commands in Test classes.");
    }
}
 
Example #23
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static Map<String, String> getLocatorValueMapInMethodInvocationTree(MethodInvocationTree tree) {
    Map<String, String> methodInvocationLocatorValueMap = new HashMap<>();

    if (methodInvocationIsElementFinder(tree) && !tree.arguments().isEmpty()) {
        String locatorStrategy = getIdentifier(tree).name();
        String locatorValue = ((LiteralTree) tree.arguments().get(0)).value().replace("\"", "");

        methodInvocationLocatorValueMap.put(locatorStrategy, locatorValue);
    }

    return methodInvocationLocatorValueMap;
}
 
Example #24
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static boolean methodInvocationIsPartOfWebDriverPackage(MethodInvocationTree methodInvocationTree) {
    if (getIdentifier(methodInvocationTree).symbol().isUnknown()) {
        return false;
    }

    String fullyQualifiedName = methodInvocationTree.symbol().owner().type().fullyQualifiedName();

    return isPartOfWebDriverPackage(fullyQualifiedName);
}
 
Example #25
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private static boolean methodInvocationIsElementFinder(MethodInvocationTree methodInvocationTree) {
    if (getIdentifier(methodInvocationTree).symbol().isUnknown()) {
        return false;
    }

    String methodName = getIdentifier(methodInvocationTree).name();
    String ownerName = getIdentifier(methodInvocationTree).symbol().owner().name();

    return (methodName.matches(FIND_ELEMENT_METHOD_REGEX) || ownerName.equals(BY_OBJECT_NAME)) &&
        methodInvocationIsPartOfWebDriverPackage(methodInvocationTree);
}
 
Example #26
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static boolean methodInvocationIsAssertion(MethodInvocationTree methodInvocationTree) {
    if (getIdentifier(methodInvocationTree).symbol().isUnknown()) {
        return false;
    }
    String methodName = getIdentifier(methodInvocationTree).name();

    String fullyQualifiedName = methodInvocationTree.symbol().owner().type().fullyQualifiedName();

    return methodIsAssertion(methodName) && isPartOfAssertionPackage(fullyQualifiedName);
}
 
Example #27
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private static IdentifierTree getIdentifier(MethodInvocationTree methodInvocationTree) {
    // methodSelect can only be Tree.Kind.IDENTIFIER or Tree.Kind.MEMBER_SELECT
    if (methodInvocationTree.methodSelect().is(Tree.Kind.IDENTIFIER)) {
        return (IdentifierTree) methodInvocationTree.methodSelect();
    }
    return ((MemberSelectExpressionTree) methodInvocationTree.methodSelect()).identifier();
}
 
Example #28
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static boolean isMethodIdentifier(IdentifierTree identifier) {
	Tree parent = identifier.parent();
	while (parent != null && !parent.is(Tree.Kind.METHOD_INVOCATION, Tree.Kind.METHOD_REFERENCE)) {
		parent = parent.parent();
	}
	if (parent == null) {
		return false;
	}
	if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
		return identifier.equals(methodName((MethodInvocationTree) parent));
	} else {
		return identifier.equals(((MethodReferenceTree) parent).method());
	}
}
 
Example #29
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isGetContentResourceUsedOnPage(MethodInvocationTree tree) {
    return GET_CONTENT_RESOURCE_METHOD.equals(tree.methodSelect().lastToken().text());
}
 
Example #30
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isGetContentResourceUsedOnResource(AssignmentExpressionTree tree) {
    return isResource(tree) &&
        tree.expression() instanceof MethodInvocationTree &&
        GET_CONTENT_RESOURCE_METHOD.equals(((MethodInvocationTree) tree.expression()).symbol().name());
}