atteo / classindex

Index classes, do not scan them!
Apache License 2.0
263 stars 43 forks source link

Does not work when used in a library #82

Open lewimuchiri opened 5 months ago

lewimuchiri commented 5 months ago

Assuming you have a library my-library with the following dependencies:

dependencies {
    compileOnly("org.atteo.classindex:classindex:3.4") // also tried implementation(..). Didn't work
    annotationProcessor("org.atteo.classindex:classindex:3.4")
}

And a custom annotation:

@Retention( RetentionPolicy.RUNTIME )
@IndexAnnotated
public @interface MyCustomAnnotation {
}

And a method in the library to get all classes with that annotation:

    public void getClassesWithAnnotation() {
        Iterable<Class<?>> klasses = ClassIndex.getAnnotated(MyCustomAnnotation.class);
        ...
    }

And then the library is imported into another Java project as follows:

    implementation("app.libs.io:my-library:2.0.0.8-SNAPSHOT") 

Then the Java Project has a class with that annotation:

@MyCustomAnnotation
public class TestClass {
}

When the Java project calls the getClassesWithAnnotation() from the library, it gets an empty list, showing that the classes in the Java project were not scanned.

sentinelt commented 5 months ago

I'm not a Gradle user myself, but I guess this is due to Gradle not doing a proper annotation processor discovery anymore. You probably need to add an additional annotationProcessor("app.libs.io:my-library:2.0.0.8-SNAPSHOT") dependency in the projects that are using your library.

lewimuchiri commented 4 months ago

Thanks. Will attempt tomorrow and give feedback