Search in sources :

Example 1 with LabeledStatementTree

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

the class JavaTreeModelTest method labeled_statement.

/**
 * 14.7. Labeled Statements
 */
@Test
public void labeled_statement() {
    LabeledStatementTree tree = (LabeledStatementTree) firstMethodFirstStatement("class T { void m() { label: ; } }");
    assertThat(tree.is(Tree.Kind.LABELED_STATEMENT)).isTrue();
    assertThat(tree.label().name()).isEqualTo("label");
    assertThat(tree.statement()).isNotNull();
    assertThat(tree.colonToken().text()).isEqualTo(":");
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) Test(org.junit.Test)

Example 2 with LabeledStatementTree

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

the class SwitchWithLabelsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    CaseGroupTree cgt = (CaseGroupTree) tree;
    for (StatementTree statementTree : cgt.body()) {
        if (statementTree.is(LABELED_STATEMENT)) {
            IdentifierTree label = ((LabeledStatementTree) statementTree).label();
            reportIssue(label, "Remove this misleading \"" + label.name() + "\" label.");
        }
    }
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree)

Example 3 with LabeledStatementTree

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

the class LabelsVisitor method associateLabel.

private void associateLabel(@Nullable IdentifierTree label) {
    if (label == null) {
        return;
    }
    LabeledStatementTree labelTree = labelTrees.get(label.name());
    if (labelTree != null) {
        JavaSymbol symbol = (JavaSymbol) labelTree.symbol();
        ((IdentifierTreeImpl) label).setSymbol(symbol);
        symbol.addUsage(label);
    }
}
Also used : IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree)

Aggregations

LabeledStatementTree (org.sonar.plugins.java.api.tree.LabeledStatementTree)3 Test (org.junit.Test)1 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)1 CaseGroupTree (org.sonar.plugins.java.api.tree.CaseGroupTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)1