是否可以扩展 @Delegate 活动注释的 DelegateProcessor?
Is extending the DelegateProcessor of the @Delegate active annotation possible?
我目前正在研究一个活动注释,它是 Xtend 活动注释的调整版本 @Delegate
。我已经有了一个丑陋的版本,它只是 class DelegateProcessor
及其内部 class Util
的改编副本。但这意味着我复制了整个 class 只是在两种方法中调整了几行代码。
我尝试扩展 DelegateProcessor
和 Util
来覆盖我需要更改的几个方法。即使在最小的设置中(见代码),这也不起作用。我知道 Javadoc 标签建议我不要这样做,但我无法相信除了复制整个 300 行代码之外别无他法。
这是我的最小设置:
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)
}
}
}
此代码在使用注释时产生以下错误:
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[=11=](DelegateDeclaredProcessor.java:28)
at jce.util.DelegateDeclaredProcessor$$Lambda311/667735929.accept(Unknown Source)
at java.lang.Iterable.forEach(Iterable.java:75)
at jce.util.DelegateDeclaredProcessor.doTransform(DelegateDeclaredProcessor.java:36)
这里有什么问题?我是在做错什么,还是根本不可能这样做?是否有另一种方法来创建现有活动注释的改编版本,例如 @Delegate
?
您似乎错过了一些覆盖
@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
}
}
我目前正在研究一个活动注释,它是 Xtend 活动注释的调整版本 @Delegate
。我已经有了一个丑陋的版本,它只是 class DelegateProcessor
及其内部 class Util
的改编副本。但这意味着我复制了整个 class 只是在两种方法中调整了几行代码。
我尝试扩展 DelegateProcessor
和 Util
来覆盖我需要更改的几个方法。即使在最小的设置中(见代码),这也不起作用。我知道 Javadoc 标签建议我不要这样做,但我无法相信除了复制整个 300 行代码之外别无他法。
这是我的最小设置:
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)
}
}
}
此代码在使用注释时产生以下错误:
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[=11=](DelegateDeclaredProcessor.java:28)
at jce.util.DelegateDeclaredProcessor$$Lambda311/667735929.accept(Unknown Source)
at java.lang.Iterable.forEach(Iterable.java:75)
at jce.util.DelegateDeclaredProcessor.doTransform(DelegateDeclaredProcessor.java:36)
这里有什么问题?我是在做错什么,还是根本不可能这样做?是否有另一种方法来创建现有活动注释的改编版本,例如 @Delegate
?
您似乎错过了一些覆盖
@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
}
}