Skip to content
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

Proposal: annotation support in reflection wrapper #2215

Merged
merged 9 commits into from Sep 7, 2014

Conversation

code-disaster
Copy link
Member

Hey ho,

with this PR it is possible to query annotation information at runtime via the reflection wrapper. It supports class and field annotations. The annotation interfaces themselves must be annotated with RetentionPolicy.RUNTIME to become available.

I've already finished adding wrapper functions to the non-GWT versions before I discovered the insanity that lies behind IReflectionCache...

Anyway, it seems to work now, so some of you might be interested in skim through the changes.

The ReflectionCacheSourceCreator for GWT now basically embeds and returns anonymous implementations of the annotation interfaces, fetching the values from source during compilation. I recommend to just try an example and peek at the generated code.

@MobiDevelop
Copy link
Member

Sorry, I haven't had time to look into this. I'll try to make a point of it soon. Any idea how much bloat is added by the addition of annotations to the ReflectionCache on GWT?

@code-disaster
Copy link
Member Author

There is one new type declaration per annotation interface, but this overhead should be pretty negligible.

Classes "Field" and "Type" have a new member annotations[], which is null if unused.

Each time a field or class annotation is used, annotations[] will gain one more entry, which gets a unique implementation of the annotation interface. The actual overhead depends on how many and what type of parameters the annotation exposes. At this point some caching mechanism might be an idea to reduce overhead.

An example:

@retention(RetentionPolicy.RUNTIME)
public @interface DeclareSerializeField {
String name() default "";
}

used in:

@DeclareSerializeField(name = "translate")
public Vector3 translation = new Vector3(Vector3.Zero);

is generated as:

new my.company.project.serialization.annotations.DeclareSerializeField() {
@OverRide public java.lang.String name() {
return "translate";
}
@OverRide public Class<? extends java.lang.annotation.Annotation> annotationType() {
return my.company.project.serialization.annotations.DeclareSerializeField.class;
}
}

@@ -673,6 +681,107 @@ else if (typeArg.isParameterized() != null)
return "null";
}

private String getAnnotations (Annotation[] annotations) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting on this is a bit funky, can you apply the gdx formatter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I did. I'm running IntelliJ 12 with the Eclipse Code Formatter plugin enabled. What's wrong with the format of this part?

@badlogic
Copy link
Member

This looks pretty good to me (haven't tested it). Actually, could you add a tiny little test just so we can check if the basic functionality is the same on all platforms?

Great stuff!

badlogic added a commit that referenced this pull request Sep 7, 2014
Proposal: annotation support in reflection wrapper
@badlogic badlogic merged commit 5bb197e into libgdx:master Sep 7, 2014
@code-disaster code-disaster deleted the annotations branch October 14, 2014 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants