use of org.sonar.java.resolve.ParametrizedTypeJavaType in project sonar-java by SonarSource.
the class EnumMapCheck method hasEnumKey.
private static boolean hasEnumKey(Type symbolType) {
Type type = symbolType;
if (type instanceof MethodJavaType) {
type = ((MethodJavaType) type).resultType();
}
if (type instanceof ParametrizedTypeJavaType) {
ParametrizedTypeJavaType parametrizedTypeJavaType = (ParametrizedTypeJavaType) type;
List<TypeVariableJavaType> typeParameters = parametrizedTypeJavaType.typeParameters();
if (!typeParameters.isEmpty()) {
return parametrizedTypeJavaType.substitution(typeParameters.get(0)).symbol().isEnum();
}
}
return false;
}
use of org.sonar.java.resolve.ParametrizedTypeJavaType in project sonar-java by SonarSource.
the class EnumSetCheck method checkIssue.
private void checkIssue(Type type, Tree reportTree, TypeTree typeTree) {
if (type.isSubtypeOf("java.util.Set") && !type.isSubtypeOf("java.util.EnumSet") && type instanceof ParametrizedTypeJavaType) {
ParametrizedTypeJavaType parametrizedType = (ParametrizedTypeJavaType) type;
List<TypeVariableJavaType> typeParameters = parametrizedType.typeParameters();
Type variableType = typeTree.symbolType();
if (typeParameters.isEmpty() && variableType instanceof ParametrizedTypeJavaType) {
// for java 7 diamond operator lookup declaration.
parametrizedType = (ParametrizedTypeJavaType) variableType;
typeParameters = parametrizedType.typeParameters();
}
if (!typeParameters.isEmpty()) {
Type typeParameter = parametrizedType.substitution(typeParameters.get(0));
if (typeParameter != null && typeParameter.symbol().isEnum()) {
reportIssue(reportTree, "Convert this Set to an EnumSet.");
}
}
}
}
use of org.sonar.java.resolve.ParametrizedTypeJavaType in project sonar-java by SonarSource.
the class CompareToNotOverloadedCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
if (hasSemantic() && isCompareToMethod(methodTree) && Boolean.FALSE.equals(methodTree.isOverriding())) {
ClassJavaType ownerType = (ClassJavaType) methodTree.symbol().owner().type();
ownerType.superTypes().stream().filter(supertype -> supertype.is("java.lang.Comparable")).findFirst().ifPresent(comparableType -> {
String name = "Object";
if (comparableType.isParameterized()) {
ParametrizedTypeJavaType ptjt = (ParametrizedTypeJavaType) comparableType;
name = ptjt.substitution(ptjt.typeParameters().get(0)).symbol().name();
}
reportIssue(methodTree.parameters().get(0), "Refactor this method so that its argument is of type '" + name + "'.");
});
}
}
use of org.sonar.java.resolve.ParametrizedTypeJavaType in project sonar-java by SonarSource.
the class CollectionInappropriateCallsCheck method getTypeParameter.
@Nullable
private static Type getTypeParameter(Type collectionType) {
if (collectionType instanceof ParametrizedTypeJavaType) {
ParametrizedTypeJavaType parametrizedType = (ParametrizedTypeJavaType) collectionType;
TypeVariableJavaType first = Iterables.getFirst(parametrizedType.typeParameters(), null);
if (first != null) {
return parametrizedType.substitution(first);
}
}
return null;
}
use of org.sonar.java.resolve.ParametrizedTypeJavaType in project sonar-java by SonarSource.
the class ClassWithoutHashCodeInHashStructureCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
Type type = ((NewClassTree) tree).symbolType();
if (type instanceof ParametrizedTypeJavaType && useHashDataStructure(type)) {
ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) type;
Symbol.TypeSymbol symbol = ptt.substitution(ptt.typeParameters().get(0)).symbol();
if (implementsEquals(symbol) && !implementsHashCode(symbol)) {
reportIssue(tree, "Add a \"hashCode()\" method to \"" + symbol.name() + "\" or remove it from this hash.");
}
}
}
Aggregations