Search in sources :

Example 1 with ModifierTree

use of org.sonar.plugins.java.api.tree.ModifierTree 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 ModifierTree

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

the class TreeFactory method newRequiresModuleDirective.

public ModuleDirectiveTree newRequiresModuleDirective(InternalSyntaxToken requiresToken, Optional<List<InternalSyntaxToken>> modifiers, ModuleNameTree moduleName, InternalSyntaxToken semicolonToken) {
    ModifiersTreeImpl newModifiers = ModifiersTreeImpl.emptyModifiers();
    if (modifiers.isPresent()) {
        List<ModifierTree> modifierKeywords = new ArrayList<>();
        // JLS9 - ยง7.7.1 'requires' only 'static' and 'transitive' modifiers are allowed
        for (InternalSyntaxToken modifierAsSyntaxToken : modifiers.get()) {
            if (JavaRestrictedKeyword.TRANSITIVE.getValue().equals(modifierAsSyntaxToken.text())) {
                modifierKeywords.add(new ModifierKeywordTreeImpl(Modifier.TRANSITIVE, modifierAsSyntaxToken));
            } else {
                modifierKeywords.add(new ModifierKeywordTreeImpl(Modifier.STATIC, modifierAsSyntaxToken));
            }
        }
        newModifiers = new ModifiersTreeImpl(modifierKeywords);
    }
    return new RequiresDirectiveTreeImpl(requiresToken, newModifiers, moduleName, semicolonToken);
}
Also used : ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) ModifierKeywordTreeImpl(org.sonar.java.model.declaration.ModifierKeywordTreeImpl) ArrayList(java.util.ArrayList) RequiresDirectiveTreeImpl(org.sonar.java.model.declaration.RequiresDirectiveTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) ModifiersTreeImpl(org.sonar.java.model.declaration.ModifiersTreeImpl)

Example 3 with ModifierTree

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

the class ModifiersOrderCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!alreadyReported.contains(tree)) {
        alreadyReported.add(tree);
        ModifierTree badlyOrderedModifier = getFirstBadlyOrdered((ModifiersTree) tree);
        if (badlyOrderedModifier != null) {
            reportIssue(badlyOrderedModifier, "Reorder the modifiers to comply with the Java Language Specification.");
        }
    }
}
Also used : ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree)

Example 4 with ModifierTree

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

the class ModifiersOrderCheck method getFirstBadlyOrdered.

private static ModifierTree getFirstBadlyOrdered(ModifiersTree modifiersTree) {
    ListIterator<ModifierTree> modifiersIterator = modifiersTree.listIterator();
    skipAnnotations(modifiersIterator);
    Modifier[] modifiers = Modifier.values();
    int modifierIndex = 0;
    while (modifiersIterator.hasNext()) {
        ModifierTree modifier = modifiersIterator.next();
        if (modifier.is(Kind.ANNOTATION)) {
            break;
        }
        ModifierKeywordTree mkt = (ModifierKeywordTree) modifier;
        for (; modifierIndex < modifiers.length && !mkt.modifier().equals(modifiers[modifierIndex]); modifierIndex++) {
        // We're just interested in the final value of modifierIndex
        }
        if (modifierIndex == modifiers.length) {
            return modifier;
        }
    }
    return testOnlyAnnotationsAreLeft(modifiersIterator);
}
Also used : ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) Modifier(org.sonar.plugins.java.api.tree.Modifier)

Example 5 with ModifierTree

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

the class SyntaxTreeDebug method methodString.

private static String methodString(MethodTree syntaxNode) {
    StringBuilder buffer = new StringBuilder();
    for (ModifierTree modifier : syntaxNode.modifiers()) {
        if (modifier.is(Tree.Kind.TOKEN)) {
            buffer.append(((SyntaxToken) modifier).text());
            buffer.append(' ');
        }
    }
    buffer.append(syntaxNode.returnType().symbolType().toString());
    buffer.append(' ');
    buffer.append(syntaxNode.simpleName().name());
    buffer.append('(');
    boolean first = true;
    for (VariableTree parameter : syntaxNode.parameters()) {
        if (first) {
            first = false;
        } else {
            buffer.append(", ");
        }
        buffer.append(parameter.type().symbolType().toString());
        buffer.append(' ');
        buffer.append(parameter.simpleName().toString());
    }
    buffer.append(')');
    return buffer.toString();
}
Also used : ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Aggregations

ModifierTree (org.sonar.plugins.java.api.tree.ModifierTree)5 Optional (com.sonar.sslr.api.typed.Optional)1 ArrayList (java.util.ArrayList)1 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)1 EnumConstantTreeImpl (org.sonar.java.model.declaration.EnumConstantTreeImpl)1 ModifierKeywordTreeImpl (org.sonar.java.model.declaration.ModifierKeywordTreeImpl)1 ModifiersTreeImpl (org.sonar.java.model.declaration.ModifiersTreeImpl)1 RequiresDirectiveTreeImpl (org.sonar.java.model.declaration.RequiresDirectiveTreeImpl)1 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)1 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)1 TypeArgumentListTreeImpl (org.sonar.java.model.expression.TypeArgumentListTreeImpl)1 Modifier (org.sonar.plugins.java.api.tree.Modifier)1 ModifierKeywordTree (org.sonar.plugins.java.api.tree.ModifierKeywordTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1