use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class MutableMembersUsageCheck method assignementsOfMutableType.
private static boolean assignementsOfMutableType(List<IdentifierTree> usages) {
for (IdentifierTree usage : usages) {
Tree current = usage;
Tree parent = usage.parent();
do {
if (parent.is(Tree.Kind.ASSIGNMENT)) {
break;
}
current = parent;
parent = current.parent();
} while (parent != null);
if (parent != null) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) parent;
if (assignment.variable().equals(current) && isMutableType(assignment.expression())) {
return true;
}
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class LeftCurlyBraceBaseTreeVisitor method getLastTokenFromSignature.
@CheckForNull
private static SyntaxToken getLastTokenFromSignature(ClassTree classTree) {
List<TypeTree> superInterfaces = classTree.superInterfaces();
if (!superInterfaces.isEmpty()) {
return getIdentifierToken(Iterables.getLast(superInterfaces));
}
TypeTree superClass = classTree.superClass();
if (superClass != null) {
return getIdentifierToken(superClass);
}
TypeParameters typeParameters = classTree.typeParameters();
if (!typeParameters.isEmpty()) {
return typeParameters.closeBracketToken();
}
IdentifierTree simpleName = classTree.simpleName();
if (simpleName != null) {
return simpleName.identifierToken();
}
// enum constants and new class trees are handled separately
return null;
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class MethodParametersOrderCheck method matchingTypesWrongOrder.
private static boolean matchingTypesWrongOrder(ParametersList formalParameterList, List<IdentifierTree> argumentList) {
Iterator<IdentifierTree> argumentsIterator = argumentList.stream().filter(Objects::nonNull).iterator();
int countArgumentsNotOrdered = 0;
while (argumentsIterator.hasNext()) {
IdentifierTree argument = argumentsIterator.next();
int index = formalParameterList.indexOf(argument.name().toLowerCase(Locale.ENGLISH));
Type formalType = formalParameterList.typeOfIndex(index);
Type argType = argument.symbolType();
if (!formalType.is(argType.fullyQualifiedName()) || formalType.isUnknown() || argType.isUnknown()) {
return false;
}
if (argumentList.indexOf(argument) != index) {
countArgumentsNotOrdered++;
}
}
return countArgumentsNotOrdered >= 2;
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class OutputStreamOverrideWriteCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
Type superType = classTree.symbol().superClass();
IdentifierTree className = classTree.simpleName();
if (className == null || classTree.symbol().isAbstract() || superType == null || !(superType.is("java.io.OutputStream") || superType.is("java.io.FilterOutputStream"))) {
return;
}
Optional<MethodTree> writeByteIntInt = findMethod(classTree, WRITE_BYTES_INT_INT);
Optional<MethodTree> writeInt = findMethod(classTree, WRITE_INT);
if (!writeByteIntInt.isPresent()) {
String message = "Provide an override of \"write(byte[],int,int)\" for this class.";
if (writeInt.isPresent()) {
MethodTree writeIntTree = writeInt.get();
if (writeIntTree.block().body().isEmpty()) {
message = "Provide an empty override of \"write(byte[],int,int)\" for this class as well.";
}
}
reportIssue(className, message);
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class ParameterReassignedToCheck method visitAssignmentExpression.
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
ExpressionTree variable = tree.variable();
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifier = (IdentifierTree) variable;
Symbol reference = identifier.symbol();
if (reference.isVariableSymbol() && variables.contains(reference)) {
context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\".");
}
}
}
Aggregations