Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.Verify.verify;
import static com.uber.nullaway.NullabilityUtil.castToNonNull;
import static com.uber.nullaway.NullabilityUtil.pathWithLeaf;
import static com.uber.nullaway.generics.TypeMetadataBuilder.TYPE_METADATA_BUILDER;
import static java.util.stream.Collectors.joining;

import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -768,7 +769,9 @@ private void reportInvalidOverridingMethodParamTypeError(
}
if (newClassTree.getIdentifier() instanceof ParameterizedTypeTree paramTypedTree
&& !paramTypedTree.getTypeArguments().isEmpty()) {
return typeWithPreservedAnnotations(paramTypedTree);
Type typeFromIdentifier = typeWithPreservedAnnotations(paramTypedTree);
return withEnclosingTypeFromQualifier(
typeFromIdentifier, newClassTree, state, calledFromDataflow);
}
return typeOrNullIfRaw(ASTHelpers.getType(tree));
} else if (tree instanceof NewArrayTree
Expand Down Expand Up @@ -1940,6 +1943,41 @@ private Type typeWithPreservedAnnotations(Tree tree) {
return tree.accept(new PreservedAnnotationTreeVisitor(config), null);
}

/**
* For a {@code NewClassTree} with a qualifying enclosing-instance expression, e.g. {@code
* (someExpr).new Inner<T>() { ... }}, replaces {@code type}'s enclosing type (which, coming from
* {@code baseType.getEnclosingType()} in {@link PreservedAnnotationTreeVisitor}, is just {@code
* Inner}'s statically-declared enclosing type, unrelated to the actual enclosing instance the
* qualifier expression evaluates to) with the properly-annotated, substituted type of the
* qualifier expression itself. If there is no qualifying expression, or we cannot determine its
* type, {@code type} is returned unchanged.
*
* @param type the type computed for the {@code NewClassTree}'s identifier, to be given the
* correct enclosing type
* @param newClassTree the {@code NewClassTree}, possibly qualified by an enclosing-instance
* expression
* @param state the visitor state
* @param calledFromDataflow true if the type is being computed as part of dataflow analysis
* @return {@code type} with its enclosing type corrected, or {@code type} unchanged if there is
* no qualifying expression or its type could not be determined
*/
private @Nullable Type withEnclosingTypeFromQualifier(
Type type, NewClassTree newClassTree, VisitorState state, boolean calledFromDataflow) {
ExpressionTree enclosingExpr = newClassTree.getEnclosingExpression();
if (enclosingExpr == null) {
return type;
}
TreePath pathToEnclosingExpr = new TreePath(state.getPath(), enclosingExpr);
Type enclosingExprType =
getTreeType(enclosingExpr, state.withPath(pathToEnclosingExpr), calledFromDataflow);
if (enclosingExprType == null) {
return type;
}
Type.ClassType classType = (Type.ClassType) type;
return TYPE_METADATA_BUILDER.createClassType(
classType, enclosingExprType, classType.getTypeArguments());
}

/**
* For a conditional expression <em>c</em>, check whether the type parameter nullability for each
* sub-expression of <em>c</em> matches the type parameter nullability of <em>c</em> itself.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,6 @@ static void anonymousClassesFullName() {
.doTest();
}

@Ignore("https://github.com/uber/NullAway/issues/836")
@Test
public void overrideAnonymousNestedClass() {
makeHelper()
Expand All @@ -1639,6 +1638,41 @@ void anonymousNestedClasses() {
// BUG: Diagnostic contains: parameter s is @NonNull, but parameter in superclass method
public String apply(String s) { return s; }
};
Wrapper<String>.Fn<String> fn2 = (this.new Wrapper<String>()).new Fn<String>() {
public String apply(String s) { return s; }
};
}
}
""")
.doTest();
}

@Test
public void overrideAnonymousDeeplyNestedClass() {
makeHelper()
.addSourceLines(
"Test.java",
"""
package com.uber;
import org.jspecify.annotations.Nullable;
class Test {
class Wrapper<P extends @Nullable Object> {
class Middle<Q extends @Nullable Object> {
abstract class Fn<R extends @Nullable Object> {
abstract R apply(P p);
}
}
}
void anonymousDeeplyNestedClasses() {
Wrapper<@Nullable String>.Middle<String>.Fn<String> fn1 =
((this.new Wrapper<@Nullable String>()).new Middle<String>()).new Fn<String>() {
// BUG: Diagnostic contains: parameter p is @NonNull, but parameter in superclass method
public String apply(String p) { return p; }
};
Wrapper<String>.Middle<String>.Fn<String> fn2 =
((this.new Wrapper<String>()).new Middle<String>()).new Fn<String>() {
public String apply(String p) { return p; }
};
}
}
""")
Expand Down
Loading