use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class CatchIllegalMonitorStateExceptionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
CatchTree catchTree = (CatchTree) tree;
TypeTree parameterTypeTree = catchTree.parameter().type();
if (parameterTypeTree.is(Kind.UNION_TYPE)) {
UnionTypeTree unionTypeTree = (UnionTypeTree) parameterTypeTree;
for (TypeTree exceptionTypeTree : unionTypeTree.typeAlternatives()) {
checkExceptionType(exceptionTypeTree);
}
} else {
checkExceptionType(parameterTypeTree);
}
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class AnonymousClassShouldBeLambdaCheck method visitNewClass.
@Override
public void visitNewClass(NewClassTree tree) {
super.visitNewClass(tree);
ClassTree classBody = tree.classBody();
if (classBody != null) {
TypeTree identifier = tree.identifier();
if (!useThisIdentifier(classBody) && !enumConstants.contains(identifier) && isSAM(classBody)) {
context.reportIssue(this, identifier, "Make this anonymous inner class a lambda" + context.getJavaVersion().java8CompatibilityMessage());
}
}
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class ChildClassShadowFieldCheck method visitNode.
@Override
public void visitNode(Tree tree) {
TypeTree superClass = ((ClassTree) tree).superClass();
if (superClass != null) {
Symbol.TypeSymbol superclassSymbol = superClass.symbolType().symbol();
((ClassTree) tree).members().stream().filter(m -> m.is(Kind.VARIABLE)).map(VariableTree.class::cast).map(VariableTree::simpleName).forEach(fieldSimpleName -> {
if (!IGNORED_FIELDS.contains(fieldSimpleName.name())) {
checkForIssue(superclassSymbol, fieldSimpleName);
}
});
}
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class ClassWithOnlyStaticMethodsInstantiationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
TypeTree identifier = ((NewClassTree) tree).identifier();
Symbol.TypeSymbol newClassTypeSymbol = identifier.symbolType().symbol();
if (!newClassTypeSymbol.isEnum() && hasOnlyStaticMethodsAndFields(newClassTypeSymbol) && !instantiateOwnClass(identifier, newClassTypeSymbol)) {
String message = "Remove this instantiation.";
String name = getNewClassName(identifier);
if (name != null) {
message = "Remove this instantiation of \"{0}\".";
}
reportIssue(identifier, MessageFormat.format(message, name));
}
}
use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitNewClass.
@Override
public void visitNewClass(NewClassTree tree) {
NewClassTreeImpl newClassTreeImpl = (NewClassTreeImpl) tree;
if (newClassTreeImpl.isTypeSet()) {
return;
}
List<JavaType> typeArgumentsTypes = ImmutableList.of();
if (tree.typeArguments() != null) {
resolveAs((List<Tree>) tree.typeArguments(), JavaSymbol.TYP);
typeArgumentsTypes = tree.typeArguments().stream().map(this::getType).collect(Collectors.toList());
}
resolveAs((List<ExpressionTree>) tree.arguments(), JavaSymbol.VAR);
List<JavaType> parameterTypes = getParameterTypes(tree.arguments());
Resolve.Env newClassEnv = semanticModel.getEnv(tree);
ExpressionTree enclosingExpression = tree.enclosingExpression();
TypeTree typeTree = tree.identifier();
IdentifierTree constructorIdentifier = newClassTreeImpl.getConstructorIdentifier();
JavaType identifierType = resolveIdentifierType(newClassEnv, enclosingExpression, typeTree, constructorIdentifier.name());
JavaSymbol.TypeJavaSymbol constructorIdentifierSymbol = (JavaSymbol.TypeJavaSymbol) identifierType.symbol();
constructorIdentifierSymbol.addUsage(constructorIdentifier);
parameterTypes = addImplicitOuterClassParameter(parameterTypes, constructorIdentifierSymbol);
Resolution resolution = resolveConstructorSymbol(constructorIdentifier, identifierType, newClassEnv, parameterTypes, typeArgumentsTypes);
JavaType constructedType = identifierType;
if (resolution.symbol().isMethodSymbol()) {
constructedType = ((MethodJavaType) resolution.type()).resultType;
if (constructedType.isTagged(JavaType.DEFERRED)) {
Tree parent = newClassTreeImpl.parent();
if (parent.is(Tree.Kind.MEMBER_SELECT)) {
constructedType = resolve.parametrizedTypeWithErasure((ParametrizedTypeJavaType) identifierType);
} else {
// will be resolved by type inference
((DeferredType) constructedType).setTree(newClassTreeImpl);
}
} else if (identifierType.symbol().isInterface()) {
// constructor of interface always resolve to 'Object' no-arg constructor
registerType(typeTree, identifierType);
} else {
registerType(typeTree, resolution.type());
}
}
ClassTree classBody = tree.classBody();
if (classBody != null) {
constructedType = getAnonymousClassType(identifierType, constructedType, classBody);
}
registerType(tree, constructedType);
}
Aggregations