Unclear inspection warning "NullableProblems" in IntelliJ - java

Why am I getting a warning from the "NullableProblems" inspection in IntelliJ on this:
public class Test implements Comparable<Test> {
#Override
public int compareTo(Test o) {
return 0;
}
}
I'm using IntelliJ 14.1.4 and compiling with Java 1.7
Screenshot:
Adding #NotNull before the argument doesn't help:

From Comparable.compareTo:
#throws NullPointerException if the specified object is null
So IntelliJ knows, that the object should not be null and adds a #NotNull annotation automatically:
IntelliJ IDEA will look carefully at SDK and libraries bytecode and will infer these annotations automatically so that they can later be used to analyze source code to spot places where you overlooked null.
Your overriden method doesn't include this annotation, so it overrides this behavior making the parameter nullable - against the contract of the Comparable interface.
You can solve this by adding #NotNull before the parameter.
You can also disable this inspection by pressing Alt + Enter, selecting the warning in the popup menu and selecting Disable inspection in the sub-menu.
Check out the Web Help and this thread for more information about #NotNull / #NonNull annotations.

This can be globally configured in IntelliJ IDEA easily and for me personally is the recommended way. If you want you can add your own annotations.
i.e. javax.validation.constraints.NotNull
Path to the setting:
Settings > Editor > Inspections > #NotNull/#Nullable problems > Configure annotations
Some screenshots:

Its because that you are overriding a method that does not have a #NotNull annotation.
IntelliJ IDEA warns you if the overriding method does not have a #NotNull annotation.

Related

How to disable "unnecessary test for null" warning in NetBeans 14?

Short Version
How do i disable the "unnecessary test for null" warning in NetBeans 14 IDE?
Long Version
NetBeans as a well-known bug 1 2 3 4 where it will erroneously tell you that a test for null is unnecessary. For example in the following code:
import javax.validation.constraints.NotNull;
private void doSomething(#NotNull Object o) {
if (o == null) return;
//...do more stuff...
}
The IDE thinks
because the o parameter was tagged as #NotNullo
it must be impossible for o to be null
so it must be that the if statement is unnecessary
This is demonstrably false
The #NotNull annotation is only an IDE hint, not a runtime guarantee.
just because an argument to a method is tagged as #NotNullable
does not mean it cannot be null
You can prove this to yourself by passing null to the doSomething method. (we can even write the test code so the IDE generates no hints or warnings at all!):
Object o = getTestValue();
doSomething(o);
private Object getTestValue()
{
Object o = null;
return o;
}
private void doSomething(#NotNull Object o) {
Objects.requireNonNull(value);
//...do more stuff...
}
And watch doSomething method fail - because o is null - even though it is tagged #NotNull.
Now, there may be other implementations of #NotNull, or other compilers that add runtime checks. I'm not talking about those. The NetBeans IDE 14 warning is wrong, so i need to disable it.
Research Effort
I tried clicking the lightbulb, to hopefully configure the warning:
but it only offers to configure null deference warnings - which i definitely want to keep.
I tried pressing Alt+Enter to bring up more options:
but nothing of value appears:
I tried to let it bring me to the area to configure the Null dereferncing hint:
but it definitely has nothing to do with *unnecessary test for null.
I tried searching for a hint or warning named "null":
but it's not there.
I tried searching for a hint or warning named "unnecessary":
but it's not there.
I tried searching for a hint or warning named "test":
but it's not there.
How to turn it off
Which brings me to my question:
given that NetBeans IDE 14 has no way to turn off "unnecessary test for null" warning
how do i turn off the "unnecessary test for null" warning in NetBeans IDE 14?
Bonus Reading
What does unnecessary test for null mean?
Netbeans tells me to remove null comparison, but that breaks my code ("First thing to note is: NetBeans is wrong")
BugZilla: Bug 226923 - Wrong warning 'the expression is never null'
Bugzilla: Bug 262707 - Add configuration of Nullable/NonNull annotations. Use FQNs or allow to exclude FQNs.
You can turn off the "Unnecessary test for null" warning using the Java annotation #SuppressWarnings("null"). That annotation is found in java.lang, and there is no need for an import.
The OpenJDK Javadoc for SuppressWarnings for JDK 17 states:
Indicates that the named compiler warnings should be suppressed in the
annotated element (and in all program elements contained in the
annotated element) ... As a matter of style, programmers should always
use this annotation on the most deeply nested element where it is
effective. If you want to suppress a warning in a particular method,
you should annotate that method rather than its class.
From the linked documentation to section 9.6.4.5 of the Java Language Specification, #SuppressWarnings appears to do exactly what you want, with my emphasis added:
9.6.4.5. #SuppressWarnings
Java compilers are increasingly capable of issuing helpful "lint-like"
warnings. To encourage the use of such warnings, there should be some
way to disable a warning in a part of the program when the programmer
knows that the warning is inappropriate.
Here's sample code, based on that in the OP:
package suppression;
import javax.validation.constraints.NotNull; // Jakarta EE 8
//import jakarta.validation.constraints.NotNull; // Jakarta EE 9
public class Suppression {
public static void main(String[] args) {
Suppression supp = new Suppression();
Object o = supp.getTestValue();
supp.doSomething(o);
supp.doSomething2(o);
}
Object getTestValue() {
Object o = null;
return o;
}
private void doSomething(#NotNull Object o) {
if (o == null) {
System.out.println("Object is null!");
}
}
#SuppressWarnings("null")
private void doSomething2(#NotNull Object o) {
if (o == null) {
System.out.println("Object is null!");
}
}
}
Here's a screenshot of that code in NetBeans 14 which shows:
The unwanted warning "Unnecessary test for null" is shown on line 22 in method doSomething().
The annotation #SuppressWarnings("null") on line 27 prevents the unwanted warning "Unnecessary test for null" being shown on line 30 in the otherwise identical method doSomething2().
The answer is: it cannot be done.
NetBeans provides no way to disable the unnecessary test for null warning.
Workaround
As other people in other answers have noted:
the value can be null
NetBeans is wrong thinking it cannot be null
The correct way to resolve the (incorrect) warning is to obfuscate the check for null.
Rather than calling:
if (customer == null) { ... }
Instead call:
if (Object.isNull(customer)) { ... }
It is the same thing; except this way NetBeans doesn't realize that you're testing the variable for null, and so doesn't warn you.

Missing annotation: #Override is not shown by compiler in Eclipse IDE

This is kind of weird! but when I implement Collection for my modal class and add unimplemented methods using Eclipse IDE, it is not showing #Override in any of the methods which are generated by clicking on "Add Unimplemented Methods".
public class MadeItACollection implements Collection{
}
When I click on "Add Implemented Methods" the following happens:
public class MadeItACollection implements Collection{
public int size() {
// TODO Auto-generated method stub
return 0;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
.
.
.
.
}
I dont see any #Override. I am compiling my codebase in Java 8. Am I missing something obvious?
As peoples' comments suggest, this is a Code Style option within Eclipse.
You can enable it under Preferences -> Java -> Code Style -- ensure that the "Add #Override annotation for new overriding methods" is checked; after, you can also look into adding it for implementations of interface methods via the link directly underneath. (You can also enable automatic adding of the annotation as a Cleanup or Save action in these menus.)
The #Override annotation isn't strictly required when implementing an interface mainly because you aren't overriding any superclass implementation you are actually implementing the interfaces declared methods. So maybe a #Implements annotation is required, but that's a whole different topic of conversation.
However, it is strongly recommended that you still use the #Override annotation on these methods because:
It explicitly conveys to anybody reading the code that it is an overriding method.
It helps avoid shooting yourself in the foot by throwing a compile time error if you misspell the method you want to override because it will tell you if the method can actually override an existing super method.
Most IDEs actually will help you add this automatically:
Intellij Idea:
Navigate to
File => Settings => Editor => Code Style => Java
And scroll to Override Method Signature and you should find Insert #Override annotation, make sure this is checked.
The Jetbrains documentation says the below about this setting option:
Insert #Override Annotation: Select this checkbox to have IntelliJ IDEA insert #Override annotations automatically.
Eclipse:
Navigate to:
Window => Preferences => Java => Code Style
And look for Add #Override annotation for new overriding methods and make sure it has been checked.
You can also add it as a Save Action by navigating to:
Window => Preferences => Java => Editor => Save Actions
And ensuring that Perform the selected actions on save has been selected and that Additional actions has been selected and ensuring that Add missing Annotations has been configured

#NotNull not working as expected

I have a method like:
public String getParamValue(#NotNull String param) {
.......
.......
.......
}
Even after putting #NotNull in-front of the param, whenever i am calling getParamValue(null) it is not throwing NPE. It proceeds as normal, do i need to do something else or am i using it wrongly? Thanks.
I am using Java 7 and javax.validation.constraints.NotNull if it helps in any ways.
This annotation doesn't do anything by itself. It is just a mark for other tools, so they know the constraints. The tools that checks it are source code analyzers and validation tools.
This is not a garantee for notnull, its more like a promise. so you could do a preconditions check:
if (param == null) {
throw new PreconditionExc...
Hi this question has already been asked before.
To summarize you will need to do this:
MVC namespace configuration for annotations:
The JSR-303 spec JAR: validation-api-1.0.0.GA.jar (looks like you already have that)
An implementation of the spec, such as Hibernate Validation, which appears to be the most commonly used example: hibernate-validator-4.1.0.Final.jar
you could also use one of these
https://code.google.com/p/gwt-validation/ and
http://bval.apache.org/
In the bean to be validated, validation annotations, either from the spec JAR or from the implementation JAR (which you have already done)
In the handler you want to validate, annotate the object you want to validate with #Valid, and then include a BindingResult in the method signature to capture errors.
Annotations from javax.validation.constraints not working

How to ignore in Sonar OpenJpa methods pcSet / pcGet?

In my application we are using OpenJpa. By decompiling my classes, I can see some methods I haven´t typed, for instance pcGetid:
public long getId()
{
return pcGetid(this);
}
(...)
private static final long pcGetid(...)
My original java file looks like:
public long getId() {
return id;
}
I assume that is some internal OpenJpa procedure. The problem comes when Sonar analyzes my code. Many Unused private method major violations appears, because some of those methods are not used.
Is ther a way to ignore sonar in these cases? I can not use //NOSONAR because in my code those methods does not exist.
It might be usefull to know in which phase of the build process this methods are added, so I can analyze the content before it happens.
Any idea would be appreciated!
Thanks
If you're using SonarQube 4.0, you can use the issue exclusion mechanism to exclude all issues on this "Unused private method" for a set of java files :http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-patterns

Unnecessary #SuppressWarnings("unused")

I'm getting a compiler warning for the #SuppressWarnings annotation in eclipse for the code:
#Override
public boolean doSomething(#SuppressWarnings("unused") String whatever) throws AnException {
throw new AnException("I'm still in bed and can't do anything until I've had a shower!");
}
It looks like a yellow squiggle under the word "unused" and on mouse hover I get the tooltip Unnecessary #SuppressWarnings("unused").
I think another developer is being prompted to put in these annotations by eclipse and I'm basically being prompted to take them out. How can I configure eclipse to prompt me to put the #SuppressWarnings annotation in instead of it complaining about it?
If anyone would like to comment on best practice here then that would also be most welcome.
In the code in your question, the #SuppressWarnings("unused") annotation is unnecessary because the method is either overriding another method from a superclass or implementing an interface. Even if you don't actually use the whatever parameter it's mandatory to declare it, otherwise the #Override annotation will produce an error (you'd be changing the signature of the overridden method if you removed the parameter.)
In some older versions of Eclipse the code as shown would not cause a warning, but in more recent releases it does. I believe it's a valid warning, and I'd rather remove the #SuppressWarnings("unused") in this case.
Go to
Window → Preferences → Java → Compiler → Errors/Warnings → Annotations.
And select Ignore for Unused '#SuppressWarnings` token.
Alternatively, if you think it's more correct to delete the SuppressWarnings annotation:
Window -> Preferences -> Java -> Compiler -> Errors/Warnings -> Unnecessary code -> Value of parameter is not used
and select Ignore in overriding and implementing methods
In my code there's no inheritance defining the 3 methods with #SuppressWarnings("unused")
This code gives 'Unnecessary #SuppressWarnings("unused")' in Eclipse Juno (latest version), but if I remove the #SuppressWarnings("unused"), I get "Constructor/Method is never used" warnings in IntelliJ IDEA 11.1.3
The methods aren't directly used in the project, only by 3rd party products Jackson, JAXB & GSON, so IntelliJ is right, I would say ...
public class EmailUnsendable extends SkjemaError {
private NestedCommand command; // Can't be Command (interface) because of GSON!
#SuppressWarnings("unused") // Used by Jackson/JAXB/GSON
public EmailUnsendable() {
}
public EmailUnsendable(String referenceNumber, String stackTrace, NestedCommand command) {
super(referenceNumber, stackTrace);
this.command = command;
}
#SuppressWarnings("unused") // Used by Jackson/JAXB/GSON
public NestedCommand getCommand() {
return command;
}
#SuppressWarnings("unused") // Used by Jackson/JAXB/GSON
public void setCommand(NestedCommand command) {
this.command = command;
}
}
I believe this is an error in Eclipse.

Categories

Resources