samedi 13 février 2016

Java 8: hierarchically scan for package-level annotations

In Java 8, how can I hierarchically scan for package-level annotations (i.e. annotations in package-info.java files)?

e.g., I want to find out for Class<x.y.z.C>, the first annotation of type A that is found existing on the following packages, in order:

  1. x.y.z
  2. x.y
  3. x
  4. (the root package)

When using reflection, I can call D.class.getPackage() to get the Package for x.y.z, and I can see the annotations on that package.

But there is no way to get the Package for x.y from x.y.z (there's no Package#getParent() or equivalent method).

I tried using Package.getPackage("x.y"), but this returns null.

I have not gotten that method to return anything other than null.

I've investigated Reflections, Guava, ByteBuddy, Fast Classpath Scanner, etc., but still can't find a way to hierarchically scan for package-level annotations.





Aucun commentaire:

Enregistrer un commentaire