use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.
the class ReturnOfBooleanExpressionsCheck method getStatementTree.
private static StatementTree getStatementTree(IfStatementTree ifStatementTree) {
StatementTree elseStatementOrNextStatement = ifStatementTree.elseStatement();
if (elseStatementOrNextStatement == null) {
JavaTree parent = (JavaTree) ifStatementTree.parent();
List<Tree> children = parent.getChildren();
int indexOfIf = children.indexOf(ifStatementTree);
if (indexOfIf < children.size() - 1) {
// Defensive, this condition should always be true as if necessarily followed by a statement or a token.
Tree next = children.get(indexOfIf + 1);
if (!next.is(Kind.TOKEN)) {
elseStatementOrNextStatement = (StatementTree) next;
}
}
}
return elseStatementOrNextStatement;
}
use of org.sonar.java.model.JavaTree 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;
}
use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.
the class JavaParser method createParentLink.
private Tree createParentLink(JavaTree topParent) {
parentList.push(topParent);
while (!parentList.isEmpty()) {
JavaTree parent = parentList.pop();
if (!parent.isLeaf()) {
for (Tree nextTree : parent.getChildren()) {
JavaTree next = (JavaTree) nextTree;
if (next != null) {
next.setParent(parent);
parentList.push(next);
}
}
}
}
return topParent;
}
Aggregations