Using an #interface to suppress certain warnings in IntelliJ - java

I'm currently working on a game API that is used as a Core dependency in a bunch of my other projects. The issue I'm having is IntelliJ is giving me all sorts warnings because the API's methods/classes/variables need to be public, have a different parameter value, are only used externally, etc. I would like to simply annotate these fields with #API instead of going through and adding #SupressWarning({(10 different checks)}) every time I add something to the API. I don't want to disable these checks entirely as they can be a helpful reminder, but I can't seem to figure out how to suppress a warning type for an #interface. There is an option for this with the unused warning where you "suppress for methods annotated by ...", but I can't find anything for the other warnings that pop up as a result of the implementation being situationally incorrect because the IDE can't see how it's being used in the other projects.
I tried annotating my API class with the #SuppressWarnings hoping that the annotated members would inherit it
#SuppressWarnings({"WeakerAccess", "unused", "SameParameterValue", ...})
public #interface API{}
but that didn't work, and I've looked for ways to add my other projects as "downstream projects" but I haven't found anything yet. Is this even possible or is it just a limitation of IntelliJ?
Thanks in advance!

Related

Java annotation that sets target as "used" [duplicate]

IntelliJ IDEA has a handy feature to detect unused methods and show them in grey, hinting a potential warning for dead code.
Some methods, however, are not executed directly but via reflection. A good example would be #RequestMapping-annotated methods which are executed by Spring. IntelliJ has decent Spring integration hence it detects this annotation and does not mark such a method as unused.
I have a tiny AJAX framework where I use my own annotation to point which method to execute based on certain HTTP request properties (very similar to what #RequestMapping is doing). Understandably, IntelliJ has no idea what does my annotation stand for and and marks such a method as unused, adding unnecessary noise.
I was thinking of:
annotating my annotation with another annotation, but are there any standard ones that would do the job without any extra effort?
finding a particular setting in IntelliJ to identify custom annotation for marking methods as used, but this would require other team members to do the same, basically a pain.
Can anyone suggest any ideas how to solve this problem?
You can tell IntelliJ to not to warn about used for any method/field annotated with the annotation the "unused" method has.
It should be a quick fix all you have to do is hit <Alt>+<Enter> and select Suppress for methods annotated by ...
You don't need to add anything to you code and you only have to do this once per annotation.
#SuppressWarnings("unused") should work.
#Peter Lawrey s solution did not help in my version of Intellij (14.1.1).
I used the hard way around:Settings-Editor->Inspections->Unused declarion
Now there is an Options point, scroll down to Configure annotations... and you can add your annotation there.
In the "Settings" you can "uncheck" Settings - Inspections - Declaration redundancy - Unused Declaration code inspection.

How to suppress warning 'cannot resolve method' because you generate the method using annotation?

My goal is to learn on how to create annotation. So, I use the project lombok's #Getter annotation for practice. However, one thing bothers me is that the IntelliJ IDEA throws warning of Cannot resolve method 'getId' in 'Product'. Please note, compiling is not a problem.
What I did:
Enable annotation processing in the Settings.
My expected result: The IDE knows that getId method will be injected at compile-time.
My actual result: The IDE throws warning.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
public static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
Product product = new Product();
logger.debug(Integer.toString(product.getId()));
}
}
import lombok.Getter;
public class Product {
#Getter
private int id = 10;
}
PS: I heard it needs Lombok plugin to be installed. Is there a way to do it without plugin? I need it to implement it in my own annotation.
Lombok isn't an annotation processor. Lombok is a unique bit of software, and (as one of the core authors), what lombok does is not trivial in the slightest: You'd have to fork the lombok code base if you want to do lombok-esque things.
Looking at lombok as an example of what annotations can do is in that sense not a good idea: Lombok is its own thing, and merely uses annotations to know where to act, it is not a processor (but, weirdly enough, it registers itself as one in order to become part of the compilation process).
To answer your direct questions:
Yes, you need the lombok plugin installed; recent versions of intellij have it installed by default if memory serves.
No, lombok cannot be made to work without that.
However, an actual normal annotation processor would work just fine.
Basic (non-lombok) annotation processors can only make new files - they cannot change existing files. Only lombok can do that (and it requires a plugin). If you write a basic annotation processor (which therefore can only make new files), what you've done (turn on 'annotation processing') is all that is needed.
Note that basic annotation processors tend to need to go through a full build and have issues with incremental compilation (as in, incrementally compiling tools generally just compile everything, every time, if annotation processors are loaded. Most tools know lombok isn't an annotation processor / works just fine in incremental compilation scenarios and know not to fall back to 'compile all the things all the time'). For medium to large project, 'compile everything' is a gigantic time waster which you want to avoid.
Next steps for you:
Consider if the changes you want to make can be done solely by making new files. If the answer is No, I must change existing files, give up. Or, fork lombok and spend half a year figuring out the inner workings of javac, ecj, and intellij :/
If you CAN do the job by making only new files, know that the experience won't be as smooth and speedy as what lombok does. Forget lombok as an example, it's not a normal annotation processor. Find any tutorial on annotation processing / read up on the APIs / have a look at the source of a project such as AutoValue for an example of how a 'normal' annotation processor works. Know that you can do what you want, and all it takes is what you already did: Enable that 'run processors' checkbox.
As noted in the #rzwitserloot answer,
do not use Lombok as a means to learn annotations.
Instead,
read an annotation tutorial.
Here are some links:
Oracle's Annotation Tutorial
Wikipedia's Annotation Page
Baeldung's Custom Annotation Article

Letting a single Java annotation implicitly set multiple annotations

I have written a Java library that defines and uses a custom annotation to find methods that are then called via reflection.
See this example
#YauaaField("DeviceClass")
public void setDeviceClass(TestRecord record, String value) {
record.deviceClass = value;
}
So IDEs like IntelliJ and probably other code analysis tools will report most of the functions annotated this way as "Unused".
What I would like is an automated way to say that anything that has been annotated with #YauaaField is automatically also annotated with #SuppressWarnings("unused").
I've done quite a bit of googling, read through several online manuals, tutorials and java documentation. Yet I have not yet been able to find how to do that. The annotations do not seem to support 'inheritance' of any kind.
So is what I want even possible?
If not then what other options do I have?
So far I have only found these two ways to suppress these needless warnings:
In IntelliJ I found the manual option to Suppress this warning on all methods annotated with YauaaField. But that is a manual option.
Manually set the #SupressWarnings("unused") on all of those methods/classes.
Is there a better way?
Not entirely automated, but a nice workaround. In IntelliJ you can set up your own Live Template to suggest an autocomplete when typing the annotation:

Providing Dummy-Annotation for older Java compilers

I'm working on a Java library that I would like to be able to use across a couple of different Java compiler versions. Some annotations (specifically #SafeVarargs) only exist on some of these compiler versions and generate errors in others.
Especially for something like #SafeVarargs, which serves mostly as a marker to suppress warnings rather than actually changing the output of the compiler, I would like to be able to use these annotations and simply provide a dummy-implementation if an earlier compiler is missing them.
How would I go about doing this?
I guess you could just create surrogate implementations of those annotations and put them in a Jar that is added to the classpath making sure that the system/compiler provided one take priority when resolved by the corresponding class loader.
For example you can just copy the code of SafeVarargs from here

Telling IntelliJ IDEA which methods not to identify as unused

IntelliJ IDEA has a handy feature to detect unused methods and show them in grey, hinting a potential warning for dead code.
Some methods, however, are not executed directly but via reflection. A good example would be #RequestMapping-annotated methods which are executed by Spring. IntelliJ has decent Spring integration hence it detects this annotation and does not mark such a method as unused.
I have a tiny AJAX framework where I use my own annotation to point which method to execute based on certain HTTP request properties (very similar to what #RequestMapping is doing). Understandably, IntelliJ has no idea what does my annotation stand for and and marks such a method as unused, adding unnecessary noise.
I was thinking of:
annotating my annotation with another annotation, but are there any standard ones that would do the job without any extra effort?
finding a particular setting in IntelliJ to identify custom annotation for marking methods as used, but this would require other team members to do the same, basically a pain.
Can anyone suggest any ideas how to solve this problem?
You can tell IntelliJ to not to warn about used for any method/field annotated with the annotation the "unused" method has.
It should be a quick fix all you have to do is hit <Alt>+<Enter> and select Suppress for methods annotated by ...
You don't need to add anything to you code and you only have to do this once per annotation.
#SuppressWarnings("unused") should work.
#Peter Lawrey s solution did not help in my version of Intellij (14.1.1).
I used the hard way around:Settings-Editor->Inspections->Unused declarion
Now there is an Options point, scroll down to Configure annotations... and you can add your annotation there.
In the "Settings" you can "uncheck" Settings - Inspections - Declaration redundancy - Unused Declaration code inspection.

Categories