Using Dagger2 with Lombok

喜夏-厌秋 提交于 2019-12-09 16:43:42

问题


Has anyone used Lombok 1.16 with Dagger2?

My current code looks like:

@AllArgsConstructor(onConstructor = @__(@Inject))
public class JuiceMaker {
    private final Apple apple;

The error is:

JuiceMaker cannot be provided without an @Inject constructor or from an @Provides-annotated method.

Without the Lombok annotation this actually works, so:

public class JuiceMaker {
    private final Apple apple;
    @Inject
    public JuiceMaker(Apple apple){
        this.apple = apple
    }
}

works


回答1:


This is a copy paste version of my answer here:

This is not a direct answer to the question, which seems to be solved, but acts as reference for future searchers:

If you're using Dagger (or something else) to process your annotations like

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.15</version>
          </path>
        </annotationProcessorPaths>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    ....
  </plugins>
</build>

You have to add here lombok as path like

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.15</version>
          </path>

          <!-- SOLUTION --> 
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
          </path>


        </annotationProcessorPaths>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    ....
  </plugins>
</build>

But you still have to list lombok as provided dependency ;)




回答2:


The issue is that, by the time the dagger annotation processor looks for @Inject constructors, they have not been generated by lombok. One solution might be to delombok the source and then compile it. But I personally haven't done that.



来源:https://stackoverflow.com/questions/50361786/using-dagger2-with-lombok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!