I am trying to use annotation processors.
How can I get classes (Class objects) annotated with the annotation defined by SupportedAnnotationTypes?
I want to access all constructors of the annotated class, which I couldn't do with a TypeMirror.
Thanks in advance. Please let me know if any more information is required.
Related
I found this quote
the first step to using reflection is to obtain a Class object that
represents the class whose annotations you want to obtain
from Herbert Schildt
Can anyone explain that sentence and also "Class" with respect to annotations. Does an annotation reside inside a "Class" or not?
You can add annotations to lots of symbols like classes, methods, constructors etc.
The point of the quote is, that in order to access those annotations at run-time, your entry point is the class object. From there you can start inspecting it, looking for methods, members etc. and access their annotations. This API is also called reflection, because it essentially reflects the code you have written as an object at run-time (including annotations).
When using Dagger 2, very often I'm applying the following pattern:
Create interface, let's call it LoginService
Create the only one implementation - LoginServiceImpl with the constructor injection:
class LoginServiceImpl implements LoginService {
#Inject LoginServiceImpl() {}
}
Bind the implementation to the interface:
#Binds
abstract LoginService bindStatisticsService(LoginServiceImpl impl);
Always depend on the interface - LoginService in this case.
Is there a possibility to avoid the #Binds annotated method? Is there a simpler (with the less amount of boilerplate code) way to tell Dagger - this class is the only one implementation of the interface, always bind it for example in some annotation on the interface itself?
I've read the documentation and unfortunately haven't found anything like that, but maybe someone knows some trick which can solve my problem.
I don't think that's possible, even hypothetically.
Dagger can't really tell that there's only one implementation, and the only way it could would be to search for every class on the (compilation) classpath to try to find all sorts of possible implementation. That would be slow at best, but Java allows classloading from custom classloaders, so you aren't ever really guaranteed to get a full list of available classes. Even if you were to claim that Dagger should just match against the first appropriate assignable type it sees (because you know that there's only one), Dagger may not have a way to identify where to find that implementation type.
Ultimately it's going to be difficult to improve on a single-line #Binds statement that identifies the fully-qualified class of the binding key (your.package.name.LoginService) and target (your.package.name.LoginServiceImpl).
IDE recognised StoreWatcher Flutter Widget as immutable. Where is that #immutable annotation? It is not in the store_watcher.dart source code.
The annotation is from the meta package
The docs say
Used to annotate a class C. Indicates that C and all subtypes of C
must be immutable.
A class is immutable if all of the instance fields of the class,
whether defined directly or inherited, are final.
Tools, such as the analyzer, can provide feedback if
the annotation is associated with anything other than a class, or a
class that has this annotation or extends, implements or mixes in a
class that has this annotation is not immutable.
Not all annotation cause the expected feedback yet.
Some might need to enable linter rules.
For some it might only planned yet to be supported eventually by the analyzer or the linter.
in the OPAL framework, is it possible to manually create an annotated method?
I currently have the following code:
Method(0, "signaturePolymorphicMethod",
MethodDescriptor(ObjectType("java/lang/Object"), VoidType), Seq())
and I want to add the annotation
#java.lang.invoke.MethodHandle$PolymorphicSignature
to this method. How can I do this?
Annotations are generally stored using the JVM's general "Attributes" mechanism.
In this case the annotation is a non-public inner class of MethodHandle with the "Runtime Retention Policy". Hence, to mark a method as having a "Polymorphic Signature" it is necessary to add the RuntimeVisibibleAnnotations_Attribute to the respective method's attributes table. However, given that the visibility of the annotation is limited to the java.lang.invoke package this is (in this specific case) probably rarely useful. Nevertheless, it is possible to query methods in the respective package
To define a CDI stereotype, i would need to define a runtime annotation.
In scala, i tried this:
#Stereotype
#Target(Array(ElementType.METHOD))
#Retention(RetentionPolicy.RUNTIME)
//... my stereotype annotations
class MyStereotype extends ClassfileAnnotation{}
But found this problem:
Implementation restriction: subclassing Classfile does not make your
annotation visible at runtime. If that is what you want, you must
write the annotation class in Java.
Is it possible, using scala 2.9.1, to define runtime annotations?
This is purely a Scala issue where runtime renentive annotations must be done in Java. So CDI (as well as other stereotypes) should be done in Java.