I have wrote a class that implements the comparable interface. I used #NotNull annotation to suppress the waring in the method parameter. But it still shows a warning.The IDE automatically import this package com.sun.istack.internal.NotNull for the #NotNull. Why this is happing? Without using this annotation how to remove this warning ?
I am using Inteij Ultimate with java 8 SE.
Here is my code snippet.
Thank you.
Apparently, you should be using
public int compareTo(#NonNull Node node) {
instead of
public int compareTo(#NotNull Node node) {
The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException.
From here.
For these annotations you need Checker Framework installed. Or you can try what the other answer says.
Change your import, you can use intellij's own com.intellij.annotations.NotNull, javax.annotation.Nonnull or javax.validation.constraints.NotNull.
This is an IntelliJ feature and it was actually possible to configure which nullable/notnull annotation to use, see this guide.
If this doesn't fix it, and the message seems to imply this, try removing the #override, you are adding an annotation to a parameter that didn't have it in the super class.
Related
Assume we have an annotation that is used to mark methods, possibly private, for use by a framework. For example:
class Foo {
#Serializer
private String serialize() {
...
}
}
The method is only ever called by the framework, so it gets marked as Unused declaration by IntelliJ IDEA.
I could suppress this warning for myself by adding the hypothetical #Serializer annotation to Preferences > Editor > Inspections > Java > Declaration redundancy > Unused declaration > Options > Entry points > Annotations... > Mark as entry point if annotated by.
The question is, can I somehow "annotate" the hypothetical #Serializer annotation itself so that IntelliJ knows that it marks an entry point, without having to share a full IntelliJ profile with other people or tell them to modify their profile?
I tried adding #SuppressWarnings("unused") to the annotation, but as I expected, that had no effect. Presumably because it suppresses the warning on the annotation itself, not the methods that are annotated with it.
Having Foo implement an interface that specifies (again, hypothetical) serialize() is not an option, since the class should have an option of keeping the method private.
Annotating with #SuppressWarnings("unused") seems to work now (Intellij 2021.1). I successfully tested it on method and class level.
I have been given a project that I have to study & understand. There's a class that has the following: -
public abstract class AbstractPayment extends GodelCheckout {
public static final String AUTH_USING_GET = "GET";
public static final String AUTH_USING_POST = "POST";
#Retention(RetentionPolicy.SOURCE)
#StringDef({AUTH_USING_GET, AUTH_USING_POST})
public #interface AuthMethod {}
I tried understanding annotation & why they are important from Google's Developer site, but I couldn't understand much apart from the fact that they are used for improving code.
But how are they improving code? Any example?
Why is #Retention(RetentionPolicy.SOURCE) required here?
What does #StringDef() actually do?
what is public #interface AuthMthod{}?
Please explain what Retention.SOURCE, .CLASS & .RUNTIME really are? I tried understanding them from Google's Developer's site, but it didn't help much?
By marking a function argument or result of it, you can define an expected behaviour. For example it marking a function argument with that annotation, no other argument than AUTH_USING_GET or AUTH_USING_POST can be passed.
With source retention, the code won't compile, when there are annotation errors. After successful compilation, they are removed from code. With retention policy RUNTIME the code is inspected during running.
#StringDef allows you to pass only String id's, for example R.string.login. #IntDef allows you to pass only R.id.sample_int to the function
This is the annotation, that desired parameters or function results or class fields must be annotated to obtain desired result.
Look at second question.
For more questions refer to Java documentation.
I am writing a library, so I don't often use the methods in my classes within the same project. As such, my IDE (IntelliJ IDEA) keeps warning me that the methods are unused.
Of course, the obvious solution is to place #SuppressWarnings("unused") before the classes. I don't like this; it doesn't describe the reason I'm writing that annotation and is very verbose. I would love to make an annotation like #LibraryClass which is just an alias of #SuppressWarnings("unused").
In short, I want to be able to change this:
#SuppressWarnings("unused")
public class MyLibraryClass {
public void myLibraryMethod() {
doSomething();
}
}
to this:
#LibraryClass
public class MyLibraryClass {
public void myLibraryMethod() {
doSomething();
}
}
but I have no idea how to do this! I tried all this, and it compiles, but the IDE still warns of unused methods:
#SuppressWarnings("unused")
#Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
#Retention(RetentionPolicy.SOURCE)
public #interface Library {
SuppressWarnings superAnnotation() default #SuppressWarnings("unused");
String[] value() default {"unused"};
}
To do one aspect of what you're asking for - attaching some kind of compile-time logic to annotations - you need to look into annotation processing. An annotation processor hooks into the Java runtime, like an agent, and gets informed about annotations and given an option to process it. To use that, you'd have to put your annotation-processor jar on the IDE's classpath.
Some links:
http://hannesdorfmann.com/annotation-processing/annotationprocessing101
http://programmaticallyspeaking.com/playing-with-java-annotation-processing.html
However, that wouldn't allow you to change the way that Intellij detects unused methods, which seems to be closer to your specific use case. What you could do there is to modify the Intellij 'unused method' inspection so that it incorporates a check for the custom annotation you've defined. YMMV, I've never had to do that at the class level before.
https://gist.github.com/itzg/5e90609cde1473ef9d4d
I have implemented a Java EE page which connects to an inventory webservice. I have implemented an inventory controller as shown below:
public class InventoryController {
#Autowired
private WebServiceTemplate inventoryWsTemplate;
...
}
inventoryWsTemplate is declared as a bean in web-servlet.xml.
The program works but I'm getting a warning that says:
field is used but is never assigned a non-"null" value
What should I do?
Just add a SuppressWarnings annotation:
#Autowired
#SuppressWarnings("null")
private WebServiceTemplate inventoryWsTemplate;
Normally I'd add a comment to justify the annotation (e.g. // Used by guice) but in this case coming hot on the heels of an Autowired annotation, I don't think it's necessary.
EDIT: I can't reproduce the problem on Eclipse to start with, so I'm not sure which annotation value is required. You should check the list of supported annotation values for your compiler.
If you don't like suppressing warnings, you can change private to any other access modifier. Package-private probably being the best choice (besides private).
do they by annotation mean a comment in a code with // or /* */?
No, an annotation is not a comment. An annotation is added to a field, class or method, using the syntax #Annotation. One of the best known annotations is #Override, used to signal a method is overriding one from a super class. For example:
public class MyClass {
#Override
public boolean equals(Object other) {
//...
}
}
See http://download.oracle.com/javase/1,5.0/docs/guide/language/annotations.html for more info.
No, annotations take the form:
#Annotation(property="A")
public class {
#Annotation(property="B")
Object field;
#Annotation(property="C")
public void method() {
}
}
Annotations can be placed on classes, methods or fields. They can provide information at runtime via reflection or compile time via apt (short for Annotation Processing Tool and not the apt package manager).
They are defined as:
#interface Annotation {
String property();
}
See http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html for more
Actually, before Java5 (i.e. 1.3 or 1.4), comments (// or /* */) were the only way to add annotation (i.e. "metadata") to be acted upon.
One classic example is the way the unit-testing framework TestNg propose all its Java5 #Annotations as comments if you are using TestNg with Java 1.4.
But that means, for Testng to launch the proper test suite, it had to access the sources of your program, not just the compiled binary.
Unlike Javadoc tags, Java annotations can be reflective in that they can be embedded in class files generated by the compiler and may be retained by the Java VM to be made retrievable at run-time.
No.
An annotation is a special construct introduced with java 1.5. An annotation adds some meta information to a java class, method or variable. This meta information can be evaluated at compile time (e.g. for generating some extra code with apt) or at runtime (e.g. to match a class to a database table).
Example for a built in annotation:
#Deprecated // this is an annotation
public void myMethod() {
...
}
Annotations are not just for java they also exist in c++, they are somehow similar with those from java.
// MyCode.h
# include <CodeAnalysis/SourceAnnotations.h>
using namespace vc_attributes;
class CMyClass
{
public:
void f ( [Pre ( Valid = Yes )] int *pWidth );
// code ...
};
// MyCode.cpp
#include "MyCode.h"
void CMyClass::f ( [Pre (Valid = Yes)] int pWidth )
{
}
You can check the MSDN for more information:
http://msdn.microsoft.com/en-us/library/ms182036(VS.80).aspx
An annotation is not a comment but it is used for many purposes such as error debugging as well it is the instruction set to the compiler but it hasn't any effect on the runtime code.
#override,#deprecated and others are the examples of annotation. It can be used with methods,constructors,parameters,variables.
Annotations are used to give detailed information to the compiler whereas Comments are for the convenience of the programmer so that he know how the code is structured.
of course not, but I think annotation ≈ comment.
the core of them is describe, but annotation has more confinement, you are not easy to make mistak, also, you can find mistake in compile time.