Scanning for resources on the user classpath from an annotation processor
Tomas Langer
tomas.langer at oracle.com
Wed Aug 4 12:15:04 UTC 2021
Hello,
I have also tried to do this and failed. I have tried the workaround - it works fine with Java 11, but fails with Java 17:
java.lang.IllegalAccessException: class MyAnnotationProcessor cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @78a0dabb
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Method.invoke(Method.java:560)
at MyAnnotationProcessor.fileManager(MyAnnotationProcessor.java:644)
Thanks,
Tomas
> Hi all,
>
> Apologies in advance if I am sending this to the wrong place.
>
> We have a requirement in the upcoming CDI lite specification to find “beans.xml” files in the user classpath from an annotation processor (Micronaut).
>
> The issue is that as far as I am aware there is no way to do this from an annotation processor since the “JavaFileManager” interface is inaccessible which provides this functionality via the “getClassLoader” method that accepts a location that can be the user classpath:
>
> https://docs.oracle.com/en/java/javase/11/docs/api/java.compiler/javax/tools/JavaFileManager.html#getClassLoader(javax.tools.JavaFileManager.Location)
>
> We have an experimental and not officially supported hack in the Micronaut annotation processor to attempt to work around this which retrieves the “JavaFileManager” via reflection:
>
> https://github.com/micronaut-projects/micronaut-core/blob/a949a27c69ff00fe50fa751adf0501540aee79b2/inject-java/src/main/java/io/micronaut/annotation/processing/visitor/JavaVisitorContext.java#L123-L142<https://github.com/micronaut-projects/micronaut-core/blob/a949a27c69ff00fe50fa751adf0501540aee79b2/inject-java/src/main/java/io/micronaut/annotation/processing/visitor/JavaVisitorContext.java#L123-L142>
>
> However clearly this violates the JDKs direction in terms of closing more of the internals.
>
> I would like to request the addition of an officially supported way to do this via the APT API and am happy to contribute a proposal / JEP + PR if it means it can be taken forward since without this functionality it would mean that users will have to unnecessarily pollute the annotation processor classpath with JARs that aren’t required on that classpath.
>
> Thanks in advance for any feedback.
>
> Graeme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210804/d44ef6b8/attachment.htm>
More information about the compiler-dev
mailing list