Search in sources :

Example 1 with EnumConstantTreeImpl

use of org.sonar.java.model.declaration.EnumConstantTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method newEnumConstant.

public EnumConstantTreeImpl newEnumConstant(Optional<List<AnnotationTreeImpl>> annotations, InternalSyntaxToken identifierToken, Optional<ArgumentListTreeImpl> arguments, Optional<ClassTreeImpl> classBody, Optional<InternalSyntaxToken> commaToken) {
    IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
    ArgumentListTreeImpl defaultArguments = new ArgumentListTreeImpl(ImmutableList.<ExpressionTree>of(), ImmutableList.<SyntaxToken>of());
    NewClassTreeImpl newClass = new NewClassTreeImpl(arguments.or(defaultArguments), classBody.orNull());
    newClass.completeWithIdentifier(identifier);
    return new EnumConstantTreeImpl(modifiers((Optional<List<ModifierTree>>) (Optional<?>) annotations), identifier, newClass, commaToken.orNull());
}
Also used : ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) Optional(com.sonar.sslr.api.typed.Optional) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) EnumConstantTreeImpl(org.sonar.java.model.declaration.EnumConstantTreeImpl) TypeArgumentListTreeImpl(org.sonar.java.model.expression.TypeArgumentListTreeImpl)

Example 2 with EnumConstantTreeImpl

use of org.sonar.java.model.declaration.EnumConstantTreeImpl in project sonar-java by SonarSource.

the class EnumConstantTest method test_annotation.

@Test
public void test_annotation() {
    LexerlessGrammarBuilder b = JavaLexer.createGrammarBuilder();
    ActionParser<Tree> parser = new ActionParser<>(StandardCharsets.UTF_8, b, JavaGrammar.class, new TreeFactory(), new JavaNodeBuilder(), JavaLexer.ENUM_CONSTANT);
    EnumConstantTreeImpl node = (EnumConstantTreeImpl) parser.parse("@Foo CONSTANT");
    assertThat(node.modifiers().size()).isEqualTo(1);
    assertThat(((IdentifierTree) ((AnnotationTreeImpl) node.modifiers().get(0)).annotationType()).identifierToken().text()).isEqualTo("Foo");
}
Also used : AnnotationTreeImpl(org.sonar.java.model.declaration.AnnotationTreeImpl) ActionParser(com.sonar.sslr.api.typed.ActionParser) TreeFactory(org.sonar.java.ast.parser.TreeFactory) LexerlessGrammarBuilder(org.sonar.sslr.grammar.LexerlessGrammarBuilder) JavaNodeBuilder(org.sonar.java.ast.parser.JavaNodeBuilder) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) EnumConstantTreeImpl(org.sonar.java.model.declaration.EnumConstantTreeImpl) Test(org.junit.Test)

Example 3 with EnumConstantTreeImpl

use of org.sonar.java.model.declaration.EnumConstantTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method newEnumDeclaration.

public ClassTreeImpl newEnumDeclaration(InternalSyntaxToken enumToken, InternalSyntaxToken identifierToken, Optional<Tuple<InternalSyntaxToken, QualifiedIdentifierListTreeImpl>> implementsClause, InternalSyntaxToken openBraceToken, Optional<List<EnumConstantTreeImpl>> enumConstants, Optional<InternalSyntaxToken> semicolonToken, Optional<List<JavaTree>> enumDeclarations, InternalSyntaxToken closeBraceToken) {
    List<JavaTree> members = Lists.newLinkedList();
    EnumConstantTreeImpl lastEnumConstant = null;
    if (enumConstants.isPresent()) {
        for (EnumConstantTreeImpl enumConstant : enumConstants.get()) {
            members.add(enumConstant);
            lastEnumConstant = enumConstant;
        }
    }
    if (semicolonToken.isPresent()) {
        InternalSyntaxToken semicolon = semicolonToken.get();
        // add the semicolon as endToken of the last enumConstant, or as empty statement in the enum members
        if (lastEnumConstant != null) {
            lastEnumConstant.setEndToken(semicolon);
        } else {
            members.add(newEmptyMember(semicolon));
        }
    }
    if (enumDeclarations.isPresent()) {
        for (JavaTree enumDeclaration : enumDeclarations.get()) {
            members.add(enumDeclaration);
        }
    }
    ClassTreeImpl result = newClassBody(Kind.ENUM, openBraceToken, Optional.of((List<JavaTree>) ImmutableList.<JavaTree>builder().addAll(members).build()), closeBraceToken);
    result.completeDeclarationKeyword(enumToken);
    IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
    result.completeIdentifier(identifier);
    if (implementsClause.isPresent()) {
        InternalSyntaxToken implementsKeyword = implementsClause.get().first();
        QualifiedIdentifierListTreeImpl interfaces = implementsClause.get().second();
        result.completeInterfaces(implementsKeyword, interfaces);
    }
    return result;
}
Also used : ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) EnumConstantTreeImpl(org.sonar.java.model.declaration.EnumConstantTreeImpl) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) JavaTree(org.sonar.java.model.JavaTree)

Aggregations

EnumConstantTreeImpl (org.sonar.java.model.declaration.EnumConstantTreeImpl)3 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)2 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)2 ImmutableList (com.google.common.collect.ImmutableList)1 ActionParser (com.sonar.sslr.api.typed.ActionParser)1 Optional (com.sonar.sslr.api.typed.Optional)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1 JavaNodeBuilder (org.sonar.java.ast.parser.JavaNodeBuilder)1 TreeFactory (org.sonar.java.ast.parser.TreeFactory)1 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)1 JavaTree (org.sonar.java.model.JavaTree)1 AnnotationTreeImpl (org.sonar.java.model.declaration.AnnotationTreeImpl)1 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)1 TypeArgumentListTreeImpl (org.sonar.java.model.expression.TypeArgumentListTreeImpl)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 ModifierTree (org.sonar.plugins.java.api.tree.ModifierTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 LexerlessGrammarBuilder (org.sonar.sslr.grammar.LexerlessGrammarBuilder)1