Behavior when Java annotation names are worn

I started writing Java and started using annotations.

Suddenly I wondered what would happen if the name of the annotation I made overlapped with another library? about it.

In the case of a class, you can use the same name for the class by explicitly indicating it with a package name, but I could not imagine that the annotation would have a package name, so I made a simple annotation. ..

As long as you separate the package names, you can create a class with the same name, so you should be able to create it, so I created A annotations for each of the a package and b package.

package a;

public @interface A {
}
package b;

public @interface A {
}

It's very simple because there is no processing.

Let's use it! As a result of coding by leaving it to intelliJ, it became as below

import a.A;

public class Main {
    @A //This is an import-declared A annotation
    // @a.A //It's the same as ↑! I comment out because I get angry, but of course this way of writing is OK
    @b.A
    public static void main(String[] args){
    }
}

The package name is entered as it is.

Summary

Perhaps it is a famous story for Java engineers, but even if you wear the annotation name, you can use it properly like a class. And it seems to be difficult until you get used to it because it feels a little uncomfortable if the annotation has a package name. I was relieved for the time being that there was no problem even if the annotation names were covered between the libraries.

Recommended Posts

Behavior when Java annotation names are worn
[Java] Annotation
[Java] Annotation
Behavior when getters generated by lombok are duplicated
When there are environment variables in Java tests
Behavior when calling Java variadic methods from Scala / Kotlin / Java
[Java] Sorting tips when strings and numbers are mixed
When you want to dynamically replace Annotation in Java8
History of Java annotation
About the behavior when doing a file map with java