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
[Suggestion] NonNull generator for parameters #3127
Conversation
Just a quick comment -- how does this behave when running the generators multiple times? e.g., are existing/redundant null checks removed? |
Codecov Report
@@ Coverage Diff @@
## master #3127 +/- ##
===============================================
- Coverage 58.332% 57.299% -1.034%
Complexity 2670 2670
===============================================
Files 634 635 +1
Lines 34418 33557 -861
Branches 5847 5785 -62
===============================================
- Hits 20077 19228 -849
+ Misses 12284 12276 -8
+ Partials 2057 2053 -4
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
At the moment, it doesn't repeat if matches exactly, for example if the assertion is already present. But there's is room for improving. At the moment if we change the error message it will add a new one instead of replacing it. |
# Conflicts: # javaparser-core-generators/pom.xml
(cherry picked from commit a661c20)
All polished and ready for review! :) |
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.
comment.getRange().isPresent() can be replaced by comment.hasRange()
Thanks for the feedback jlerbsc! I think that could be a improvement for the visitor generators, to use the corresponding "has" method instead of using isPresent() 👍 |
Those ones are generated iirc |
Hi @4everTheOne You have a lot of interesting PR waiting with conflicts. Do you have time to resolve these conflicts especially this one so I can merge it? Thanks. |
Hi @jlerbsc! |
I've spent some time looking again at your previous PRs which are generally very good in intent. Often the problem is that the impact is far too great for me to validate your work. It would certainly be necessary to proceed in small steps, trying to impact public apis as little as possible. Thank you for your investment. |
This reverts commit 7876a3a.
…del/non-null # Conflicts: # javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/resolution/typesolvers/JarTypeSolver.java
@jlerbsc, this PR has been updated :) |
Do you think that this evolution can have an impact on the functioning of applications that use JavaParser? I am asking this question to name the next version of JavaParser. |
I don't see any impact on it. The most sensitive class here is the StaticJavaParser, which is the class that contains the annotation NonNull. But annotation were only added when it makes sense for the parameter to not be null. But let me know, if you see any impact at StaticJavaParser :) |
I was thinking a difference at runtime. |
Thank you for your contribution. |
One important factor to keep the quality of the software is to make sure is handles unintended behavior. @MysterAitch suggested at #2707 to add
@Nullable
and@NotNull
to indicate if a given variable can take null values.In this PR is suggested a way to improve the quality by providing a generator that creates the assertions required for the annotated variables. Lets take a look at an example.
To make sure the example above can only take non-null values we simply add a
@NotNull
annotation to the parameter:By doing this, when the Generator is run, the preconditions will be added automatically making it looks like this:
How the generator works
At the moment the generator only support method and constructor parameters but can be expanded for other types like variable declarations or even field declaration.
The generator get all methods and constructors in the class, and check if the parameter is annotated with
@NotNull
if it's, then it will generate a call at the top of the method toPreconditions.checkNotNull
to make sure no null value is present. The logic for the constructor follows the same guidelines as a method, but with a tiny difference. If the the first statement in the constructor is aExplicitConstructorInvocationStmt
then it adds the assertion after to avoid compilation issues.I would like to hear your opinions! :)