2017-07-08 57 views
0

我目前正在研究活动注释,该注释是Xtend活动注释的调整版本@Delegate。我已经有一个丑陋的版本,只是类DelegateProcessor及其内部类Util的改编副本。但是这意味着我复制了整个类,只是用两种方法来修改几行代码。是否可以扩展@Delegate活动注释的DelegateProcessor?

我试图扩展DelegateProcessorUtil以覆盖我需要更改的几个方法。即使在最小的设置(见代码),这是行不通的。我意识到Javadoc标签建议我不要这样做,但我不相信除复制整行300行代码外别无它法。

这是我最小的设置:

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) 

有什么问题就在这里:使用注释时

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

此代码产生以下错误?我做错了什么,或者做不到这一点?是否有另一种方法来创建现有活动注释的修改版本,如@Delegate

+0

是原来@Delegate注释可能产生相同的错误相同的使用? –

+0

不,原来的@Delegate注释工作得很好。然而,我自己的不是。即使用它来做一个非常简单的代表团。 – ConveniencePatterns

回答

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 
    } 

} 
+0

确实是这个问题!非常感谢您的帮助! – ConveniencePatterns