-
Notifications
You must be signed in to change notification settings - Fork 34
Add Null Support #143
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
Draft
rcosta358
wants to merge
11
commits into
main
Choose a base branch
from
null-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add Null Support #143
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7a8de62
Add Null & Boxed Types Support
rcosta358 c2ae323
Add Tests
rcosta358 d73b81c
Minor Changes
rcosta358 f24dc28
Unused Imports
rcosta358 cb50d1e
Minor Improvements
rcosta358 65e938d
Merge branch 'main' into null-support
rcosta358 aef5189
Mark Uninitialized Fields as Null
rcosta358 e959ce6
Merge branch 'main' into null-support
rcosta358 4c4661c
Fix Merge
rcosta358 8998f23
Remove Boxed Types
rcosta358 d8e500d
Merge branch 'main' into null-support
rcosta358 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
liquidjava-example/src/main/java/testSuite/CorrectNullChecks.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package testSuite; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class CorrectNullChecks { | ||
|
|
||
| Integer x; // implicit null | ||
|
|
||
| void test() { | ||
| mustBeNull(x); | ||
| x = 1; // implicit non-null after assignment | ||
| mustBeNotNull(x); | ||
| } | ||
|
|
||
| void mustBeNull(@Refinement("_ == null") Integer i) {} | ||
| void mustBeNotNull(@Refinement("_ != null") Integer i) {} | ||
|
|
||
| void testNullInteger() { | ||
| Integer i = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| Integer i1 = i; | ||
|
|
||
| i = 123; | ||
|
|
||
| @Refinement("_ != null") | ||
| Integer i2 = i; | ||
| } | ||
|
|
||
| void testNullString() { | ||
| String s = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| String s1 = s; | ||
|
|
||
| s = "hello"; | ||
|
|
||
| @Refinement("_ != null") | ||
| String s2 = s; | ||
| } | ||
|
|
||
| void testNulls() { | ||
| @Refinement("_ == null") | ||
| String s = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| Integer i = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| Boolean b = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| Double d = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| Long l = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| Float f = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| Date dt = null; | ||
|
|
||
| @Refinement("_ == null") | ||
| ArrayList<String> lst = null; | ||
| } | ||
|
|
||
| void testNonNulls() { | ||
| @Refinement("_ != null") | ||
| String s = "hello"; | ||
|
|
||
| @Refinement("_ != null") | ||
| Integer i = 123; | ||
|
|
||
| @Refinement("_ != null") | ||
| Boolean b = true; | ||
|
|
||
| @Refinement("_ != null") | ||
| Double d = 1.0; | ||
|
|
||
| @Refinement("_ != null") | ||
| Long l = 2L; | ||
|
|
||
| @Refinement("_ != null") | ||
| Float f = 1.0f; | ||
|
|
||
| @Refinement("_ != null") | ||
| Date dt = new Date(); | ||
|
|
||
| @Refinement("_ != null") | ||
| ArrayList<String> lst = new ArrayList<>(); | ||
|
|
||
| @Refinement("_ != null") | ||
| CorrectNullChecks r = this; | ||
| } | ||
|
|
||
| void testNullChecksInMethods() { | ||
| @Refinement("_ != null") | ||
| String x = returnNotNullIf(null); | ||
|
|
||
| @Refinement("_ != null") | ||
| String y = returnNotNullTernary(null); | ||
|
|
||
| @Refinement("_ != null") | ||
| String z = returnNotNullParam("not null"); | ||
|
|
||
| @Refinement("_ == null") | ||
| String w = returnNull(); | ||
| } | ||
|
|
||
| @Refinement("_ != null") | ||
| String returnNotNullIf(String s) { | ||
| if (s == null) | ||
| s = "default"; | ||
|
|
||
| return s; | ||
| } | ||
|
|
||
| @Refinement("_ != null") | ||
| String returnNotNullTernary(String s) { | ||
| return s != null ? s : "default"; | ||
| } | ||
|
|
||
| @Refinement("_ != null") | ||
| String returnNotNullParam(@Refinement("_ != null") String s) { | ||
| return s; | ||
| } | ||
|
|
||
| @Refinement("_ == null") | ||
| String returnNull() { | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
liquidjava-example/src/main/java/testSuite/ErrorNullCheckAssignNonNull.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class ErrorNullCheckAssignNonNull { | ||
| public static void main(String[] args) { | ||
| @Refinement("_ == null") | ||
| String s = "not null"; | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
liquidjava-example/src/main/java/testSuite/ErrorNullCheckAssignNull.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class ErrorNullCheckAssignNull { | ||
| public static void main(String[] args) { | ||
| @Refinement("_ != null") | ||
| String s = null; | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
liquidjava-example/src/main/java/testSuite/ErrorNullCheckAssignNullAfter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class ErrorNullCheckAssignNullAfter { | ||
| public static void main(String[] args) { | ||
| @Refinement("_ != null") | ||
| String s = "not null"; | ||
| s = null; // error | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
liquidjava-example/src/main/java/testSuite/ErrorNullCheckAssignNullObject.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class ErrorNullCheckAssignNullObject { | ||
| public static void main(String[] args) { | ||
| @Refinement("_ != null") | ||
| Date date = null; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
liquidjava-example/src/main/java/testSuite/ErrorNullCheckFieldAssignment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| public class ErrorNullCheckFieldAssignment { | ||
|
|
||
| String s; // implicit null | ||
|
|
||
| void test() { | ||
| mustBeNull(s); | ||
| s = "hello"; // implicit non-null after assignment | ||
| mustBeNull(s); // error | ||
| } | ||
|
|
||
| void mustBeNull(@Refinement("_ == null") String s) {} | ||
| } |
15 changes: 15 additions & 0 deletions
15
liquidjava-example/src/main/java/testSuite/ErrorNullCheckFieldNonInitialized.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class ErrorNullCheckFieldNonInitialized { | ||
|
|
||
| Double d; // implicit null | ||
|
|
||
| void test() { | ||
| @Refinement("_ != null") | ||
| Double d2 = d; // should be error | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
liquidjava-example/src/main/java/testSuite/ErrorNullCheckMethod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Refinement Error | ||
| package testSuite; | ||
|
|
||
| import liquidjava.specification.Refinement; | ||
|
|
||
| public class ErrorNullCheckMethod { | ||
|
|
||
| @Refinement("_ != null") | ||
| String returnNotNull(String s) { | ||
| return s; // error: s can be null | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
.../src/main/java/testSuite/classes/atomic_reference_correct/AtomicReferenceRefinements.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package testSuite.classes.atomic_reference_correct; | ||
|
|
||
| import liquidjava.specification.ExternalRefinementsFor; | ||
| import liquidjava.specification.StateRefinement; | ||
| import liquidjava.specification.StateSet; | ||
|
|
||
| @StateSet({"empty", "holding"}) | ||
| @ExternalRefinementsFor("java.util.concurrent.atomic.AtomicReference") | ||
| public interface AtomicReferenceRefinements<V> { | ||
|
|
||
| @StateRefinement(to="initialValue == null ? empty(this) : holding(this)") | ||
| public void AtomicReference(V initialValue); | ||
|
|
||
| @StateRefinement(from="holding(this)") | ||
| public V get(); | ||
|
|
||
| @StateRefinement(to="newValue == null ? empty(this) : holding(this)") | ||
| public void set(V newValue); | ||
| } |
15 changes: 15 additions & 0 deletions
15
...example/src/main/java/testSuite/classes/atomic_reference_correct/AtomicReferenceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package testSuite.classes.atomic_reference_correct; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class AtomicReferenceTest { | ||
|
|
||
| public void testOk() { | ||
| AtomicReference<String> ref = new AtomicReference<>("hello"); | ||
| String s = ref.get(); | ||
|
|
||
| ref.set("world"); | ||
| String s2 = ref.get(); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
liquidjava-example/src/main/java/testSuite/classes/atomic_reference_error/.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| State Refinement Error |
19 changes: 19 additions & 0 deletions
19
...le/src/main/java/testSuite/classes/atomic_reference_error/AtomicReferenceRefinements.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package testSuite.classes.atomic_reference_error; | ||
|
|
||
| import liquidjava.specification.ExternalRefinementsFor; | ||
| import liquidjava.specification.StateRefinement; | ||
| import liquidjava.specification.StateSet; | ||
|
|
||
| @StateSet({"empty", "holding"}) | ||
| @ExternalRefinementsFor("java.util.concurrent.atomic.AtomicReference") | ||
| public interface AtomicReferenceRefinements<V> { | ||
|
|
||
| @StateRefinement(to="initialValue == null ? empty(this) : holding(this)") | ||
| public void AtomicReference(V initialValue); | ||
|
|
||
| @StateRefinement(from="holding(this)") | ||
| public V get(); | ||
|
|
||
| @StateRefinement(to="newValue == null ? empty(this) : holding(this)") | ||
| public void set(V newValue); | ||
| } |
13 changes: 13 additions & 0 deletions
13
...a-example/src/main/java/testSuite/classes/atomic_reference_error/AtomicReferenceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package testSuite.classes.atomic_reference_error; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public class AtomicReferenceTest { | ||
|
|
||
| public void testError() { | ||
| AtomicReference<String> ref = new AtomicReference<>(null); | ||
| String s = ref.get(); // error | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we don't know if this is null right? how do we know? what if there was a constructor, what if another method is called first and sets this fiels? what are the assumptions here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you're right. I'll try to handle those cases.