33import static com .google .common .base .Verify .verify ;
44import static com .uber .nullaway .NullabilityUtil .castToNonNull ;
55
6+ import com .google .common .base .Verify ;
67import com .google .errorprone .VisitorState ;
78import com .google .errorprone .util .ASTHelpers ;
89import com .sun .source .tree .AnnotatedTypeTree ;
@@ -54,8 +55,8 @@ public final class GenericsChecks {
5455 * from type variables for the method to their inferred type arguments (most importantly with
5556 * inferred nullability information).
5657 */
57- private final Map <MethodInvocationTree , Map <TypeVariable , Type >>
58- inferredSubstitutionsForGenericMethodCalls = new LinkedHashMap <>();
58+ private final Map <Tree , Map <TypeVariable , Type >> inferredSubstitutionsForGenericMethodCalls =
59+ new LinkedHashMap <>();
5960
6061 /**
6162 * Checks that for an instantiated generic type, {@code @Nullable} types are only used for type
@@ -943,31 +944,31 @@ private static com.sun.tools.javac.util.List<Type> convertTreesToTypes(
943944 /**
944945 * Substitutes the type arguments from a generic method invocation into the method's type.
945946 *
946- * @param methodInvocationTree the method invocation tree
947+ * @param tree the method invocation tree
947948 * @param methodSymbol symbol for the invoked generic method
948949 * @param state the visitor state
949950 * @param config the NullAway config
950951 * @return the substituted method type for the generic method
951952 */
952953 private Type substituteTypeArgsInGenericMethodType (
953- MethodInvocationTree methodInvocationTree ,
954- Symbol .MethodSymbol methodSymbol ,
955- VisitorState state ,
956- Config config ) {
954+ Tree tree , Symbol .MethodSymbol methodSymbol , VisitorState state , Config config ) {
957955
958- List <? extends Tree > typeArgumentTrees = methodInvocationTree .getTypeArguments ();
956+ List <? extends Tree > typeArgumentTrees =
957+ (tree instanceof MethodInvocationTree )
958+ ? ((MethodInvocationTree ) tree ).getTypeArguments ()
959+ : ((NewClassTree ) tree ).getTypeArguments ();
959960 com .sun .tools .javac .util .List <Type > explicitTypeArgs = convertTreesToTypes (typeArgumentTrees );
960961
961962 Type .ForAll forAllType = (Type .ForAll ) methodSymbol .type ;
962963 Type .MethodType underlyingMethodType = (Type .MethodType ) forAllType .qtype ;
963964
964965 // There are no explicit type arguments, so use the inferred types
965966 if (explicitTypeArgs .isEmpty ()) {
966- if (inferredSubstitutionsForGenericMethodCalls .containsKey (methodInvocationTree )) {
967+ if (inferredSubstitutionsForGenericMethodCalls .containsKey (tree )) {
967968 return substituteInferredTypesForTypeVariables (
968969 state ,
969970 underlyingMethodType ,
970- inferredSubstitutionsForGenericMethodCalls .get (methodInvocationTree ),
971+ inferredSubstitutionsForGenericMethodCalls .get (tree ),
971972 config );
972973 }
973974 }
@@ -1012,7 +1013,7 @@ private Type substituteTypeArgsInGenericMethodType(
10121013 public Nullness getGenericParameterNullnessAtInvocation (
10131014 int paramIndex ,
10141015 Symbol .MethodSymbol invokedMethodSymbol ,
1015- MethodInvocationTree tree ,
1016+ Tree tree ,
10161017 VisitorState state ,
10171018 Config config ) {
10181019 boolean isVarargsParam =
@@ -1035,11 +1036,27 @@ public Nullness getGenericParameterNullnessAtInvocation(
10351036 }
10361037 }
10371038
1038- if (!(tree .getMethodSelect () instanceof MemberSelectTree ) || invokedMethodSymbol .isStatic ()) {
1039- return Nullness .NONNULL ;
1039+ if (tree instanceof MethodInvocationTree ) {
1040+ if (!(((MethodInvocationTree ) tree ).getMethodSelect () instanceof MemberSelectTree )
1041+ || invokedMethodSymbol .isStatic ()) {
1042+ return Nullness .NONNULL ;
1043+ }
10401044 }
1041- Type enclosingType =
1042- getTreeType (((MemberSelectTree ) tree .getMethodSelect ()).getExpression (), config );
1045+
1046+ Type enclosingType = null ;
1047+ if (tree instanceof MethodInvocationTree ) {
1048+ enclosingType =
1049+ getTreeType (
1050+ ((MemberSelectTree ) ((MethodInvocationTree ) tree ).getMethodSelect ()).getExpression (),
1051+ config );
1052+
1053+ } else {
1054+ Verify .verify (tree instanceof NewClassTree );
1055+ // for a constructor invocation, the type from the invocation itself is the "enclosing type"
1056+ // for the purposes of determining type arguments
1057+ enclosingType = getTreeType (tree , config );
1058+ }
1059+
10431060 return getGenericMethodParameterNullness (
10441061 paramIndex , invokedMethodSymbol , enclosingType , state , config );
10451062 }
0 commit comments