Created
October 29, 2013 17:11
-
-
Save thomasdarimont/7218774 to your computer and use it in GitHub Desktop.
Prototype of @AnnotationMixing Feature for ProjectLombok
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Base{ | |
String a() default "A"; | |
int b(); | |
int c(); | |
} | |
@interface Custom1{ | |
String a() default "AA"; | |
int b(); | |
int c(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lombok.experimental; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Target(ElementType.ANNOTATION_TYPE) | |
@Retention(RetentionPolicy.SOURCE) | |
public @interface AnnotationMixin { | |
AnyAnnotation[] copyMethodsFrom() default {}; | |
/** | |
* Placeholder annotation to enable the placement of annotations on the generated code. | |
* @deprecated Don't use this annotation, ever - Read the documentation. | |
*/ | |
@Deprecated | |
@Retention(RetentionPolicy.SOURCE) | |
@Target({}) | |
@interface AnyAnnotation {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.experimental.AnnotationMixin; | |
@interface Base{ | |
String a() default "A"; | |
int b(); | |
int c(); | |
} | |
@AnnotationMixin(copyMethodsFrom=@__({@Base})) | |
public @interface Custom1{ | |
String a() default "AA"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lombok.javac.handlers; | |
import static lombok.javac.handlers.JavacHandlerUtil.*; | |
import lombok.core.AnnotationValues; | |
import lombok.experimental.AnnotationMixin; | |
import lombok.javac.JavacASTVisitor; | |
import lombok.javac.JavacAnnotationHandler; | |
import lombok.javac.JavacNode; | |
import org.mangosdk.spi.ProviderFor; | |
import com.sun.tools.javac.tree.JCTree.JCAnnotation; | |
import com.sun.tools.javac.util.List; | |
@ProviderFor(JavacAnnotationHandler.class) | |
public class HandleAnnotationMixin extends JavacAnnotationHandler<AnnotationMixin> { | |
@Override public void handle(AnnotationValues<AnnotationMixin> annotation, JCAnnotation ast, JavacNode annotationNode) { | |
annotationNode.getAst().traverse(new JavacASTVisitor.Printer(false)); | |
deleteAnnotationIfNeccessary(annotationNode, AnnotationMixin.class); | |
deleteImportFromCompilationUnit(annotationNode, "lombok.experimental.AnnotationMixin"); | |
List<JCAnnotation> copyMethodsFrom = unboxAndRemoveAnnotationParameter(ast, "copyMethodsFrom", "@AnnotationMixin(", annotationNode); | |
for (JCAnnotation anno : copyMethodsFrom) { | |
// FYI annotationNode.getImportList() -> defs seems to contain the Type | |
// definition of the @Base annotation that I'm looking for, but I can't find a way to access it... :-( | |
// Question: How can I access the method definitions of the annotations | |
// found in copyMethodsFrom? | |
// TODO inject method declarations of the annotations found in | |
// copyMethodsFrom if such method declarations do not exist yet. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment