Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does '+' in an annotation name have some special meaning?

I was looking at the bytecode of the .jar file of the custom annotated jdk-8 in the central maven repository supplied by the Checker Framework.

There I noticed some invalid Java Code in Object.class and Class.class files. When I loaded the jar in Eclipse it was an annotation with the following syntax:

@jdk.Profile+Annotation(value=(int) 1)
public class java.lang.Object {

Now, as far as my knowledge goes, this annotation name is invalid Java. But, I'm assuming it might mean something to the compiler (similar to the names that the compiler assigns to anonymous classes). I'm not sure what and I couldn't find anything about it on searching online either. Hence, the query.

Any help is appreciated.

like image 342
helix Avatar asked May 17 '19 08:05

helix


People also ask

Why using annotations?

Annotations have a number of uses, among them: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.

Why to use annotation in Java?

Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. Let's take an example of @Override annotation.

Can we apply two annotations together?

It is also possible to use multiple annotations on the same declaration: @Author(name = "Jane Doe") @EBook class MyClass { ... } If the annotations have the same type, then this is called a repeating annotation: @Author(name = "Jane Doe") @Author(name = "John Smith") class MyClass { ... }


1 Answers

I don't think it has any special meaning. It's a system annotation related to JEP 161 which added additive "profiles" so that you can run an app on a subset of the JRE. It is added programatically so it does not need to adhere to the rules of the grammar.

Searching the OpenJDK bug tracker for this JEP turned up the commit where the "synthetic" annotation is added.

It looks as though the precedent was already established for a sun.Proprietary+Annotation and they just followed the convention.

I suspect that the reason the Sun annotation was named that way has been lost to the annals of time - it predates Java 6, before the code was open source.

I also found a commit to jdeps where they're parsing these annotations where they do not give any importance to the manner in which they are named; they just use the fully qualified name for both.


You too can start using illegal type identifiers -- no career at Oracle required!

final DynamicType.Unloaded<? extends Annotation> unloaded = new ByteBuddy()
    .with(TypeValidation.DISABLED)
    .makeAnnotation()
    .name("jdk.Profile+Annotation")
    .make();

final DynamicType.Loaded<? extends Annotation> loaded = unloaded.load(Test.class.getClassLoader());
final Class<? extends Annotation> myBadlyNamedAnnotation = loaded.getLoaded();

System.out.println(myBadlyNamedAnnotation);
like image 172
Michael Avatar answered Oct 20 '22 00:10

Michael