New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Looking up annotations (also) reports annotations of annotations #559
Comments
A stand-alone Eclipse test project with the code shown above. |
The complete output of the main method: test-classgraph.log |
ClassGraph currently returns meta-annotations and inherited annotations when you call I just added the method PS thanks for the test project! But you were right in your interpretation of the current behavior, and it's a quick fix to add a method that returns just the direct annotations. |
Hi @lukehutch, I tried your update. Yes, the I cannot image a situation, where one wants to lookup annotations of a class including all meta-annotations of all annotations of this class. If one wants that functionality, he could easily implement it himself by walking the dependency tree. I think, the default implementation should only report annotations of the class. However, for some annotations it might be desirable to look up the first one defined on the class or one of its super classes (if the requested annotation is marked |
@haumacher Thank you for this perspective, and sorry for the delayed response. I think you're right that a user's default assumption would be that only direct annotations should be returned by I remember why I made it work this way originally: because methods like It has been so long since I wrote this code that I forgot that not only did I add a method However, this brings up a bigger discussion: there is a chance to break the API for a version 5.0.0 release of ClassGraph, and I am willing to change the behavior of the API at that point. I have two questions, if you're able to weigh in on this:
I'm tending towards the third option for an API change; however, it would not be backwards compatible to change Please let me know your thoughts. Thanks! |
This is something that I would never have expected that a list result of a getter provides specialized "filtering" utilities. In my use-case, I evaluated ClassGraph to replace a Java annotation processor that retrieves information at compile-time with a run-time solution that is aware of all classes that could potentially be loaded. Therefore I was biased for what to expect from a Java model API by
I personally prefer an API that makes easy things simple and complex ones possible - not the other way around. I'm happy, if the names on an API reflect this principle - short names for simple functions and composed names for complex functions. Here, the direct (simple) relations of a class/interface is the direct super class/interfaces, its directly implemented/extended interfaces and the reverse of this relations - direct sub classes/interfaces. I would expect that those relation are cached and fast to retrieve using a simple name getXyz(). Complex relations as "all-transitive-generalizations" or "all-transitive-specializations" of a type should have names that remind me that retrieving this relation might involve computational overhead that I do not want to invest, if it is not really necessary, e.g. computeTransitiveXyz() or getTransitiveXyz(). In this concrete example I would prefer an API like this:
But in the ClassGraph API, it seems to be common that getters with list result are not declared The drawback of the For the compatibility issue - yes this is a good idea not to change the semantics of an existing method. In such a case, I prefer a change that helps the customer to easily find the correct fix. If you change In short - no there is no optimal solution - choose carefully :-) |
@haumacher all great thoughts -- thanks for taking the time to think about this, and to provide a detailed writeup! I think I will move to At least for now you know how to get just the direct annotations, however I'll keep this bug open, since I'm starting to assemble a list of things to do for ClassGraph 5.0, which will break the API for the first time in over 4 years. It might be a few months before I get this done, however. |
Situation as follows: I have an annotation
Foo
that can annotate all kind of types (including annotations) and it annotates itself with the string "bar":There is an interface hierarchy with interfaces
A
andB
, whereB
is annotated withFoo
:Now, I'm looking for all specializations of
A
that are annotated withFoo
:This code reports
Which tells me that
B
is annotated twice withFoo
- one time with the valuebar
and one time with the valuebaz
. Even worse, if I'm askinginfo.getAnnotationInfo(Foo.class)
I get@test.classgraph.annotation.Foo("bar")
, which is the meta annotation ofFoo
instead the requested annotation ofB
.Are my expectations wrong?
The text was updated successfully, but these errors were encountered: