-
Notifications
You must be signed in to change notification settings - Fork 360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annonymous class in field initializer needs to treat surrounding class as under initialization #904
Comments
Final fields without initializers might get set after the anonymous class is used, resulting in an NPE. Test case added at |
Also applies to inner classes. This related case also doesn't have any errors: class Foo {
private final Object mBar;
class Inner {
void run() {
// No problem, even though it NPEs.
mBar.toString();
}
};
public Foo() {
new Inner().run();
mBar = "";
}
public static void main(String[] args) {
new Foo();
}
} |
Also, to avoid too many false positives if this issue is fixed, can the following be added as a test case as well? class Foo {
private final Object mBar;
private final Runnable mRunnable =
new Runnable() {
@Override
public void run() {
mBar.toString();
}
};
private class Inner {
void run() {
mBar.toString();
}
};
public Foo() {
mBar = "";
}
public void shouldGiveNoErrors() {
mRunnable.run();
new Inner().run();
}
} (It currently has no errors, as intended) |
Resuming back this thread as it's still an open issue, here's another example I'm running into:
This is still an open issue, can we please have an update? |
@NazguL86 Can you please provide a complete example? import org.checkerframework.checker.nullness.qual.Nullable;
class Test {
@Nullable Object f;
Runnable r = new Runnable() {
public void run() {
f = null;
f.toString();
}
};
} For which the Nullness Checker gives: |
Thanks for the quick follow up. The same example you copied it's not throwing any errors for me, so perhaps this has been addressed in a newer version of CheckerFramework that I'm not consuming yet. Would you be able to point the version / change that addressed this issue by any chance? |
I tried with master and the 2.5.4 release, both of which report the error. |
I have tried using 2.5.3 and 2.5.6 versions, will try with latest 2.5.8 next. |
I think I figured out the difference, it seems that it's due to using the "onlyDefs" option making the CheckerFramework behave differently for anonymous inner initializers. build.gradle
This doesn't signal the error on the anonymous class initializer, while if I run CF on the entire package it does. Unfortunately I have to use 'onlyDefs' since the project I'm working is very large and not fully annotated, so I need to target specific classes via 'onlyDefs'. |
I've filed #2227 to keep track of this issue. Let's continue discussion there. |
This pull request corrects the type of both implicit and explicit `this` references by correctly computing the enclosing type of `this`. This pull request fixes two kinds of bugs: 1. The type of `Outer.this` when used in an inner class. (Issues #352, #2208, and #3561.) 2. For the Initialization Checker, the type of `Outer.this` when used in an anonymous class declared in a constructor. (Issues #354, part of #904, and #3408) #409 (and other duplicate issues) is a related bug in the Initialization Checker, but isn't fixed by this PR. Closes #352, closes #354, closes #2208, closes #3266, closes #3408 and closes #3561.
No error about clear NPE when calling function in annonymous class from constructor.
Discovered while discussing #903. Tested with v2.1.1.
The text was updated successfully, but these errors were encountered: