in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java [63:115]
public static void checkInstantiationForParameterizedTypedTree(
ParameterizedTypeTree tree,
VisitorState state,
NullAway analysis,
Config config,
Handler handler) {
if (!config.isJSpecifyMode()) {
return;
}
List<? extends Tree> typeArguments = tree.getTypeArguments();
if (typeArguments.isEmpty()) {
return;
}
Map<Integer, Tree> nullableTypeArguments = new HashMap<>();
for (int i = 0; i < typeArguments.size(); i++) {
Tree curTypeArg = typeArguments.get(i);
if (curTypeArg instanceof AnnotatedTypeTree) {
AnnotatedTypeTree annotatedType = (AnnotatedTypeTree) curTypeArg;
for (AnnotationTree annotation : annotatedType.getAnnotations()) {
Type annotationType = ASTHelpers.getType(annotation);
if (annotationType != null
&& Nullness.isNullableAnnotation(annotationType.toString(), config)) {
nullableTypeArguments.put(i, curTypeArg);
break;
}
}
}
}
// base type that is being instantiated
Type baseType = ASTHelpers.getType(tree);
if (baseType == null) {
return;
}
com.sun.tools.javac.util.List<Type> baseTypeArgs = baseType.tsym.type.getTypeArguments();
for (int i = 0; i < baseTypeArgs.size(); i++) {
if (nullableTypeArguments.containsKey(i)) {
Type typeVariable = baseTypeArgs.get(i);
Type upperBound = typeVariable.getUpperBound();
com.sun.tools.javac.util.List<Attribute.TypeCompound> annotationMirrors =
upperBound.getAnnotationMirrors();
boolean hasNullableAnnotation =
Nullness.hasNullableAnnotation(annotationMirrors.stream(), config)
|| handler.onOverrideTypeParameterUpperBound(baseType.tsym.toString(), i);
// if base type argument does not have @Nullable annotation then the instantiation is
// invalid
if (!hasNullableAnnotation) {
reportInvalidInstantiationError(
nullableTypeArguments.get(i), baseType, typeVariable, state, analysis);
}
}
}
}