Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
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
89 changes: 82 additions & 7 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,15 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {
* Updates the {@link EnclosingEnvironmentNullness} with an entry for lambda or anonymous class,
* capturing nullability info for locals just before the declaration of the entity
*
* <p>The entry comes from dataflow analysis of the enclosing code, so this first adds the entry
* that analysis starts from if it is missing; see {@link
* #addMissingEnclosingEnvironmentMapping(TreePath, VisitorState)}.
*
* @param treePath either a lambda or a local / anonymous class, identified by its tree path
* @param state visitor state
*/
private void updateEnvironmentMapping(TreePath treePath, VisitorState state) {
addMissingEnclosingEnvironmentMapping(treePath, state);
AccessPathNullnessAnalysis analysis = getNullnessAnalysis(state);
// two notes:
// 1. we are free to take local variable information from the program point before
Expand All @@ -494,6 +499,75 @@ private void updateEnvironmentMapping(TreePath treePath, VisitorState state) {
analysis.getNullnessInfoBeforeNestedMethodNode(treePath, state, handler));
}

/**
* Ensures that the code enclosing {@code treePath} has the environment mapping its dataflow
* analysis starts from.
*
* <p>That is the mapping of the enclosing lambda, or, for a method or initializer, the mapping of
* the closest local or anonymous class it belongs to, looking through member classes. The
* matchers add mappings only in annotated code, so the mapping is missing when that lambda or
* class is in unmarked code and {@code treePath} is in null-marked code. Adding it runs dataflow
* analysis over the code enclosing that lambda or class, which adds the mapping that code needs
* in turn.
*
* @param treePath a lambda or a local or anonymous class, identified by its tree path
* @param state visitor state
*/
private void addMissingEnclosingEnvironmentMapping(TreePath treePath, VisitorState state) {
TreePath enclosingCode = NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer(treePath);
if (enclosingCode == null) {
return; // diff-coverage: ignore -- a lambda or a local or anonymous class is always in code
}
if (enclosingCode.getLeaf() instanceof LambdaExpressionTree) {
addEnvironmentMappingIfMissing(enclosingCode, state);
} else {
// a method or initializer is a member of a class
addMissingEnvironmentMappingOfClosestLocalOrAnonymousClass(
enclosingCode.getParentPath(), state);
}
}

/**
* Adds the environment mapping of the closest local or anonymous class enclosing a class, looking
* through member classes, if that mapping is missing. Nothing is added for a class that is nested
* only in member classes of a top-level class.
*
* @param classPath the tree path of a class, which counts as its own closest local or anonymous
* class when it is one
* @param state visitor state
*/
private void addMissingEnvironmentMappingOfClosestLocalOrAnonymousClass(
TreePath classPath, VisitorState state) {
// a class that is not local or anonymous is a member of a class or is top-level
while (true) {
NestingKind nestingKind =
ASTHelpers.getSymbol((ClassTree) classPath.getLeaf()).getNestingKind();
if (nestingKind.equals(NestingKind.LOCAL) || nestingKind.equals(NestingKind.ANONYMOUS)) {
addEnvironmentMappingIfMissing(classPath, state);
return;
}
if (!nestingKind.isNested()) {
return;
}
classPath = classPath.getParentPath();
}
}

/**
* Calls {@link #updateEnvironmentMapping(TreePath, VisitorState)} for a lambda or a local or
* anonymous class that has no entry in {@link EnclosingEnvironmentNullness} yet.
*
* @param treePath a lambda or a local or anonymous class, identified by its tree path
* @param state visitor state
*/
private void addEnvironmentMappingIfMissing(TreePath treePath, VisitorState state) {
if (EnclosingEnvironmentNullness.instance(state.context)
.getEnvironmentMapping(treePath.getLeaf())
== null) {
updateEnvironmentMapping(treePath, state);
}
}

private Symbol.MethodSymbol getSymbolOfSuperConstructor(
Symbol.MethodSymbol anonClassConstructorSymbol, VisitorState state) {
// get the statements in the body of the anonymous class constructor
Expand Down Expand Up @@ -675,18 +749,15 @@ private void checkForMethodNullMarkedness(MethodTree tree, VisitorState state) {
}
}
if (markedMethodInUnmarkedContext) {
// If this is a @NullMarked method of a @NullUnmarked local or anonymous class, we need to set
// its environment mapping, since we skipped it during matchClass.
// The dataflow analysis of a @NullMarked method in an unmarked class starts from the
// environment mapping of the closest local or anonymous class enclosing it, which matchClass
// skipped.
TreePath pathToEnclosingClass =
ASTHelpers.findPathFromEnclosingNodeToTopLevel(state.getPath(), ClassTree.class);
if (pathToEnclosingClass == null) {
return;
}
ClassTree enclosingClass = (ClassTree) pathToEnclosingClass.getLeaf();
NestingKind nestingKind = ASTHelpers.getSymbol(enclosingClass).getNestingKind();
if (nestingKind.equals(NestingKind.LOCAL) || nestingKind.equals(NestingKind.ANONYMOUS)) {
updateEnvironmentMapping(pathToEnclosingClass, state);
}
addMissingEnvironmentMappingOfClosestLocalOrAnonymousClass(pathToEnclosingClass, state);
}
}

Expand Down Expand Up @@ -2073,6 +2144,10 @@ public Description matchClass(ClassTree tree, VisitorState state) {
// may run dataflow analysis
if (nestingKind.equals(NestingKind.LOCAL) || nestingKind.equals(NestingKind.ANONYMOUS)) {
updateEnvironmentMapping(state.getPath(), state);
} else {
// the analysis of a member class starts from the environment mapping of the closest local
// or anonymous class enclosing it, which is missing when that class is unmarked
addMissingEnvironmentMappingOfClosestLocalOrAnonymousClass(state.getPath(), state);
}
checkFieldInitialization(tree, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,252 @@ public String foo() {
.doTest();
}

@Test
public void nullMarkedLocalClassInAMethodOfAnUnmarkedLocalClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
class A {
void m() {
%s
}
}
""");
}

@Test
public void nullMarkedLocalClassInAMethodOfAnUnmarkedAnonymousClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
Runnable r = new Runnable() {
public void run() {
%s
}
};
""");
}

@Test
public void nullMarkedLocalClassInAnUnmarkedLambda() {
checkNullMarkedClassSeesCapturedLocals(
"""
Runnable r = () -> {
%s
};
""");
}

@Test
public void nullMarkedLocalClassInAMethodOfAMemberOfAnUnmarkedLocalClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
class A {
class Member {
void m() {
%s
}
}
}
""");
}

@Test
public void nullMarkedLocalClassInAnInitializerOfAnUnmarkedLocalClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
class A {
{
%s
}
}
""");
}

@Test
public void nullMarkedLocalClassInsideTwoUnmarkedLocalClasses() {
checkNullMarkedClassSeesCapturedLocals(
"""
class A {
void m() {
class Inner {
void n() {
%s
}
}
}
}
""");
}

@Test
public void nullMarkedMemberClassOfAnUnmarkedLocalClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
class A {
%s
}
""");
}

@Test
public void nullMarkedMemberClassOfAMemberOfAnUnmarkedLocalClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
class A {
class Member {
%s
}
}
""");
}

@Test
public void nullMarkedMemberClassOfAnUnmarkedAnonymousClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
Runnable r = new Runnable() {
%s
public void run() {}
};
""");
}

@Test
public void nullMarkedLocalClassInALambdaInAFieldInitializerOfAnUnmarkedLocalClass() {
checkNullMarkedClassSeesCapturedLocals(
"""
class A {
Runnable r = () -> {
%s
};
}
""");
}

@Test
public void nullMarkedLocalClassInANullUnmarkedMethodOfAnAnnotatedPackage() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"""
package com.uber;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
public class Test {
@NullUnmarked
public static void test() {
Object x = null;
class A {
void m() {
@NullMarked
class Marked {
Object get() {
// BUG: Diagnostic contains: returning @Nullable expression
return x;
}
}
}
}
}
}
""")
.doTest();
}

@Test
public void nullMarkedMethodOfAMemberClassOfAnUnmarkedLocalClass() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"""
package com.example.thirdparty;
import org.jspecify.annotations.NullMarked;
public class Test {
public static void test() {
Object x = null;
class A {
class Member {
@NullMarked
Object get() {
// BUG: Diagnostic contains: returning @Nullable expression
return x;
}
}
}
}
}
""")
.doTest();
}

@Test
public void nullMarkedMethodOfALocalClassInsideAnUnmarkedLocalClass() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"""
package com.example.thirdparty;
import org.jspecify.annotations.NullMarked;
public class Test {
public static void test() {
Object x = null;
class A {
void m() {
class Local {
@NullMarked
Object get() {
// BUG: Diagnostic contains: returning @Nullable expression
return x;
}
}
}
}
}
}
""")
.doTest();
}

/**
* Compiles a {@code @NullMarked} class declared in unmarked code inside an unmarked method, and
* checks that the class sees the nullness of the locals that method declares: returning the null
* local is reported, and returning the non-null one is not. A crash of NullAway fails the
* compilation instead.
*
* @param enclosingCode the unmarked code around the class, with {@code %s} where the class
* declaration goes, as a local class or as a member of a class
*/
private void checkNullMarkedClassSeesCapturedLocals(String enclosingCode) {
String markedClass =
"""
@NullMarked
class Marked {
Object getNull() {
// BUG: Diagnostic contains: returning @Nullable expression
return x;
}
Object getNonNull() {
return y;
}
}
""";
defaultCompilationHelper
.addSourceLines(
"Test.java",
"""
package com.example.thirdparty;
import org.jspecify.annotations.NullMarked;
public class Test {
public static void test() {
Object x = null;
Object y = new Object();
%s
}
}
"""
.formatted(enclosingCode.formatted(markedClass)))
.doTest();
}

@Test
public void configUnannotatedOverridesNullMarked() {
makeTestHelperWithArgs(
Expand Down
Loading