Skip to content

Expose findAnnotated* in public API #4925

Closed
@SergejIsbrecht

Description

@SergejIsbrecht

Feature request

I would like to implement a Feature to resolve all classes, which are marked with a special Annotation. As far as I see it there is straight forward way to get all classes / interfaces, which would provide such capabilities.

    implementation("org.graalvm.nativeimage:svm:22.2.0")

Describe the solution you'd like.
After some searching I found following public methods, which are implemented in the Feature-Impl. I would like to make following methods to be publicly available in the Feature-Interface

FeatureImpl.FeatureAccessImpl

        public List<Class<?>> findAnnotatedClasses(Class<? extends Annotation> annotationClass) {
            return imageClassLoader.findAnnotatedClasses(annotationClass, false);
        }

        public List<Method> findAnnotatedMethods(Class<? extends Annotation> annotationClass) {
            return imageClassLoader.findAnnotatedMethods(annotationClass);
        }

        public List<Field> findAnnotatedFields(Class<? extends Annotation> annotationClass) {
            return imageClassLoader.findAnnotatedFields(annotationClass);
        }

Describe who do you think will benefit the most.
Devs, which implement Features based upon certain Annotations

Describe alternatives you've considered.
Accessing the impl classes by casting / reflection does not work due to not being exported

java.lang.IllegalAccessError: class substratevm.features.FeatX (in unnamed module @0x5151a94d) cannot access class com.oracle.svm.hosted.FeatureImpl$FeatureAccessImpl (in module org.graalvm.nativeimage.builder) because module org.graalvm.nativeimage.builder does not export com.oracle.svm.hosted to unnamed module @0x5151a94d
	at substratevm.features.FeatX.duringAnalysis(FeatX.java:17)

An alternative would be using an external library, which seems to work

    ClassLoader applicationClassLoader = access.getApplicationClassLoader();
    try {
      Reflections reflections = new Reflections(
          new ConfigurationBuilder()
              .forPackage("package", applicationClassLoader));
      Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Annoclass);

Additional context.
/

Express whether you'd like to help contributing this feature
I could make them public in a backwards compatible way, but it is up to the team whether my proposal has some merits. It seems it was a concise decision to NOT expose it via the interface.

If there is another way to get all classes, fields, methods, which are marked with a Annotation please provide an alternative solution and close this issue.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions