use of org.sonar.java.resolve.TypeVariableJavaType 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.TypeVariableJavaType 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.TypeVariableJavaType in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method requiredForMemberAccess.
private static boolean requiredForMemberAccess(TypeCastTree typeCastTree) {
ExpressionTree expression = typeCastTree.expression();
if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
Tree parent = typeCastTree.parent();
return expression.is(Tree.Kind.METHOD_REFERENCE) && parent != null && skipParentheses(parent).is(Tree.Kind.MEMBER_SELECT);
}
Symbol symbol = ((MethodInvocationTree) expression).symbol();
if (!symbol.isMethodSymbol()) {
return false;
}
Type returnType = ((Symbol.MethodSymbol) symbol).returnType().type();
if (!(returnType instanceof TypeVariableJavaType) || ((TypeVariableJavaType) returnType).bounds().get(0).is("java.lang.Object")) {
return false;
}
// as the member accessed could have also been part of initial type
return skipParentheses(typeCastTree.parent()).is(Tree.Kind.MEMBER_SELECT);
}
use of org.sonar.java.resolve.TypeVariableJavaType 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;
}
Aggregations