use of org.sonar.plugins.java.api.tree.TypeTree 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.TypeTree in project sonar-java by SonarSource.
the class TreeFactory method annotationIdentifier.
public TypeTree annotationIdentifier(InternalSyntaxToken firstIdentifier, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> rests) {
List<InternalSyntaxToken> children = Lists.newArrayList();
children.add(firstIdentifier);
if (rests.isPresent()) {
for (Tuple<InternalSyntaxToken, InternalSyntaxToken> rest : rests.get()) {
children.add(rest.first());
children.add(rest.second());
}
}
JavaTree result = null;
InternalSyntaxToken dotToken = null;
for (InternalSyntaxToken child : children) {
if (!child.getGrammarRuleKey().equals(JavaTokenType.IDENTIFIER)) {
dotToken = child;
} else {
InternalSyntaxToken identifierToken = child;
if (result == null) {
result = new IdentifierTreeImpl(identifierToken);
} else {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
result = new MemberSelectExpressionTreeImpl((ExpressionTree) result, dotToken, identifier);
}
}
}
return (TypeTree) result;
}
use of org.sonar.plugins.java.api.tree.TypeTree 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());
}
}
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class RedundantThrowsDeclarationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
ListTree<TypeTree> thrownList = methodTree.throwsClauses();
Set<Type> thrownExceptions = thrownExceptionsFromBody(methodTree);
boolean hasTryWithResourceInBody = hasTryWithResourceInBody(methodTree);
Set<String> reported = new HashSet<>();
for (TypeTree typeTree : thrownList) {
Type exceptionType = typeTree.symbolType();
if (hasTryWithResourceInBody && (exceptionType.is("java.io.IOException") || exceptionType.is("java.lang.Exception"))) {
// method 'close()' from 'java.io.Closeable' interface throws 'java.io.IOException"
continue;
}
String fullyQualifiedName = exceptionType.fullyQualifiedName();
if (!reported.contains(fullyQualifiedName)) {
String superTypeName = isSubclassOfAny(exceptionType, thrownList);
if (superTypeName != null) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a subclass of '%s'.", fullyQualifiedName, superTypeName));
} else if (exceptionType.isSubtypeOf("java.lang.RuntimeException")) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a runtime exception.", fullyQualifiedName));
} else if (declaredMoreThanOnce(fullyQualifiedName, thrownList)) {
reportIssue(typeTree, String.format("Remove the redundant '%s' thrown exception declaration(s).", fullyQualifiedName));
} else if (canNotBeThrown(methodTree, exceptionType, thrownExceptions)) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s', as it cannot be thrown from %s's body.", fullyQualifiedName, methodTreeType(methodTree)));
}
reported.add(fullyQualifiedName);
}
}
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class CastArithmeticOperandCheck method checkMethodTree.
private void checkMethodTree(MethodTreeImpl methodTree) {
TypeTree returnTypeTree = methodTree.returnType();
Type returnType = returnTypeTree != null ? returnTypeTree.symbolType() : null;
if (returnType != null && isVarTypeErrorProne(returnType)) {
methodTree.accept(new ReturnStatementVisitor(returnType));
}
}
Aggregations