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

The following examples show how to use org.sonar.plugins.java.api.tree.AssignmentExpressionTree. 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: 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 #3
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 #4
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 #5
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 #6
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    if (isGetContentResourceUsedOnResource(tree)) {
        contentResources.put(tree.variable().firstToken().text(), false);
    }
    super.visitAssignmentExpression(tree);
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: ResourceResolverTryWithResourcesCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
  if (insideTryStatement && !insideLambdaExpression && isResourceResolver(tree)) {
    context.reportIssue(this, tree, RULE_MESSAGE);
  }
  super.visitAssignmentExpression(tree);
}
 
Example #12
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isVariableAnIdentifier(AssignmentExpressionTree tree) {
    return tree.variable().is(Kind.IDENTIFIER);
}
 
Example #13
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());
}
 
Example #14
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isResource(AssignmentExpressionTree tree) {
    return Constants.SLING_RESOURCE_QUALIFIED_NAME.equals(tree.variable().symbolType().fullyQualifiedName());
}
 
Example #15
Source File: ResourceResolverTryWithResourcesCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isResourceResolver(AssignmentExpressionTree tree) {
  return SLING_RESOURCE_RESOLVER.equals(tree.variable().symbolType().fullyQualifiedName());
}
 
Example #16
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean variableIsEqualToReturnedVariableIn(AssignmentExpressionTree tree) {
    return tree.variable().is(Kind.IDENTIFIER)
        && getDeclaration((IdentifierTree) tree.variable()).equals(declarationOfReturnedVariable);
}
 
Example #17
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isExpressionAMethodInvocation(AssignmentExpressionTree tree) {
    return tree.expression().is(Kind.METHOD_INVOCATION);
}
 
Example #18
Source File: DefaultInjectionStrategyAnnotationCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isDefaultInjectionStrategyAssignment(AssignmentExpressionTree assignment) {
    return DEFAULT_INJECTION_STRATEGY.equals(((IdentifierTree) assignment.variable()).name());
}
 
Example #19
Source File: DefaultInjectionStrategyAnnotationCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isOptionalStrategyValue(AssignmentExpressionTree assignment) {
    return OPTIONAL.equals(((MemberSelectExpressionTree) assignment.expression()).identifier().name());
}
 
Example #20
Source File: FindSessionDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isMethodInvocation(AssignmentExpressionTree tree) {
    return tree.expression().is(Kind.METHOD_INVOCATION);
}
 
Example #21
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 #22
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());
	}
}