Create Custom Dagger 2 Scope with Kotlin - java

I'm trying to convert a Java code into Kotlin for custom dagger scope creation.
Here is Java code:
#Documented
#Scope
#Retention(RetentionPolicy.RUNTIME)
public #interface CustomScope {
}
Once converted into kotlin here is the result
#Scope
#Documented
#Retention(RetentionPolicy.RUNTIME) annotation class CustomScope
I have a type mismatch with #Retention(RetentionPolicy.RUNTIME).I have the following error message :Required Type is AnnotationRetention but RetentionPolicy type was found.
Also #interface seems to have been replaced.

The Retention annotation class which you might have used is from the Kotlin's library (from the package kotlin.annotation).
It expects a property of the enum type AnnotationRetention. So, you can do something like this:
#MustBeDocumented
#Scope
#Retention(AnnotationRetention.RUNTIME)
annotation class CustomScope
Btw, if you look at the Annotations.kt file, you will see that that the Retention annotation will take the default property AnnotationRetention.RUNTIME when you don't pass anything to it.
So, just #Retention annotation will do too.

Related

Java annotations logic implementation

I'm creating a java library that can be imported in multiple projects and creates a graphQL schema file with classes and queries annotated with my custom annotations.
I want that when I compile the project, the classes marked by the annotation will be added (with all their fields) into a file that will be the "schema.graphqls" file.
My question is: where I have to insert my logic implementation? Creating a method into the annotation class?
I created two custom annotations, as in the following files:
package annotations;
import java.lang.annotation.*;
#Documented
#Retention(RetentionPolicy.SOURCE)
#Target(ElementType.TYPE) //with TYPE, you can annotate class, interfaces, annotation, types or enum
public #interface GraphQLQuery {
}
GraphQLQuery.java
package annotations;
import java.lang.annotation.*;
#Documented
#Retention(RetentionPolicy.SOURCE)
#Target(ElementType.TYPE) //with TYPE, you can annotate class, interfaces, annotation, types or enum
public #interface GraphQLModel {
}
GraphQLModel.java

Converting package level Java annotation to Kotlin

Is it possible to convert a package level Java annotation to Kotlin?
Java annotation (MyAnnotation.java):
package com.myexample.annotation;
#Retention(RUNTIME) #Target(PACKAGE)
public #interface MyAnnotation {
}
Application of annotation (package-info.java)
#MyAnnotation
package com.myexample
The following does not seem to work (although it does compile) - my annotation processor does not detected any of the classes in the package com.myexample:
MyAnnotation.kt
package com.myexample.annotation
#Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.FILE])
#Retention(AnnotationRetention.SOURCE)
annotation class MyAnnotation
package-info.kt
#file:MyAnnotation
package com.myexample
import com.myexample.annotation.MyAnnotation
No, it's not currently possible. You can simply leave package-info.java as a Java file.

How will I know the annotations of a Java class?

I need to know the annotations of a Java class. I am using Lombok.
Sample is:
#Data
#Builder
public class JavaBean {}
I tried java.lang.annotation.Annotation[] annotation = JavaBean.class.getAnnotations but it doesn't show Data and Builder.
I think you cannot see the annotations in JavaBean.class.getAnnotations because the #Retention is equals to SOURCE.
This kind of annotation is not needed at runtime.
For more details : Annotation SOURCE Retention Policy
Have a good day.
The answer is in source of these annotations:
#Target({TYPE, METHOD, CONSTRUCTOR})
#Retention(SOURCE)
public #interface Builder {
.....
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.SOURCE)
public #interface Data {
.....
Your code to get annotation is correct but it's #Retention(RetentionPolicy.SOURCE) which is playing role here.
Java defined 3 types of retention policies through java.lang.annotation.RetentionPolicy enumeration. It has SOURCE, CLASS and RUNTIME.
1) Annotation with retention policy SOURCE will be retained only with source code, and discarded during compile time.
2) Annotation with retention policy CLASS will be retained till compiling the code, and discarded during runtime.
3) Annotation with retention policy RUNTIME will be available to the JVM through runtime.
#Data and #Builder are marked with #Retention(SOURCE) which means these annotations are not present at runtime with your class hence you are not able to get these annotations..
Lombok annotations are pre-processed before the actual compilation, thus the compiled classes do not contain them as annotation, but rather as the already generated code.

How annotation internally works in java

I know how to create custom annotation. But i am unable to understand how does it work internally. If i take example of spring annontation.
#PropertySource(value = { "classpath:database.properties" }).
if we see internal details of #PropertySource annotation
#Target({ java.lang.annotation.ElementType.TYPE })
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Repeatable(PropertySources.class)
public #interface PropertySource {
public abstract String name();
public abstract String[] value();
public abstract boolean ignoreResourceNotFound();
}
We do not have provided any implementation here for loading property file.
Then How is it loading property file from classpath. Who is working behind the scene ?
Really simple: framework. That is. All 'custom' annotations processed by frameworks using reflection. Only small scope of annotations are processed by compiler, such as #Override, #SuppressWarnings, #Retention and so on

Why does #Documented annotation have runtime retention?

As I know, #Documented annotation is used only by javadoc generator to generate javadocs from sources. So retention type should be SOURCE, but it's RUNTIME. Why?
#Documented
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.ANNOTATION_TYPE)
public #interface Documented {
}
IMO that does not explain why #Documented needs runtime retention
Yes, it does. Lets say I ship a jar file without the sources. A user can build a proper javadoc using only information from classfiles the the reason that classfile have proper annotations is becase they are RetentionPolicy.RUNTIME.

Categories

Resources