use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class RawExceptionCheck method visitThrowStatement.
@Override
public void visitThrowStatement(ThrowStatementTree tree) {
if (tree.expression().is(Tree.Kind.NEW_CLASS)) {
TypeTree exception = ((NewClassTree) tree.expression()).identifier();
Type symbolType = exception.symbolType();
if (symbolType instanceof MethodJavaType) {
symbolType = ((MethodJavaType) exception.symbolType()).resultType();
}
if (isRawException(symbolType)) {
reportIssue(exception);
}
}
super.visitThrowStatement(tree);
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class OSCommandInjectionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
Arguments arguments = mit.arguments();
if (RUNTIME_EXEC_MATCHER.matches(mit)) {
checkForIssue(tree, arguments.get(0));
} else if (PROCESS_BUILDER_COMMAND_MATCHER.matches(mit) && !arguments.isEmpty()) {
checkForIssue(tree, arguments);
}
} else if (((NewClassTree) tree).symbolType().is("java.lang.ProcessBuilder")) {
checkForIssue(tree, ((NewClassTree) tree).arguments());
}
}
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class FileLinesVisitor method computeExecutableLines.
private void computeExecutableLines(List<? extends Tree> trees) {
if (trees.isEmpty()) {
return;
}
// rely on cfg to get every instructions and get most of the token.
CFG cfg = CFG.buildCFG(trees);
cfg.blocks().stream().flatMap(b -> b.elements().stream()).forEach(t -> {
if (t.is(NEW_CLASS)) {
NewClassTree newClassTree = (NewClassTree) t;
new ExecutableLinesTokenVisitor().scanTree(newClassTree.identifier());
executableLines.add(newClassTree.newKeyword().line());
} else if (t.is(TRY_STATEMENT)) {
// add last token of try statements
executableLines.add(t.lastToken().line());
} else {
executableLines.add(t.firstToken().line());
}
});
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class DeadStoreCheck method checkElement.
private Set<Symbol> checkElement(Symbol.MethodSymbol methodSymbol, Set<Symbol> outVar, Set<Tree> assignmentLHS, Tree element) {
Set<Symbol> out = outVar;
switch(element.kind()) {
case PLUS_ASSIGNMENT:
case DIVIDE_ASSIGNMENT:
case MINUS_ASSIGNMENT:
case MULTIPLY_ASSIGNMENT:
case OR_ASSIGNMENT:
case XOR_ASSIGNMENT:
case AND_ASSIGNMENT:
case LEFT_SHIFT_ASSIGNMENT:
case RIGHT_SHIFT_ASSIGNMENT:
case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
case REMAINDER_ASSIGNMENT:
case ASSIGNMENT:
handleAssignment(out, assignmentLHS, (AssignmentExpressionTree) element);
break;
case IDENTIFIER:
handleIdentifier(out, assignmentLHS, (IdentifierTree) element);
break;
case VARIABLE:
handleVariable(out, (VariableTree) element);
break;
case NEW_CLASS:
handleNewClass(out, methodSymbol, (NewClassTree) element);
break;
case LAMBDA_EXPRESSION:
LambdaExpressionTree lambda = (LambdaExpressionTree) element;
out.addAll(getUsedLocalVarInSubTree(lambda.body(), methodSymbol));
break;
case METHOD_REFERENCE:
MethodReferenceTree methodRef = (MethodReferenceTree) element;
out.addAll(getUsedLocalVarInSubTree(methodRef.expression(), methodSymbol));
break;
case TRY_STATEMENT:
handleTryStatement(out, methodSymbol, (TryStatementTree) element);
break;
case PREFIX_DECREMENT:
case PREFIX_INCREMENT:
handlePrefixExpression(out, (UnaryExpressionTree) element);
break;
case POSTFIX_INCREMENT:
case POSTFIX_DECREMENT:
handlePostfixExpression(out, (UnaryExpressionTree) element);
break;
case CLASS:
case ENUM:
case ANNOTATION_TYPE:
case INTERFACE:
ClassTree classTree = (ClassTree) element;
out.addAll(getUsedLocalVarInSubTree(classTree, methodSymbol));
break;
default:
}
return out;
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class DiamondOperatorCheck method visitNode.
@Override
public void visitNode(Tree tree) {
NewClassTree newClassTree = (NewClassTree) tree;
TypeTree newTypeTree = newClassTree.identifier();
if (newClassTree.classBody() == null && isParameterizedType(newTypeTree)) {
TypeTree type = getTypeFromExpression(tree.parent(), expressionKindsToCheck);
if (type != null && isParameterizedType(type)) {
reportIssue(((ParameterizedTypeTree) newTypeTree).typeArguments(), "Replace the type specification in this constructor call with the diamond operator (\"<>\")." + context.getJavaVersion().java7CompatibilityMessage());
}
}
}
Aggregations