Reputation: 2109
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:
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.
Upvotes: 0
Views: 597
Reputation: 38338
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:
Upvotes: 1
Reputation: 103893
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:
Upvotes: 3