Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Created October 29, 2013 17:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasdarimont/7218774 to your computer and use it in GitHub Desktop.
Save thomasdarimont/7218774 to your computer and use it in GitHub Desktop.
Prototype of @AnnotationMixing Feature for ProjectLombok
@interface Base{
String a() default "A";
int b();
int c();
}
@interface Custom1{
String a() default "AA";
int b();
int c();
}
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 {}
}
import lombok.experimental.AnnotationMixin;
@interface Base{
String a() default "A";
int b();
int c();
}
@AnnotationMixin(copyMethodsFrom=@__({@Base}))
public @interface Custom1{
String a() default "AA";
}
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