use of org.sonar.plugins.java.api.semantic.Type 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.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class DITCheck method visitClass.
@Override
public void visitClass(ClassTree tree) {
if (!isBodyOfEnumConstantTree(tree)) {
Type superClass = tree.symbol().superClass();
int dit = 0;
while (superClass != null) {
String fullyQualifiedName = superClass.fullyQualifiedName();
if (getPatterns().stream().anyMatch(wp -> wp.match(fullyQualifiedName))) {
break;
}
dit++;
superClass = superClass.symbol().superClass();
}
if (dit > max) {
Tree reportTree = tree.simpleName();
if (tree.parent().is(Tree.Kind.NEW_CLASS)) {
reportTree = ((NewClassTree) tree.parent()).newKeyword();
}
context.reportIssue(this, reportTree, "This class has " + dit + " parents which is greater than " + max + " authorized.", new ArrayList<>(), dit - max);
}
}
super.visitClass(tree);
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class EqualsNotOverriddenInSubclassCheck method parentClassImplementsEquals.
private static boolean parentClassImplementsEquals(ClassTree tree) {
TypeTree superClass = tree.superClass();
if (superClass != null) {
Type superClassType = superClass.symbolType();
while (superClassType.symbol().isTypeSymbol() && !superClassType.is("java.lang.Object")) {
Symbol.TypeSymbol superClassSymbol = superClassType.symbol();
if (hasNotFinalEqualsMethod(superClassSymbol)) {
return true;
}
superClassType = superClassSymbol.superClass();
}
}
return false;
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class CFG method handleExceptionalPaths.
private void handleExceptionalPaths(Symbol symbol) {
TryStatement pop = enclosingTry.pop();
TryStatement tryStatement;
Block exceptionPredecessor = currentBlock;
if (enclosedByCatch.peek()) {
tryStatement = enclosingTry.peek();
} else {
tryStatement = pop;
}
enclosingTry.push(pop);
if (pop != outerTry) {
currentBlock = createBlock(currentBlock);
currentBlock.exceptions.add(exitBlocks.peek());
if (!enclosedByCatch.peek()) {
exceptionPredecessor = currentBlock;
}
}
if (symbol.isMethodSymbol()) {
List<Type> thrownTypes = ((Symbol.MethodSymbol) symbol).thrownTypes();
thrownTypes.forEach(thrownType -> {
for (Type caughtType : tryStatement.catches.keySet()) {
if (thrownType.isSubtypeOf(caughtType) || caughtType.isSubtypeOf(thrownType)) {
currentBlock.exceptions.add(tryStatement.catches.get(caughtType));
}
}
});
}
exceptionPredecessor.exceptions.addAll(tryStatement.runtimeCatches);
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class TypeSubstitutionSolver method substitutionFromSuperType.
static TypeSubstitution substitutionFromSuperType(ParametrizedTypeJavaType target, ParametrizedTypeJavaType source) {
TypeSubstitution result = new TypeSubstitution(target.typeSubstitution);
if (target.rawType != source.rawType) {
TypeJavaSymbol targetSymbol = target.symbol;
Type superClass = targetSymbol.superClass();
if (superClass != null && ((JavaType) superClass).isParameterized()) {
TypeSubstitution newSub = substitutionFromSuperType((ParametrizedTypeJavaType) superClass, source);
result = result.combine(newSub);
}
for (Type superInterface : targetSymbol.interfaces()) {
if (((JavaType) superInterface).isParameterized()) {
TypeSubstitution newSub = substitutionFromSuperType((ParametrizedTypeJavaType) superInterface, source);
result = result.combine(newSub);
}
}
} else {
result = target.typeSubstitution.combine(source.typeSubstitution);
}
return result;
}
Aggregations