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

The following examples show how to use org.sonar.plugins.java.api.tree.ExpressionTree. 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: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 6 votes vote down vote up
private void checkLocator(ExpressionTree expressionTree, String locatorKey, String locatorValue,
    LocatorStrategyCheckType locatorStrategyCheckType) {
    switch (locatorStrategyCheckType) {
        case CLASS_NAME_VALUE:
            reportClassNameIssue(expressionTree, locatorKey, locatorValue);
            break;
        case ID_VALUE:
            reportIdIssue(expressionTree, locatorKey, locatorValue);
            break;
        case CSS_VALUE:
            reportCssIssue(expressionTree, locatorKey, locatorValue);
            break;
        case XPATH_VALUE:
            reportXpathIssue(expressionTree, locatorKey, locatorValue);
            break;
        case XPATH_LOCATOR_STRATEGY:
            reportXpathLocatorIssue(expressionTree, locatorKey);
            break;
        case LINK_TEXT_AND_TAG_NAME_LOCATOR_STRATEGY:
            reportLinkTextAndTagNameLocatorIssue(expressionTree, locatorKey);
            break;
    }
}
 
Example #2
Source File: AbstractSmellCheck.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static final String extractMessage(final ExpressionTree expressionTree) {
    String message = "";
    switch (expressionTree.kind()) {
        case STRING_LITERAL:
            message = ((LiteralTree) expressionTree).value();
            break;
        case PLUS:
            final BinaryExpressionTree bet = (BinaryExpressionTree) expressionTree;
            message = extractMessage(bet.leftOperand()) + extractMessage(bet.rightOperand());
            break;
        case IDENTIFIER:
            final IdentifierTree it = (IdentifierTree) expressionTree;
            message = extractMessage(((VariableTree) (it.symbol()
                .declaration())).initializer());
            break;
        default:
            LOGGER.warn("Cannot extract message due to unexpected expressionTree kind: {}", expressionTree.kind());
            break;
    }
    return trimQuotes(message);
}
 
Example #3
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportLinkTextAndTagNameLocatorIssue(ExpressionTree expressionTree, String locatorKey) {
    JavaFileScannerContext context = getContext();

    if (LOCATORS_TO_AVOID.contains(locatorKey)) {
        context.reportIssue(this, expressionTree,
            "Avoid using " + locatorKey + " locator, try using " + LOCATORS_RECOMMENDED.toString());
    }
}
 
Example #4
Source File: AbstractSmellCheck.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void handleSmellAnnotation(final AnnotationTree annotationTree) {
    String message = "";
    Integer minutes = 0;
    SmellType type = null;
    final Arguments arguments = annotationTree.arguments();
    for (final ExpressionTree expressionTree : arguments) {
        if (expressionTree.is(Tree.Kind.ASSIGNMENT)) {
            final AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
            final String variable = ((IdentifierTree) aet.variable()).name();
            switch (variable) {
                case "minutes":
                    minutes += Integer.valueOf(((LiteralTree) aet.expression()).value());
                    LOGGER.debug("{} = {}", variable, minutes);
                    break;
                case "reason":
                    message = extractMessage(aet.expression());
                    LOGGER.debug("{} = {}", variable, message);
                    break;
                case "type":
                    type = SmellType.valueOf(((MemberSelectExpressionTree) aet.expression()).identifier()
                        .name());
                    break;
                default:
                    break;
            }
        }
    }
    if (smellType().equals(type)) {
        final Matcher matcher = PATTERN.matcher(message);
        if (matcher.matches()) {
            message = matcher.group(1);
        }
        reportIssue(annotationTree, message, new ArrayList<JavaFileScannerContext.Location>(), minutes);
    }
}
 
Example #5
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private boolean isAssignmentToName(ExpressionTree expression) {
    boolean result = false;
    if (expression.is(Tree.Kind.ASSIGNMENT)) {
        AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
        result = NAME.equals(((IdentifierTree) assignment.variable()).name());
    }
    return result;
}
 
Example #6
Source File: PreferSlingServletAnnotation.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAnnotation(AnnotationTree annotationTree) {
    if (annotations.hasSlingServletAnnotation() && isPropertyAnnotation(annotationTree)) {
        for (ExpressionTree expression : annotationTree.arguments()) {
            if (isAssignmentToName(expression)) {
                checkIfPropertyInsteadSlingServletIsUsed(annotationTree, (AssignmentExpressionTree) expression);
            }
        }
    }
    super.visitAnnotation(annotationTree);
}
 
Example #7
Source File: DefaultInjectionStrategyAnnotationCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private boolean isOptionalDefaultValue(Arguments arguments) {
    boolean optionalIsDefaultValue = false;
    for (ExpressionTree argument : arguments) {
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) argument;
            if (isDefaultInjectionStrategyAssignment(assignment)) {
                optionalIsDefaultValue = isOptionalStrategyValue(assignment);
            }
        }
    }
    return optionalIsDefaultValue;
}
 
Example #8
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private void addAssignment(ExpressionTree tree) {
	ExpressionTree variable = ExpressionUtils.skipParentheses(tree);
	if (variable.is(Tree.Kind.IDENTIFIER)) {
		addAssignment((IdentifierTree) variable);
	} else if (variable.is(Tree.Kind.MEMBER_SELECT)) {
		addAssignment(((MemberSelectExpressionTree) variable).identifier());
	}
}
 
Example #9
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 #10
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private void addAssignment(ExpressionTree tree) {
	ExpressionTree variable = ExpressionUtils.skipParentheses(tree);
	if (variable.is(Tree.Kind.IDENTIFIER)) {
		addAssignment((IdentifierTree) variable);
	} else if (variable.is(Tree.Kind.MEMBER_SELECT)) {
		addAssignment(((MemberSelectExpressionTree) variable).identifier());
	}
}
 
Example #11
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 #12
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static Map<String, String> getLocatorValueMapInAnnotationTree(AnnotationTree annotationTree) {
    Map<String, String> locatorMap = new HashMap<>();
    String annotationType = annotationTree.annotationType().toString();
    String fullyQualifiedName = annotationTree.annotationType().symbolType().fullyQualifiedName();

    if (FIND_BY_ANNOTATION_NAME.equals(annotationType) &&
        isPartOfWebDriverPackage(fullyQualifiedName)) {
        for (ExpressionTree expressionTree : annotationTree.arguments()) {
            AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) expressionTree;

            String property = assignmentExpressionTree.variable().toString();
            String locator = HOW_PROPERTY.equals(property)
                ? ((MemberSelectExpressionTree) ((AssignmentExpressionTree) expressionTree)
                .expression()).identifier().name()
                : ((AssignmentExpressionTree) expressionTree).variable().toString();

            String propertyValue = assignmentExpressionTree.expression().is(Kind.STRING_LITERAL)
                ? ((LiteralTree) assignmentExpressionTree.expression()).value().replace("\"", "")
                : null;

            ExpressionTree howExpressionTree = annotationTree.arguments()
                .stream()
                .filter(aet -> HOW_PROPERTY.equals(((AssignmentExpressionTree) aet).variable().toString()))
                // Only one "how" property allowed in annotation
                .findFirst()
                .orElse(null);

            if (howExpressionTree != null && USING_PROPERTY.equals(property)) {
                String howLocator = ((MemberSelectExpressionTree) ((AssignmentExpressionTree) howExpressionTree)
                    .expression()).identifier().name();
                locatorMap.put(howLocator, propertyValue);
            } else {
                locatorMap.put(locator, propertyValue);
            }
        }
    }

    return locatorMap;
}
 
Example #13
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportClassNameIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (CLASS_NAME_LOCATORS.contains(locatorKey.toLowerCase()) && locatorValue.contains(SPACE)) {
        context.reportIssue(this, expressionTree,
            "Avoid compound class names with by class name locator strategy");
    }
}
 
Example #14
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportIdIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (ID_LOCATORS.contains(locatorKey.toLowerCase()) && !locatorValue.matches(VALID_ID_LOCATOR_REGEX)) {
        context.reportIssue(this, expressionTree,
            "Invalid id locator");
    }
}
 
Example #15
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportCssIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (CSS_LOCATORS.contains(locatorKey.toLowerCase()) && locatorValue.matches(AVOID_CSS_LOCATOR_REGEX)) {
        context.reportIssue(this, expressionTree,
            "Avoid using css locator tied to page layout");
    }
}
 
Example #16
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportXpathIssue(ExpressionTree expressionTree, String locatorKey, String locatorValue) {
    JavaFileScannerContext context = getContext();

    if (XPATH_LOCATORS.contains(locatorKey.toLowerCase()) && !locatorValue
        .matches(RECOMMENDED_XPATH_LOCATOR_REGEX)) {
        context.reportIssue(this, expressionTree,
            "Avoid using xpath locator tied to page layout");
    }
}
 
Example #17
Source File: BaseLocatorValueCheck.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
private void reportXpathLocatorIssue(ExpressionTree expressionTree, String locatorKey) {
    JavaFileScannerContext context = getContext();

    if (XPATH_LOCATORS.contains(locatorKey.toLowerCase())) {
        context.reportIssue(this, expressionTree,
            "Xpath locator is not recommended, try using " + LOCATORS_RECOMMENDED.toString());
    }
}
 
Example #18
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private void collectAssignment(ExpressionTree expressionTree) {
	if (expressionTree.is(ASSIGNMENT_KINDS)) {
		addAssignment(((AssignmentExpressionTree) expressionTree).variable());
	}
}
 
Example #19
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private void collectAssignment(ExpressionTree expressionTree) {
	if (expressionTree.is(ASSIGNMENT_KINDS)) {
		addAssignment(((AssignmentExpressionTree) expressionTree).variable());
	}
}