use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class TreeFactory method newStatementExpressions.
public StatementExpressionListTreeImpl newStatementExpressions(ExpressionTree expression, Optional<List<Tuple<InternalSyntaxToken, ExpressionTree>>> rests) {
ImmutableList.Builder<StatementTree> statements = ImmutableList.builder();
statements.add(new ExpressionStatementTreeImpl(expression, null));
ImmutableList.Builder<SyntaxToken> separators = ImmutableList.builder();
if (rests.isPresent()) {
for (Tuple<InternalSyntaxToken, ExpressionTree> rest : rests.get()) {
separators.add(rest.first());
statements.add(new ExpressionStatementTreeImpl(rest.second(), null));
}
}
return new StatementExpressionListTreeImpl(statements.build(), separators.build());
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class TreeFactory method newArrayInitializer.
public NewArrayTreeImpl newArrayInitializer(InternalSyntaxToken openBraceToken, Optional<InternalSyntaxToken> optionalComma, Optional<List<Tuple<ExpressionTree, Optional<InternalSyntaxToken>>>> rests, InternalSyntaxToken closeBraceToken) {
ImmutableList.Builder<ExpressionTree> initializers = ImmutableList.builder();
ImmutableList.Builder<SyntaxToken> separators = ImmutableList.builder();
if (optionalComma.isPresent()) {
separators.add(optionalComma.get());
}
if (rests.isPresent()) {
for (Tuple<ExpressionTree, Optional<InternalSyntaxToken>> rest : rests.get()) {
initializers.add(rest.first());
if (rest.second().isPresent()) {
separators.add(rest.second().get());
}
}
}
return new NewArrayTreeImpl(ImmutableList.<ArrayDimensionTree>of(), new InitializerListTreeImpl(initializers.build(), separators.build())).completeWithCurlyBraces(openBraceToken, closeBraceToken);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class TreeFactory method newAnnotationTypeMethod.
public MethodTreeImpl newAnnotationTypeMethod(InternalSyntaxToken openParenToken, InternalSyntaxToken closeParenToken, Optional<Tuple<InternalSyntaxToken, ExpressionTree>> defaultValue) {
FormalParametersListTreeImpl parameters = new FormalParametersListTreeImpl(openParenToken, closeParenToken);
InternalSyntaxToken defaultToken = null;
ExpressionTree defaultExpression = null;
if (defaultValue.isPresent()) {
defaultToken = defaultValue.get().first();
defaultExpression = defaultValue.get().second();
}
return new MethodTreeImpl(parameters, defaultToken, defaultExpression);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildBasicBlock.
private UCFGBuilder.BlockBuilder buildBasicBlock(CFG.Block javaBlock, MethodTree methodTree, IdentifierGenerator idGenerator) {
UCFGBuilder.BlockBuilder blockBuilder = UCFGBuilder.newBasicBlock(String.valueOf(javaBlock.id()), location(javaBlock));
javaBlock.elements().forEach(e -> buildCall(e, blockBuilder, idGenerator));
Tree terminator = javaBlock.terminator();
if (terminator != null && terminator.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnedExpression = ((ReturnStatementTree) terminator).expression();
Expression retExpr = constant(IdentifierGenerator.CONST);
if (methodTree.returnType() != null && isString(methodTree.returnType().symbolType())) {
retExpr = idGenerator.lookupExpressionFor(returnedExpression);
}
blockBuilder.ret(retExpr, location(terminator));
return blockBuilder;
}
Set<CFG.Block> successors = javaBlock.successors();
if (!successors.isEmpty()) {
blockBuilder.jumpTo(successors.stream().map(b -> UCFGBuilder.createLabel(Integer.toString(b.id()))).toArray(Label[]::new));
return blockBuilder;
}
Preconditions.checkState(javaBlock.id() == 0);
blockBuilder.ret(constant("implicit return"), location(methodTree.lastToken()));
return blockBuilder;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class AccessorsUtils method referencePrivateProperty.
private static boolean referencePrivateProperty(ReturnStatementTree returnStatementTree, ClassTree classTree) {
ExpressionTree expression = returnStatementTree.expression();
String variableName = "";
if (expression == null) {
return false;
} else if (expression.is(Tree.Kind.IDENTIFIER)) {
variableName = ((IdentifierTree) expression).name();
}
return !StringUtils.isEmpty(variableName) && referencePrivateProperty(variableName, classTree);
}
Aggregations