Search code examples
javalombokdaggergradle-kotlin-dsl

cannot be provided without an @Inject constructor or from an @Provides-annotated method in dagger


I have added lombok dependency in build.gradle.kts as

dependencies {
    brazilGradle.run().forEach { runtimeOnly(it) }
    brazilGradle.build().forEach { implementation(it) }
    brazilGradle.testbuild().forEach { testImplementation(it) }

    brazilGradle.tool("LambdaDaggerLauncher").forEach {
        annotationProcessor(it)
        compileOnly(it)
    }
    brazilGradle.tool("DaggerBuildTool").forEach {
        annotationProcessor(it)
        compileOnly(it)
    }

    // Gradle version of: <ht:import file="lombok/happier-trails.xml"/>
    brazilGradle.tool("Lombok").forEach {
        compileOnly(it)
        annotationProcessor(it)
        testCompileOnly(it)
        testAnnotationProcessor(it)
    }
}

Have added the lombok dependency in config file

build-tools = {
    1.0 = {
        
        JDK11 = 1.0;
        Lombok = 1.18.x;
        LombokUtils = 1.1;

        ...

    };
};

Tried adding LombokApi too

The handler is written as

@Slf4j
@AllArgsConstructor(onConstructor = @__({ @Inject}))
public class Handler implements RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> {

    private final Service service;

    @Override
    public APIGatewayV2HTTPResponse handleRequest(final APIGatewayV2HTTPEvent apiGatewayV2HTTPEvent,
                                                  final Context context) {
        log.info("Received input: {}", apiGatewayV2HTTPEvent);
        return service.processRequest(apiGatewayV2HTTPEvent, context);
    }
}

and dagger injection as

@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {

    @LambdaHandler(withAppConfig = true, appName = "Lambda")
    Handler buildHandler();
}

@Module
public class AppModule {

    @Provides
    @Singleton
    public Service getService(final ApiGatewayEventProcessor ApiGatewayEventProcessor) {
        return new Service(ApiGatewayEventProcessor);
    }
}

But I am getting this error:

error: com.amazon.lambda.Handler cannot be provided without an @Inject constructor or from an @Provides-annotated method. Handler buildHandler();

What am I missing? I tried using @Inject directly like

public class Handler implements RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> {

    private final Service service;

    @Inject
    public Handler(final Service service) {
        this.service = service;
    }

    @Override
    public APIGatewayV2HTTPResponse handleRequest(final APIGatewayV2HTTPEvent apiGatewayV2HTTPEvent,
                                                  final Context context) {
        log.info("Received input: {}", apiGatewayV2HTTPEvent);
        return service.processRequest(apiGatewayV2HTTPEvent, context);
    }

and this is working. What am I doing wrong with the @AllArgsConstructor implementation


Solution

  • The issue was the ordering of brazil gradle dependencies,

    Changing it to below order worked

    dependencies {
        brazilGradle.run().forEach { runtimeOnly(it) }
        brazilGradle.build().forEach { implementation(it) }
        brazilGradle.testbuild().forEach { testImplementation(it) }
    
        // Gradle version of: <ht:import file="lombok/happier-trails.xml"/>
        brazilGradle.tool("Lombok").forEach {
            compileOnly(it)
            annotationProcessor(it)
            testCompileOnly(it)
            testAnnotationProcessor(it)
        }
    
        brazilGradle.tool("DaggerRuntime").forEach {
            annotationProcessor(it)
            compileOnly(it)
        }
    
        brazilGradle.tool("LambdaDaggerLauncher").forEach {
            annotationProcessor(it)
            compileOnly(it)
        }
    }