Annotation Processor Not Running - java

Project 1 (annotation project):
build.gradle
plugins {
id 'java'
}
apply plugin: 'java'
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'com.google.auto.service:auto-service:1.0.1'
implementation 'com.google.auto.service:auto-service:1.0.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
test {
useJUnitPlatform()
}
Annotation
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
public #interface Table {
String value();
String[] ignoreColumns() default {};
}
Processor
#AutoService(TableProcessor.class)
#SupportedAnnotationTypes("com.github.ahuangJM.annotations.Table")
#SupportedSourceVersion(SourceVersion.RELEASE_8)
public class TableProcessor extends AbstractProcessor {
#Override
public synchronized void init(ProcessingEnvironment processingEnv) {
System.out.println("init hit!");
super.init(processingEnv);
}
#Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
final Messager messager = processingEnv.getMessager();
messager.printMessage(Diagnostic.Kind.NOTE, "Processing...");
return true;
}
}
\resources\META-INF\javax.annotation.processing.Processor
org.example.processors.TableProcessor
Project 2 (annotation project consumer):
Annotation Consumer
#Table("user-info")
public class UserInfo {
}
build.gradle
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation files("REDACTED\\code-generator-annotation.jar")
annotationProcessor files("REDACTED\\code-generator-annotation.jar")
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
test {
useJUnitPlatform()
}
This should be a very simple annotation processor just printing debugging statements.
This all seems correct to me, but process() is not running when I build/run project 2. I have tried overriding getSupportedVersion() and getSupportedAnnotationTypes() as well. That didn't do anything. Also tried without #AutoService, that didn't change anything as well.
EDIT: why am i can getting the print() and/or printMessage() statement?

#AutoService(TableProcessor.class)
should be
#AutoService(Processor.class)

#Retention(RetentionPolicy.RUNTIME)
should be
#Retention(RetentionPolicy.SOURCE)

#AutoService(TableProcessor.class)
should be
#AutoService(AbstractProcessor.class) or #AutoService(Processor.class)
#autoservice(X) means you implement X, but i see a class or interface Processor in your question, so you have to be sure what implement what but your #autoservice first is wrong.

Related

null pointer exception at service in spring boot using #Autowired [duplicate]

This question already has answers here:
SpringApplication.run main method
(3 answers)
Closed 1 year ago.
I have a service that has a function that prints a string :
#Service
public class Aservice {
public void write(String test) {
System.out.println(test);
}
}
I'm just trying to call this function in the main function, but it gives me a null pointer exception for the service, what am i doing wrong ?
#SpringBootApplication
public class TestApplication {
#Autowired
private Aservice aservice;
public static void main(String[] args) {
TestApplication test = new TestApplication();
test.start();
}
public void start() {
aservice.write("ddd");
}
}
here's the error:
Exception in thread "main" java.lang.NullPointerException
at com.example.test.TestApplication.start(TestApplication.java:19)
at com.example.test.TestApplication.main(TestApplication.java:15)
and here's my build.gradle:
plugins {
id 'org.springframework.boot' version '2.5.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
The correct way to run a Spring Boot application is to use the SpringApplication run method:
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
This will bootstrap the Spring framework, including component scanning, dependency injection, etc.

Gradle 'implementation' dependency isn't compile with correct code

I have a compilation issue with implementation dependency when compiling with Gradle the following project that contains of 3 modules:
test-impl
test-lib
my-test
My error:
/Users/igor/projects/my-test/src/main/java/MyTest.java:4: error: cannot access TestImpl
lib.foo("");
^
class file for TestImpl not found
It is compiled when I change implemention to api or if I rename any of foo methods in TestLib class.
Gradle 6.0.1, Java 1.8.0_271-b09, OSX
Doesn't it look like a bug? Where to report?
All build.gradle files:
Module test-impl build.gradle:
apply plugin: 'java-library'
Module test-lib build.gradle:
apply plugin: 'java-library'
dependencies {
implementation project(':test-impl')
}
Module my-test build.gradle:
apply plugin: 'java-library'
dependencies {
api project(':test-lib')
}
All source codes:
TestImpl.java in test-impl module:
public class TestImpl {
}
TestLib.java in test-lib module:
public class TestLib {
public void foo(String s) {
}
private void foo(TestImpl impl) {
}
}
MyTest.java in my-test module:
public class MyTest {
public void test() {
TestLib lib = new TestLib();
lib.foo("");
}
}

Dagger 2 - cannot be provided without an #Inject constructor or an #Provides-annotated method

I have a problem with Dagger 2 dependency injection. I'm using it in Android project but in a java-library module and in unit testing.
ApiTestModule.kt
#Module
class ApiTestModule {
#Provides
#Named("testDatasource")
fun provideGithubTestDatasource(): GithubDatasourceImpl {
return GithubDatasourceImpl(Mockito.mock(GithubService::class.java))
}
}
TestComponent.kt
#Singleton
#Component(modules = [ApiTestModule::class])
interface TestComponent {
fun inject(test: GithubDatasourceImplTest)
}
GithubDatasourceImplTest.kt
class GithubDatasourceImplTest {
#set:[Inject Named("testDatasource")]
lateinit var datasource: GithubDatasourceImpl
#Before
fun setUp() {
val component = DaggerTestComponent.builder()
.apiTestModule(ApiTestModule())
.build()
component.inject(this)
}
#Test
fun test_create() {
checkNotNull(datasource)
}
}
module build.gradle
apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
dependencies {
rootProject.ext.apiGithubDependencies.each {
add(it.configuration, it.dependency)
}
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
My error
error:
[Dagger/MissingBinding] hr.thekarlo95.api.github.GithubDatasourceImpl cannot be provided without an #Inject constructor or an #Provides-annotated method.
public abstract void inject(#org.jetbrains.annotations.NotNull()
^
hr.thekarlo95.api.github.GithubDatasourceImpl is injected at
hr.thekarlo95.api.github.GithubDatasourceImplTest.setDatasource(p0)
hr.thekarlo95.api.github.GithubDatasourceImplTest is injected at
hr.thekarlo95.api.github.di.TestComponent.inject(hr.thekarlo95.api.github.GithubDatasourceImplTest)
I cannot get this to work and error message doesn't really help. If I remove fun inject(test: GithubDatasourceImplTest) from TestComponent everything compiles but then I cannot inject dependencies to my unit test.

Error: #dagger.android.ContributesAndroidInjector was used, but dagger.android.processor.AndroidProcessor was not found

I am trying to setup Dagger 2.12 and I'm getting this error:
error: #dagger.android.ContributesAndroidInjector was used, but dagger.android.processor.AndroidProcessor was not found on the processor path
Here's how I've configured Dagger:
My Application class:
public final class App extends android.app.Application implements HasActivityInjector {
#Inject
DispatchingAndroidInjector<Activity> activityInjector;
#Override
public void onCreate() {
super.onCreate();
DaggerAppComponent.builder().build().inject(this);
}
#Override
public AndroidInjector<Activity> activityInjector() {
return activityInjector;
}
}
ActivityBindingModule:
#Module
public abstract class ActivityBindingModule {
#ContributesAndroidInjector(modules = SearchActivityModule.class)
abstract SearchActivity searchActivity();
}
SearchActivityModule:
#Module
public class SearchActivityModule {
#Provides
public SearchActivityDelegate getDelegate(SearchActivity searchActivity) {
return searchActivity;
}
#Provides
public SearchActivityPresenter providePresenter(SearchActivity searchActivity) {
return new SearchActivityPresenter(new OtherDependency(), searchActivity);
}
}
AppModule:
#Module(includes = { AndroidInjectionModule.class, ActivityBindingModule.class })
public abstract class AppModule {
}
Does anyone know what could be causing this error?
Go to your module level build.gradle, under
annotationProcessor 'com.google.dagger:dagger-android-processor:[YOUR VERSION NUMBER]',
add:
kapt 'com.google.dagger:dagger-android-processor:[YOUR VERSION NUMBER]'.
the only solution for me was using old version of dagger (2.16)
kotlin version : 1.2.71
// dagger
implementation 'com.google.dagger:dagger-android:2.16'
implementation 'com.google.dagger:dagger-android-support:2.16'
kapt "com.google.dagger:dagger-compiler:2.16"
kapt "com.google.dagger:dagger-android-processor:2.16"
Probably you would have missed the following dependency.
annotationProcessor 'com.google.dagger:dagger-android-processor:' + yourDaggerVersion
For Java
Add this to your build.gradle
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
For Kotlin
Add this to your build.gradle
apply plugin: 'kotlin-kapt'
kapt "com.google.dagger:dagger-android-processor:$dagger_version"

Cannot resolve symbol generated AutoValue_Foo class inside pure java module in android project

I am trying to use AutoValue in a module that is written in pure Java that acts the domain module for my Android application. My application is composed of 3 layers, presentation, domain and data. Presentation and Data both have android dependencies, but domain does not.
Here is the AutoValue class data implementation :
import com.google.auto.value.AutoValue;
import org.jetbrains.annotations.Nullable;
import java.util.Date;
#AutoValue
public abstract class Trip {
public abstract int id();
public abstract String name();
public abstract int totalDistance();
#Nullable public abstract Date startDate();
#Nullable public abstract Date endDate();
public static Builder builder() {
return new AutoValue_Trip.Builder().totalDistance(0);
}
#AutoValue.Builder
public abstract static class Builder {
public abstract Builder id(int id);
public abstract Builder name(String name);
public abstract Builder totalDistance(int totalDistance);
public abstract Builder startDate(Date startDate);
public abstract Builder endDate(Date endDate);
public abstract Trip build();
}
}
Android Studio cannot find the generated AutoValue_Trip class, so it marks it as an error however, the project builds and runs just fine.
Here is my build.gradle of the domain module
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
classpath "net.ltgt.gradle:gradle-apt-plugin:0.12"
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: "net.ltgt.apt"
apply plugin: 'me.tatarka.retrolambda'
//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.8
//noinspection GroovyUnusedAssignment
targetCompatibility = 1.8
configurations {
provided
}
sourceSets {
main {
compileClasspath += configurations.provided
}
}
dependencies {
def domainDependencies = rootProject.ext.domainDependencies
def domainTestDependencies = rootProject.ext.domainTestDependencies
apt domainDependencies.autoValue
compileOnly domainDependencies.autoValue
provided domainDependencies.javaxAnnotation
compile domainDependencies.javaxInject
compile domainDependencies.rxJava
compile domainDependencies.arrow
testCompile domainTestDependencies.junit
testCompile domainTestDependencies.mockito
testCompile domainTestDependencies.assertj
}
I have tried using a sourceSet as follows to add the generated build files and folders to the source path:
main {
java {
srcDirs += 'src/../build/generated'
}
}
But then I was getting compileJava errors.
Has anyone ran into this issue and how did you correct it? Additional info will be provided if necessary.

Categories