Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.