Jason Rich Darmawan
Jason Rich Darmawan

Reputation: 2109

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:

  1. 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.

Upvotes: 0

Views: 597

Answers (2)

DwB
DwB

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

rzwitserloot
rzwitserloot

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:

  1. Yes, you need the lombok plugin installed; recent versions of intellij have it installed by default if memory serves.

  2. No, lombok cannot be made to work without that.

  3. However, an actual normal annotation processor would work just fine.

  4. 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.

Upvotes: 3

Related Questions