I am currently working on an active annotation that is an adjusted version of the Xtend active annotation @Delegate
. I already have an ugly version that is just an adapted copy of the class DelegateProcessor
and its inner class Util
. But this means I copied the whole class just do adapt a few lines of code in two methods.
I tried to extend the DelegateProcessor
and Util
to override the few methods I need to change. Even in a minimal setup (see code) this is not working. I recognize that the Javadoc tags suggest me not to do this, but I cannot believe that there is no other way than copying the whole 300 lines of code.
This is my minimal setup:
import com.google.common.annotations.Beta
import com.google.common.annotations.GwtCompatible
import java.lang.annotation.Documented
import java.lang.annotation.ElementType
import java.lang.annotation.Target
import java.util.List
import org.eclipse.xtend.lib.annotations.Delegate
import org.eclipse.xtend.lib.annotations.DelegateProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.TransformationParticipant
import org.eclipse.xtend.lib.macro.declaration.MutableMemberDeclaration
/**
* Copy of the Xtend {@link Delegate} annotation.
*/
@Beta
@GwtCompatible
@Target(ElementType.FIELD, ElementType.METHOD)
@Active(DelegateDeclaredProcessor)
@Documented
annotation DelegateDeclared {
/**
* Optional list of interfaces that this delegate is restricted to.
* Defaults to the common interfaces of the context type and the annotated
* element.
*/
Class<?>[] value = #[]
}
@Beta
class DelegateDeclaredProcessor extends DelegateProcessor implements TransformationParticipant<MutableMemberDeclaration> {
override doTransform(List<? extends MutableMemberDeclaration> elements, extension TransformationContext context) {
val extension util = new Util(context) // Overridden to use my own Util class, which i want to adapt later on.
elements.forEach [
if (validDelegate) {
methodsToImplement.forEach[method|implementMethod(method)]
}
]
}
@Beta
static class Util extends DelegateProcessor.Util { // this is where I want to later override some methods.
new(TransformationContext context) {
super(context)
}
}
}
This code produces the following error when using the annotation:
Error during annotation processing:
java.lang.NullPointerException
at org.eclipse.xtend.lib.annotations.DelegateProcessor$Util.listedInterfaces(DelegateProcessor.java:258)
at org.eclipse.xtend.lib.annotations.DelegateProcessor$Util.areListedInterfacesValid(DelegateProcessor.java:184)
at org.eclipse.xtend.lib.annotations.DelegateProcessor$Util._isValidDelegate(DelegateProcessor.java:67)
at org.eclipse.xtend.lib.annotations.DelegateProcessor$Util.isValidDelegate(DelegateProcessor.java:592)
at jce.util.DelegateDeclaredProcessor.lambda$0(DelegateDeclaredProcessor.java:28)
at jce.util.DelegateDeclaredProcessor$$Lambda$15311/667735929.accept(Unknown Source)
at java.lang.Iterable.forEach(Iterable.java:75)
at jce.util.DelegateDeclaredProcessor.doTransform(DelegateDeclaredProcessor.java:36)
What is the problem here? Am I doing anything wrong or is it just not possible to do that? Is there another way to create adapted versions of existing active annotations such as @Delegate
?
looks like you miss some overrides
@Beta
static class Util extends DelegateProcessor.Util { // this is where I want to later override some methods.
extension TransformationContext context
new(TransformationContext context) {
super(context)
this.context = context
}
override getDelegates(TypeDeclaration it) {
declaredMembers.filter[findAnnotation(findTypeGlobally(DelegateDeclared)) !== null]
}
override listedInterfaces(MemberDeclaration it) {
findAnnotation(findTypeGlobally(DelegateDeclared)).getClassArrayValue("value").toSet
}
}