use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class EnumMutableFieldCheck method isSetter.
private static boolean isSetter(MethodTree methodTree) {
TypeTree returnType = methodTree.returnType();
BlockTree block = methodTree.block();
boolean returnsVoid = returnType.is(Tree.Kind.PRIMITIVE_TYPE) && "void".equals(((PrimitiveTypeTree) returnType).keyword().text());
boolean hasAtLeastOneStatement = block == null || !block.body().isEmpty();
return methodTree.simpleName().name().startsWith("set") && methodTree.parameters().size() == 1 && returnsVoid && hasAtLeastOneStatement;
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class ErrorClassExtendedCheck method visitClass.
@Override
public void visitClass(ClassTree tree) {
TypeTree superClass = tree.superClass();
if (tree.is(Tree.Kind.CLASS) && superClass != null) {
if (superClass.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree idt = (IdentifierTree) superClass;
if ("Error".equals(idt.name())) {
context.reportIssue(this, superClass, "Extend \"java.lang.Exception\" or one of its subclasses.");
}
} else if (superClass.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mse = (MemberSelectExpressionTree) superClass;
if ("Error".equals(mse.identifier().name()) && isJavaLang(mse.expression())) {
context.reportIssue(this, superClass, "Extend \"java.lang.Exception\" or one of its subclasses.");
}
}
}
super.visitClass(tree);
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class UselessExtendsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
checkExtendsObject(classTree);
ListTree<TypeTree> superInterfaces = classTree.superInterfaces();
if (superInterfaces.isEmpty()) {
return;
}
Set<ClassJavaType> superTypes = ((JavaSymbol.TypeJavaSymbol) classTree.symbol()).superTypes();
List<Type> superInterfacesTypes = getTypes(superInterfaces);
Set<String> reportedNames = new HashSet<>();
for (TypeTree superInterface : superInterfaces) {
String superInterfaceName = extractInterfaceName(superInterface);
if (isDuplicate(superInterfaces, superInterface) && !reportedNames.add(superInterfaceName)) {
// add an issue on a duplicated interface the second time it is encountered
reportIssue(superInterface, "\"" + superInterfaceName + "\" is listed multiple times.");
}
if (!superInterface.symbolType().isUnknown()) {
checkRedundancy(superInterface, superInterfacesTypes, superTypes);
}
}
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class TreeFactory method basicClassExpression.
public ExpressionTree basicClassExpression(PrimitiveTypeTreeImpl basicType, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> dimensions, InternalSyntaxToken dotToken, InternalSyntaxToken classToken) {
// 15.8.2. Class Literals
// int.class
// int[].class
IdentifierTreeImpl classIdentifier = new IdentifierTreeImpl(classToken);
ArrayTypeTreeImpl nestedDimensions = newArrayTypeTree(dimensions);
TypeTree typeTree = applyDim(basicType, nestedDimensions);
return new MemberSelectExpressionTreeImpl((ExpressionTree) typeTree, dotToken, classIdentifier);
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class TreeFactory method newMethodOrConstructor.
private static MethodTreeImpl newMethodOrConstructor(Optional<TypeTree> type, InternalSyntaxToken identifierToken, FormalParametersListTreeImpl parameters, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> annotatedDimensions, Optional<Tuple<InternalSyntaxToken, QualifiedIdentifierListTreeImpl>> throwsClause, JavaTree blockOrSemicolon) {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
ArrayTypeTreeImpl nestedDimensions = newArrayTypeTreeWithAnnotations(annotatedDimensions);
TypeTree actualType;
if (type.isPresent()) {
actualType = applyDim(type.get(), nestedDimensions);
} else {
actualType = null;
}
BlockTreeImpl block = null;
InternalSyntaxToken semicolonToken = null;
if (blockOrSemicolon.is(Tree.Kind.BLOCK)) {
block = (BlockTreeImpl) blockOrSemicolon;
} else {
semicolonToken = (InternalSyntaxToken) blockOrSemicolon;
}
InternalSyntaxToken throwsToken = null;
ListTree<TypeTree> throwsClauses = QualifiedIdentifierListTreeImpl.emptyList();
if (throwsClause.isPresent()) {
throwsToken = throwsClause.get().first();
throwsClauses = throwsClause.get().second();
}
return new MethodTreeImpl(actualType, identifier, parameters, throwsToken, throwsClauses, block, semicolonToken);
}
Aggregations