是否可以扩展@Delegate活动注释的DelegateProcessor?

时间:2017-07-08 11:56:01

标签: java delegates annotations xtext xtend

我目前正在处理一个活动注释,它是Xtend活动注释@Delegate的调整版本。我已经有了一个丑陋的版本,它只是类DelegateProcessor及其内部类Util的改编版本。但这意味着我复制了整个班级,只是在两种方法中调整了几行代码。

我尝试扩展DelegateProcessorUtil以覆盖我需要更改的几个方法。即使在最小的设置(见代码),这是行不通的。我认识到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$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)

这是什么问题?我做错了什么或是不可能做到这一点?是否有另一种方法可以创建现有活动注释的改编版本,例如@Delegate

1 个答案:

答案 0 :(得分:1)

看起来你错过了一些覆盖

@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
    }

}