org.sonar.plugins.java.api.tree.Tree.Kind Java Examples

The following examples show how to use org.sonar.plugins.java.api.tree.Tree.Kind. 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: CatchUsesExceptionWithContextCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
    IdentifierTree identifier = null;
    ExpressionTree expression = tree.expression();
    if (expression.is(Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) expression;
    } else if (expression.is(Kind.PARENTHESIZED_EXPRESSION) && ((ParenthesizedTree) expression).expression().is(Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) ((ParenthesizedTree) expression).expression();
    }
    if (!validUsagesStack.isEmpty() && identifier != null) {
        Iterator<Collection<IdentifierTree>> iterator = validUsagesStack.iterator();
        while (iterator.hasNext()) {
            iterator.next().remove(identifier);
        }
    }
    super.visitMemberSelectExpression(tree);

}
 
Example #2
Source File: CatchUsesExceptionWithContextCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
public static String concatenate(ExpressionTree tree) {
  if(tree == null) {
    return "";
  }
  Deque<String> pieces = new LinkedList<>();
  ExpressionTree expr = tree;
  while (expr.is(Tree.Kind.MEMBER_SELECT)) {
    MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
    pieces.push(mse.identifier().name());
    pieces.push(".");
    expr = mse.expression();
  }
  if (expr.is(Tree.Kind.IDENTIFIER)) {
    IdentifierTree idt = (IdentifierTree) expr;
    pieces.push(idt.name());
  }

  StringBuilder sb = new StringBuilder();
  for (String piece: pieces) {
    sb.append(piece);
  }
  return sb.toString();
}
 
Example #3
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 #4
Source File: CatchUsesExceptionWithContextCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
    IdentifierTree identifier = null;
    ExpressionTree expression = tree.expression();
    if (expression.is(Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) expression;
    } else if (expression.is(Kind.PARENTHESIZED_EXPRESSION) && ((ParenthesizedTree) expression).expression().is(Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) ((ParenthesizedTree) expression).expression();
    }
    if (!validUsagesStack.isEmpty() && identifier != null) {
        Iterator<Collection<IdentifierTree>> iterator = validUsagesStack.iterator();
        while (iterator.hasNext()) {
            iterator.next().remove(identifier);
        }
    }
    super.visitMemberSelectExpression(tree);

}
 
Example #5
Source File: CatchUsesExceptionWithContextCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
public static String concatenate(ExpressionTree tree) {
  if(tree == null) {
    return "";
  }
  Deque<String> pieces = new LinkedList<>();
  ExpressionTree expr = tree;
  while (expr.is(Tree.Kind.MEMBER_SELECT)) {
    MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
    pieces.push(mse.identifier().name());
    pieces.push(".");
    expr = mse.expression();
  }
  if (expr.is(Tree.Kind.IDENTIFIER)) {
    IdentifierTree idt = (IdentifierTree) expr;
    pieces.push(idt.name());
  }

  StringBuilder sb = new StringBuilder();
  for (String piece: pieces) {
    sb.append(piece);
  }
  return sb.toString();
}
 
Example #6
Source File: ThreadSafeFieldCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void checkMember(Tree member) {
    boolean isVariableField = member.is(Kind.VARIABLE);
    if (isVariableField) {
        VariableTree variableField = (VariableTree) member;
        String name = variableField.type().symbolType().fullyQualifiedName();
        if (NON_THREAD_SAFE_TYPES.contains(name)) {
            context.reportIssue(this, member, String.format(RULE_MESSAGE, name));
        }
    }
}
 
Example #7
Source File: SynchronizedKeywordUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitNode(Tree tree) {
    Kind i = tree.kind();
    if (i == Kind.METHOD) {
        SynchronizedMethodVisitor visitor = new SynchronizedMethodVisitor(this);
        tree.accept(visitor);
    } else if (i == Kind.SYNCHRONIZED_STATEMENT) {
        reportIssue(tree, MESSAGE);
    }
    super.visitNode(tree);
}
 
Example #8
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 #9
Source File: FindSessionDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
    if (tree.expression() != null && tree.expression().is(Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) tree.expression();
        Tree declaration = getDeclaration(identifier);
        if (sessions.contains(declaration)) {
            sessions.remove(declaration);
        }
    }
    super.visitReturnStatement(tree);
}
 
Example #10
Source File: FindSessionDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
    if (tree.expression() != null && tree.expression().is(Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) tree.expression();
        declarationOfReturnedVariable = getDeclaration(identifier);
    }
    super.visitReturnStatement(tree);
}
 
Example #11
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 #12
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 #13
Source File: AnnotationsConstantsCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitLiteral(LiteralTree tree) {
    if (inAnnotation && tree.is(Kind.STRING_LITERAL)) {
        String literalValue = removeQuotes(tree.value());
        if (ConstantsChecker.isAnnotationConstant(literalValue)) {
            context.reportIssue(this, tree,
                String.format("Use constant %s instead of hardcoded value.", ConstantsChecker.getAnnotationMessageForConstant(literalValue)));
        }
    }
    super.visitLiteral(tree);
}
 
Example #14
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
    if (tree.expression() != null && tree.expression().is(Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) tree.expression();
        Tree declaration = identifier.symbol().declaration();
        if (resourceResolvers.contains(declaration)) {
            resourceResolvers.remove(declaration);
        }
    }
    super.visitReturnStatement(tree);
}
 
Example #15
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private boolean isAMethodInvocation(VariableTree tree) {
    boolean returnVal = false;
    if (tree.initializer() != null) {
        returnVal = tree.initializer().is(Kind.METHOD_INVOCATION);
    }
    return returnVal;
}
 
Example #16
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
    if (tree.expression() != null && tree.expression().is(Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) tree.expression();
        declarationOfReturnedVariable = getDeclaration(identifier);
    }
    super.visitReturnStatement(tree);
}
 
Example #17
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 #18
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 #19
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 #20
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 #21
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static String fullQualifiedName(Tree tree) {
	if (tree.is(Tree.Kind.IDENTIFIER)) {
		return ((IdentifierTree) tree).name();
	} else if (tree.is(Tree.Kind.MEMBER_SELECT)) {
		MemberSelectExpressionTree m = (MemberSelectExpressionTree) tree;
		return fullQualifiedName(m.expression()) + "." + m.identifier().name();
	}
	throw new UnsupportedOperationException(String.format("Kind/Class '%s' not supported", tree.getClass()));
}
 
Example #22
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 #23
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 5 votes vote down vote up
private static String fullQualifiedName(Tree tree) {
	if (tree.is(Tree.Kind.IDENTIFIER)) {
		return ((IdentifierTree) tree).name();
	} else if (tree.is(Tree.Kind.MEMBER_SELECT)) {
		MemberSelectExpressionTree m = (MemberSelectExpressionTree) tree;
		return fullQualifiedName(m.expression()) + "." + m.identifier().name();
	}
	throw new UnsupportedOperationException(String.format("Kind/Class '%s' not supported", tree.getClass()));
}
 
Example #24
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 #25
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private void checkClassFields(ClassTree classTree) {
	classTree.members().stream().filter(member -> member.is(Tree.Kind.VARIABLE)).map(VariableTree.class::cast)
			.forEach(this::checkIfUnused);
}
 
Example #26
Source File: AbstractSmellCheck.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<Kind> nodesToVisit() {
    return ImmutableList.<Kind> of(Kind.ANNOTATION);
}
 
Example #27
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isNullLiteral(Tree operand) {
    return Kind.NULL_LITERAL.equals(operand.kind());
}
 
Example #28
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isResourceInitialized(VariableTree tree) {
    return tree.initializer() != null && tree.initializer().kind() != Kind.NULL_LITERAL;
}
 
Example #29
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void updateNullCheckedResources(IfStatementTree tree, boolean value) {
    boolean rightOperandIsNull = Kind.NULL_LITERAL.equals(tree.condition().lastToken().parent().kind());
    SyntaxToken variable = rightOperandIsNull ? tree.condition().firstToken() : tree.condition().lastToken();
    contentResources.replace(variable.text(), value);
}
 
Example #30
Source File: ConstantsCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public List<Kind> nodesToVisit() {
    return List.of(Kind.STRING_LITERAL);
}