Cannot resolve method generated by annotation processor

597 views Asked by At

I have two projects. One of them is projectA a processor for annotations. Second one, projectB use projectA as dependency. Now this processor is add toJson method to exists class before final compile. Everything works normally. When I compile projectB my projectA is run and modify exists source file and add toJson method. Also intellij auto detect this processor and automatically configure my custom processor to the processor path. But editor give error Cannot resolve method 'toJson' in 'CacheData'. How I can solve this problem?

The below image is my intellij configuration. As you see IDEA automatically detect my processor from my pom.xml but editor does not recognize the generated code intellij configuration

I use IntelliJ IDEA 2021.3 (Ultimate Edition), maven-3.8.1, jdk-1.8 for processor(projectA) and jdk-11 for projectB

1

There are 1 answers

1
sancho On

Finally I solved my problem. It seems that current version of Intellij only supported auto generated class. But if you modify exist file with you annotation processor Intellij editor can not recognize your changes and shows as errors. Thank you very much to @mplushnikov for his lombok-intellij-plugin. I just create my own plugin for IntellIJ and add dependence. After doing this you need to override some entrance classes and add your own annotation and its processor handler and extends it from AbstractClassProcessor

Let me share some example so if anyone who has this problem can use it.

IntellIJ Plugin tutorial for beginner

The below code snippets are from my plugin.xml. First you need add some dependencies to your plugin

<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.java</depends>
<depends>Lombook Plugin</depends> <!-- this is the lombok-intellij-plugin-->

Second step is adding required extensions to your plugin as below

 <extensions defaultExtensionNs="com.intellij">
  <!-- Add your own custom processor. because lombok plugin default search only lombok package and own annotations -->
  <lang.psiAugmentProvider implementation="your_package.AugmentProviderImpl"/>
  <!-- if you packaged your classes other than lombok   -->
  <codeInsight.externalLibraryResolver implementation="your_package.ExternalLibraryImpl"/>
  <implicitUsageProvider implementation="your_package. ImplicitUsageProviderImpl"/>

  <!-- Add your custom annotation processor handler and extend it from   AbstractClassProcessor -->
  <applicationService serviceImplementation="your_package.CustomAnnotationProcessor"/>
 </extensions>

Thats it. Follow these steps, build your plugin and install it. After restart your class's methods which is generated by your own annotation will be recognized by intellij.

Implementing the class is not hard. Just look the lombok-intellij-plugin source code and copy the required file and just modified it.