Search in sources :

Example 6 with TryStatementTree

use of org.sonar.plugins.java.api.tree.TryStatementTree in project sonar-java by SonarSource.

the class TryWithResourcesCheck method leaveNode.

@Override
public void leaveNode(Tree tree) {
    if (tree.is(Tree.Kind.TRY_STATEMENT)) {
        TryStatementTree tryStatementTree = withinTry.pop();
        List<Tree> secondaryTrees = toReport.pop();
        if (!secondaryTrees.isEmpty()) {
            List<JavaFileScannerContext.Location> secondary = new ArrayList<>();
            for (Tree autoCloseable : secondaryTrees) {
                secondary.add(new JavaFileScannerContext.Location("AutoCloseable resource", autoCloseable));
            }
            reportIssue(tryStatementTree.tryKeyword(), "Change this \"try\" to a try-with-resources." + context.getJavaVersion().java7CompatibilityMessage(), secondary, null);
        }
    }
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) ArrayList(java.util.ArrayList) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree)

Aggregations

TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)6 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 ArrayList (java.util.ArrayList)2 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Rule (org.sonar.check.Rule)1 MethodMatcher (org.sonar.java.matcher.MethodMatcher)1 TypeCriteria (org.sonar.java.matcher.TypeCriteria)1 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Type (org.sonar.plugins.java.api.semantic.Type)1 BaseTreeVisitor (org.sonar.plugins.java.api.tree.BaseTreeVisitor)1 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1