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

The following examples show how to use org.sonar.plugins.java.api.tree.MethodTree. 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: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	if (!hasSemantic()) {
		return;
	}
	MethodTree methodTree = (MethodTree) tree;
	if (methodTree.block() == null || !isIncluded(methodTree)) {
		return;
	}
	List<IdentifierTree> unused = Lists.newArrayList();
	for (VariableTree var : methodTree.parameters()) {
		Symbol symbol = var.symbol();
		if (symbol.usages().isEmpty() && !symbol.metadata().isAnnotatedWith(AUTHORIZED_ANNOTATION)
				&& !isStrutsActionParameter(var)) {
			unused.add(var.simpleName());
		}
	}
	Set<String> unresolvedIdentifierNames = unresolvedIdentifierNames(methodTree.block());
	// kill the noise regarding unresolved identifiers, and remove the one with matching names from the list of
	// unused
	unused = unused.stream().filter(id -> !unresolvedIdentifierNames.contains(id.name()))
			.collect(Collectors.toList());
	if (!unused.isEmpty()) {
		reportUnusedParameters(unused);
	}
}
 
Example #2
Source File: SessionShouldBeLoggedOut.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
protected boolean checkIfLongSession(MethodTree method) {
    List<AnnotationTree> annotations = method.modifiers().annotations();
    for (AnnotationTree annotationTree : annotations) {
        if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
            IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
            if (idf.name().equals(ACTIVATE)) {
                collectLongSessionOpened(method);
                return true;
            } else if (idf.name().equals(DEACTIVATE)) {
                collectLongSessionClosed(method);
                return true;
            }
        }
    }
    return false;
}
 
Example #3
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	if (!hasSemantic()) {
		return;
	}
	switch (tree.kind()) {
	case METHOD:
		checkIfNativeMethod((MethodTree) tree);
		break;
	case CLASS:
		classes.add((ClassTree) tree);
		break;
	case IMPORT:// VJ
		checkIfLombokClass((ImportTree) tree);
		break;
	case EXPRESSION_STATEMENT:
		collectAssignment(((ExpressionStatementTree) tree).expression());
		break;
	case IDENTIFIER:
		collectUnknownIdentifier((IdentifierTree) tree);
		break;
	default:
		throw new IllegalStateException("Unexpected subscribed tree.");
	}
}
 
Example #4
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
protected boolean checkIfLongResourceResolver(MethodTree method) {
    List<AnnotationTree> annotations = method.modifiers().annotations();
    for (AnnotationTree annotationTree : annotations) {
        if (annotationTree.annotationType().is(Tree.Kind.IDENTIFIER)) {
            IdentifierTree idf = (IdentifierTree) annotationTree.annotationType();
            if (idf.name().equals(ACTIVATE)) {
                collectLongResourceResolverOpened(method);
                return true;
            } else if (idf.name().equals(DEACTIVATE)) {
                collectLongResourceResolverClosed(method);
                return true;
            }
        }
    }
    return false;
}
 
Example #5
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	if (!hasSemantic()) {
		return;
	}
	MethodTree methodTree = (MethodTree) tree;
	if (methodTree.block() == null || !isIncluded(methodTree)) {
		return;
	}
	List<IdentifierTree> unused = Lists.newArrayList();
	for (VariableTree var : methodTree.parameters()) {
		Symbol symbol = var.symbol();
		if (symbol.usages().isEmpty() && !symbol.metadata().isAnnotatedWith(AUTHORIZED_ANNOTATION)
				&& !isStrutsActionParameter(var)) {
			unused.add(var.simpleName());
		}
	}
	Set<String> unresolvedIdentifierNames = unresolvedIdentifierNames(methodTree.block());
	// kill the noise regarding unresolved identifiers, and remove the one with matching names from the list of
	// unused
	unused = unused.stream().filter(id -> !unresolvedIdentifierNames.contains(id.name()))
			.collect(Collectors.toList());
	if (!unused.isEmpty()) {
		reportUnusedParameters(unused);
	}
}
 
Example #6
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 6 votes vote down vote up
@Override
public void visitNode(Tree tree) {
	if (!hasSemantic()) {
		return;
	}
	switch (tree.kind()) {
	case METHOD:
		checkIfNativeMethod((MethodTree) tree);
		break;
	case CLASS:
		classes.add((ClassTree) tree);
		break;
	case IMPORT:// VJ
		checkIfLombokClass((ImportTree) tree);
		break;
	case EXPRESSION_STATEMENT:
		collectAssignment(((ExpressionStatementTree) tree).expression());
		break;
	case IDENTIFIER:
		collectUnknownIdentifier((IdentifierTree) tree);
		break;
	default:
		throw new IllegalStateException("Unexpected subscribed tree.");
	}
}
 
Example #7
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 #8
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
protected boolean checkIfResourceResolverIsClosed(MethodTree method, VariableTree injector) {
    Set<IdentifierTree> usagesOfRR = new HashSet<>(injector.symbol().usages());
    CheckClosedVisitor checkClosedVisitor = new CheckClosedVisitor(usagesOfRR);
    FinallyBlockVisitor finallyBlockVisitor = new FinallyBlockVisitor(checkClosedVisitor);
    method.accept(finallyBlockVisitor);
    return checkClosedVisitor.isClosed();
}
 
Example #9
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void collectLongResourceResolverClosed(MethodTree method) {
    if (longResourceResolvers != null) {
        for (VariableTree longResourceResolver : longResourceResolvers) {
            if (!checkIfResourceResolverIsClosed(method, longResourceResolver)) {
                context.reportIssue(this, longResourceResolver, RULE_MESSAGE);
            }
        }
    }
}
 
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 visitMethod(MethodTree tree) {
    FindDeclarationOfReturnedVariable visitor = new FindDeclarationOfReturnedVariable();
    tree.accept(visitor);
    declarationOfReturnedVariable = visitor.getDeclarationOfReturnedVariable();
    super.visitMethod(tree);
}
 
Example #11
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(MethodTree method) {
    if (!checkIfLongResourceResolver(method)) {
        Collection<VariableTree> resolvers = findResolversInMethod(method);
        for (VariableTree injector : resolvers) {
            if (!checkIfResourceResolverIsClosed(method, injector)) {
                context.reportIssue(this, injector, RULE_MESSAGE);
            }
        }
    }
    super.visitMethod(method);
}
 
Example #12
Source File: SessionShouldBeLoggedOut.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
protected boolean checkIfLoggedOut(MethodTree method, VariableTree injector) {
    Set<IdentifierTree> usagesOfSession = new HashSet<>(injector.symbol().usages());
    CheckLoggedOutVisitor checkLoggedOutVisitor = new CheckLoggedOutVisitor(usagesOfSession);
    FinallyBlockVisitor finallyBlockVisitor = new FinallyBlockVisitor(checkLoggedOutVisitor);
    method.accept(finallyBlockVisitor);
    return checkLoggedOutVisitor.isLoggedOut();
}
 
Example #13
Source File: SessionShouldBeLoggedOut.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private void collectLongSessionClosed(MethodTree method) {
    if (longSessions != null) {
        for (VariableTree longSession : longSessions) {
            if (!checkIfLoggedOut(method, longSession)) {
                context.reportIssue(this, longSession, RULE_MESSAGE);
            }
        }
    }
}
 
Example #14
Source File: ModifiableValueMapUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(MethodTree tree) {
    List<VariableTree> parameters = tree.parameters();
    if (argumentIndex >= 0 && argumentIndex <= (parameters.size() - 1)) {
        VariableTree variableTree = parameters.get(argumentIndex);
        scanner.checkIfMapVariableIsModified(variableTree.symbol().usages());
    }
    super.visitMethod(tree);
}
 
Example #15
Source File: SessionShouldBeLoggedOut.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(MethodTree method) {
    if (!checkIfLongSession(method)) {
        Collection<VariableTree> sessions = findSessionsInMethod(method);
        for (VariableTree session : sessions) {
            if (!checkIfLoggedOut(method, session)) {
                context.reportIssue(this, session, RULE_MESSAGE);
            }
        }
    }
    super.visitMethod(method);
}
 
Example #16
Source File: CommonUtil.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public static List<AnnotationTree> getAnnotationTrees(ClassTree tree) {
    List<AnnotationTree> annotationTrees = new ArrayList<>();
    List<Tree> members = tree.members();

    for (Tree member : members) {
        if (METHOD_KIND.equals(member.kind().toString())) {
            annotationTrees.addAll(((MethodTree) member).modifiers().annotations());
        }
    }

    return annotationTrees;
}
 
Example #17
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 #18
Source File: FindSessionDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(MethodTree tree) {
    FindDeclarationOfReturnedVariable visitor = new FindDeclarationOfReturnedVariable();
    tree.accept(visitor);
    declarationOfReturnedVariable = visitor.getDeclarationOfReturnedVariable();
    super.visitMethod(tree);
}
 
Example #19
Source File: SynchronizedKeywordUsageCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(MethodTree tree) {
    List<ModifierKeywordTree> modifiers = tree.modifiers().modifiers();
    for (ModifierKeywordTree modifier : modifiers) {
        if (modifier.modifier() == Modifier.SYNCHRONIZED) {
            visitor.reportIssue(modifier, MESSAGE);
        }
    }
    super.visitMethod(tree);
}
 
Example #20
Source File: MethodMatcherTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void givenMethodInvocationTree(String codeToParse) {
    CompilationUnitTree compilationUnitTree = parse(codeToParse);
    ClassTree classTree = (ClassTree) compilationUnitTree.types().get(CLASS_INDEX);
    StatementTree statementTree = ((MethodTree) classTree.members().get(CLASS_METHOD_INDEX)).block().body().get(METHOD_INVOCATION_INDEX);
    this.methodInvocationTree = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).expression();
}
 
Example #21
Source File: FindVariableDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMethod(MethodTree tree) {
    // omit all apart from block
    scan(tree.block());
}
 
Example #22
Source File: ContentResourceShouldBeNullCheckedCheck.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
@Override
public void visitMethod(MethodTree tree) {
    super.visitMethod(tree);
    returnOccurredInsideEqualNullCheck = false;
}
 
Example #23
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private static boolean isIncluded(MethodTree tree) {
	return isPrivateMethod(tree);
}
 
Example #24
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private MethodTree getMethodTree(MethodInvocationTree methodInvocation) {
    IdentifierTree method = (IdentifierTree) methodInvocation.methodSelect();
    return (MethodTree) getDeclaration(method);
}
 
Example #25
Source File: FindRRDeclarationVisitor.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private boolean isManuallyCreatedResourceResolver(MethodTree methodDeclaration) {
    CheckIfRRCreatedManually rrCreatedManually = new CheckIfRRCreatedManually();
    methodDeclaration.accept(rrCreatedManually);
    return rrCreatedManually.isCreatedManually();
}
 
Example #26
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private static boolean isPrivateMethod(MethodTree methodTree) {
	return ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.PRIVATE);
}
 
Example #27
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
protected Collection<VariableTree> findResolversInMethod(MethodTree methodTree) {
    FindRRDeclarationVisitor findVariableDeclarationVisitor = new FindRRDeclarationVisitor();
    methodTree.accept(findVariableDeclarationVisitor);
    return findVariableDeclarationVisitor.getDeclarations();
}
 
Example #28
Source File: UnusedPrivateFieldCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private void checkIfNativeMethod(MethodTree method) {
	if (ModifiersUtils.hasModifier(method.modifiers(), Modifier.NATIVE)) {
		hasNativeMethod = true;
	}
}
 
Example #29
Source File: UnusedMethodParameterCheck.java    From vjtools with Apache License 2.0 4 votes vote down vote up
private static boolean isIncluded(MethodTree tree) {
	return isPrivateMethod(tree);
}
 
Example #30
Source File: ResourceResolverShouldBeClosed.java    From AEM-Rules-for-SonarQube with Apache License 2.0 4 votes vote down vote up
private void collectLongResourceResolverOpened(MethodTree method) {
    longResourceResolvers = findResolversInMethod(method);
}