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());
}
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);
}
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.");
}
}
}
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);
}
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();
}
Aggregations