Skip to content

Commit

Permalink
SONARJAVA-4576 Update API with methods from JUtils (#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource authored Nov 20, 2023
1 parent c3092b3 commit 6893424
Show file tree
Hide file tree
Showing 33 changed files with 305 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private static boolean isSubtypeOf(Type type, Type superType) {

private static boolean autoboxing(Type argumentType, Type collectionParameterType) {
return argumentType.isPrimitive()
&& isSubtypeOf(JUtils.primitiveWrapperType(argumentType), collectionParameterType);
&& isSubtypeOf(argumentType.primitiveWrapperType(), collectionParameterType);
}

private static class TypeChecker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonar.java.checks;

import org.sonar.java.checks.helpers.MethodTreeUtils;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Type;
Expand Down Expand Up @@ -65,15 +64,15 @@ private static boolean isEquals(MethodTree tree) {
}

protected static boolean isNullComparison(Type leftOpType, Type rightOpType) {
return JUtils.isNullType(leftOpType) || JUtils.isNullType(rightOpType);
return leftOpType.isNullType() || rightOpType.isNullType();
}

protected static boolean isStringType(Type leftOpType, Type rightOpType) {
return leftOpType.is(JAVA_LANG_STRING) && rightOpType.is(JAVA_LANG_STRING);
}

protected static boolean isBoxedType(Type leftOpType, Type rightOpType) {
return JUtils.isPrimitiveWrapper(leftOpType) && JUtils.isPrimitiveWrapper(rightOpType);
return leftOpType.isPrimitiveWrapper() && rightOpType.isPrimitiveWrapper();
}

protected void reportIssue(SyntaxToken opToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static String message(Type varargParameter, Type varargArgument) {
message = "Remove this argument or pass an empty '%s' array to the vararg method.";
} else if (isPrimitiveArray(varargArgument)) {
Type argumentType = ((Type.ArrayType) varargArgument).elementType();
return String.format("Use an array of '%s' instead of an array of '%s'.", JUtils.primitiveWrapperType(argumentType).name(), argumentType.name());
return String.format("Use an array of '%s' instead of an array of '%s'.", argumentType.primitiveWrapperType().name(), argumentType.name());
}
return String.format(message, parameterType.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.annotation.CheckForNull;
import org.sonar.check.Rule;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.LiteralUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Expand Down Expand Up @@ -92,7 +91,7 @@ public void visitNode(Tree tree) {

private static boolean isIntOrLong(ExpressionTree expression) {
Type type = expression.symbolType();
return isIntegral(type) || (JUtils.isPrimitiveWrapper(type) && isIntegral(JUtils.primitiveType(type)));
return isIntegral(type) || (type.isPrimitiveWrapper() && isIntegral(type.primitiveType()));
}

private static boolean isTruncation(MethodInvocationTree methodTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.LinkedList;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Symbol;
Expand Down Expand Up @@ -116,7 +115,7 @@ private static boolean isParameterizedWithTypeVarFromParent(ClassTree tree) {
return parameterizedSuperTypes.stream()
.flatMap(parameterizedTypeTree -> parameterizedTypeTree.typeArguments().stream())
.map(TypeTree::symbolType)
.anyMatch(JUtils::isTypeVar);
.anyMatch(Type::isTypeVar);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void visitNode(Tree tree) {
}

private static boolean isGenericOrWildCard(TypeTree tree) {
return JUtils.isTypeVar(tree.symbolType()) || tree instanceof WildcardTree;
return tree.symbolType().isTypeVar() || tree instanceof WildcardTree;
}

private static Optional<TypeTree> getMapKeyTree(ParameterizedTypeTree typeTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonar.java.checks;

import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Expand Down Expand Up @@ -88,7 +87,7 @@ private static boolean isValueOfInvocation(ExpressionTree abstractTypedTree) {
MethodMatchers valueOfMatcher = MethodMatchers.create()
.ofTypes(type.fullyQualifiedName())
.names("valueOf")
.addParametersMatcher(JUtils.primitiveType(type).fullyQualifiedName())
.addParametersMatcher(type.primitiveType().fullyQualifiedName())
.build();
return valueOfMatcher.matches((MethodInvocationTree) abstractTypedTree);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonar.java.checks;

import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ConditionalExpressionTree;
Expand Down Expand Up @@ -49,7 +48,7 @@ public void visitNode(Tree tree) {
}

private static boolean dissimilarPrimitiveTypeWrappers(Type trueExprType, Type falseExprType) {
return JUtils.isPrimitiveWrapper(trueExprType) && JUtils.isPrimitiveWrapper(falseExprType) && !trueExprType.equals(falseExprType);
return trueExprType.isPrimitiveWrapper() && falseExprType.isPrimitiveWrapper() && !trueExprType.equals(falseExprType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Type;
Expand Down Expand Up @@ -97,7 +96,7 @@ private void checkTypeTree(@Nullable TypeTree typeTree) {

private void checkIdentifier(IdentifierTree identifier) {
Type type = identifier.symbolType();
if (JUtils.isRawType(type) && !type.equals(JUtils.declaringType(type))) {
if (type.isRawType() && !type.equals(type.declaringType())) {
context.reportIssue(this, identifier, "Provide the parametrized type for this generic.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static boolean canNotBeThrown(MethodTree methodTree, Type exceptionType,
|| !exceptionType.isSubtypeOf("java.lang.Exception")
|| exceptionType.isSubtypeOf("java.lang.RuntimeException")
|| thrownExceptions == null
|| thrownExceptions.stream().anyMatch(JUtils::isTypeVar)) {
|| thrownExceptions.stream().anyMatch(Type::isTypeVar)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private static Optional<String> getTypeName(TypeTree type) {
}

private static boolean isGeneric(IdentifierTree identifierTree) {
return JUtils.isTypeVar(identifierTree.symbolType());
return identifierTree.symbolType().isTypeVar();
}

private static boolean isSingleParamExpression(ExpressionTree expression, Symbol symbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonarsource.analyzer.commons.collections.ListUtils;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ExpressionTree;
Expand Down Expand Up @@ -53,7 +52,7 @@ protected void onMethodInvocationFound(MethodInvocationTree tree) {
ExpressionTree firstArgument = ListUtils.getOnlyElement(tree.arguments());
Type argumentType = firstArgument.symbolType().erasure();
if (argumentType.isPrimitive()) {
argumentType = JUtils.primitiveWrapperType(argumentType);
argumentType = argumentType.primitiveWrapperType();
}
Type ownerType = getMethodOwnerType(tree).erasure();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.sonar.java.checks;

import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.ClassTree;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void visitNode(Tree tree) {

private void checkImports(List<ImportTree> imports) {
imports.stream()
.map(JUtils::importTreeSymbol)
.map(ImportTree::symbol)
.filter(Objects::nonNull)
.forEach(this::checkSymbol);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Type;
Expand Down Expand Up @@ -200,7 +199,7 @@ private static Optional<String> handleBiConsumerInterface(Type parametrizedType,

ParameterTypeNameAndTreeType firstArgument = new ParameterTypeNameAndTreeType(parametrizedType, 0);
ParameterTypeNameAndTreeType secondArgument = new ParameterTypeNameAndTreeType(parametrizedType, 1);
if (secondArgument.paramTypeName != null && !JUtils.isPrimitiveWrapper(firstArgument.paramType)) {
if (secondArgument.paramTypeName != null && !firstArgument.paramType.isPrimitiveWrapper()) {
return Optional.of(String.format("Obj%sConsumer<%s>", secondArgument.paramTypeName, firstArgument.paramType));
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.sonar.check.Rule;
import org.sonar.java.checks.helpers.QuickFixHelper;
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonar.java.model.JUtils;
import org.sonar.java.reporting.AnalyzerMessage;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.java.reporting.JavaTextEdit;
Expand Down Expand Up @@ -61,7 +60,7 @@ private void checkCast(TypeCastTree castTree, MethodInvocationTree mit) {
Type elementType = ((Type.ArrayType) type).elementType();
ExpressionTree methodSelect = mit.methodSelect();
// Do not report an issue for type variables and call to toArray from the Collection itself
if (!JUtils.isTypeVar(elementType) && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
if (!elementType.isTypeVar() && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
String typeName = String.format("new %s[0]", elementType.name());
QuickFixHelper.newIssue(context)
.forRule(this)
Expand All @@ -76,7 +75,7 @@ private void checkCast(TypeCastTree castTree, MethodInvocationTree mit) {
private static JavaQuickFix getQuickFix(TypeCastTree castTree, MethodInvocationTree mit, MemberSelectExpressionTree methodSelect, String typeName) {
List<JavaTextEdit> textEdits = new ArrayList<>();
textEdits.add(JavaTextEdit.insertAfterTree(mit.arguments().firstToken(), typeName));
if (!JUtils.isRawType(methodSelect.expression().symbolType())) {
if (!methodSelect.expression().symbolType().isRawType()) {
textEdits.add(JavaTextEdit.removeTextSpan(AnalyzerMessage.textSpanBetween(castTree, true, mit, false)));
}
return JavaQuickFix.newQuickFix("Pass \"%s\" as argument", typeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.java.checks.serialization.SerializableContract;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Type;
Expand Down Expand Up @@ -80,7 +79,7 @@ public void visitNode(Tree tree) {
}

private static boolean isConstantType(Type symbolType) {
return symbolType.isPrimitive() || symbolType.is("java.lang.String") || JUtils.isPrimitiveWrapper(symbolType);
return symbolType.isPrimitive() || symbolType.is("java.lang.String") || symbolType.isPrimitiveWrapper();
}

private void checkName(VariableTree variableTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Type;
Expand Down Expand Up @@ -104,7 +103,7 @@ private boolean isLocalConstant(VariableTree tree) {
}

private static boolean isConstantType(Type symbolType) {
return symbolType.isPrimitive() || symbolType.is("java.lang.String") || JUtils.isPrimitiveWrapper(symbolType);
return symbolType.isPrimitive() || symbolType.is("java.lang.String") || symbolType.isPrimitiveWrapper();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.ModifiersUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
Expand Down Expand Up @@ -195,7 +194,7 @@ private static boolean implementsSerializable(@Nullable Type type) {
if (type.isArray()) {
return implementsSerializable(((Type.ArrayType) type).elementType());
}
if (type.isClass() || JUtils.isTypeVar(type)) {
if (type.isClass() || type.isTypeVar()) {
return type.isSubtypeOf("java.io.Serializable");
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.sonar.java.checks.helpers.MethodTreeUtils;
import org.sonar.java.checks.helpers.UnitTestUtils;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.Symbols;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
Expand Down Expand Up @@ -369,7 +368,7 @@ static Type expectedArgumentType(MethodInvocationTree mit, int argumentIndex) {

static Type wrapperType(Type type) {
if (type.isPrimitive()) {
Type wrapperType = JUtils.primitiveWrapperType(type);
Type wrapperType = type.primitiveWrapperType();
return wrapperType != null ? wrapperType : type;
}
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;
import java.util.Locale;
import org.sonar.check.Rule;
import org.sonar.java.model.JUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.ClassTree;
Expand All @@ -46,7 +45,7 @@ public List<Tree.Kind> nodesToVisit() {
public void visitNode(Tree tree) {
TypeParameters typeParameters = tree.is(Tree.Kind.METHOD) ? ((MethodTree) tree).typeParameters() : ((ClassTree) tree).typeParameters();
for (TypeParameterTree typeParameter : typeParameters) {
Symbol symbol = JUtils.typeParameterTreeSymbol(typeParameter);
Symbol symbol = typeParameter.symbol();
if (!symbol.isUnknown() && symbol.usages().isEmpty()) {
String message = String.format(ISSUE_MESSAGE, symbol.name(), tree.kind().name().toLowerCase(Locale.ROOT));
reportIssue(typeParameter.identifier(), message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.List;
import org.sonar.api.batch.sensor.symbol.NewSymbol;
import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
import org.sonar.java.model.JUtils;
import org.sonar.java.model.declaration.VariableTreeImpl;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
Expand Down Expand Up @@ -67,7 +66,7 @@ public void visitClass(ClassTree tree) {
createSymbol(simpleName, tree.symbol().usages());
}
for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
createSymbol(typeParameterTree.identifier(), JUtils.typeParameterTreeSymbol(typeParameterTree).usages());
createSymbol(typeParameterTree.identifier(), typeParameterTree.symbol().usages());
}
super.visitClass(tree);
}
Expand All @@ -89,7 +88,7 @@ public void visitMethod(MethodTree tree) {
List<IdentifierTree> usages = tree.symbol().usages();
createSymbol(tree.simpleName(), usages);
for (TypeParameterTree typeParameterTree : tree.typeParameters()) {
createSymbol(typeParameterTree.identifier(), JUtils.typeParameterTreeSymbol(typeParameterTree).usages());
createSymbol(typeParameterTree.identifier(), typeParameterTree.symbol().usages());
}
super.visitMethod(tree);
}
Expand All @@ -110,7 +109,7 @@ public void visitImport(ImportTree tree) {
}
// Exclude on demands imports
if (!"*".equals(identifierTree.name())) {
Symbol symbol = JUtils.importTreeSymbol(tree);
Symbol symbol = tree.symbol();
if (symbol != null) {
createSymbol(identifierTree, symbol.usages());
}
Expand Down
Loading

0 comments on commit 6893424

Please sign in to comment.