gpt4 book ai didi

java - 是否可以扩展@Delegate Activity 注释的DelegateProcessor?

转载 作者:行者123 更新时间:2023-12-02 12:39:59 25 4
gpt4 key购买 nike

我目前正在开发一个 Activity 注释,它是 Xtend Activity 注释 @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)

这里有什么问题吗?我做错了什么还是不可能这样做?是否有另一种方法可以创建现有 Activity 注释的改编版本,例如 @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
}

}

关于java - 是否可以扩展@Delegate Activity 注释的DelegateProcessor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44985985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com