Seb*_*iot 7 java generics annotations
我之前问过一个例子"注释处理器",它会为一个接口生成一个代理/代理,但没有得到答案,也没有在互联网上找到任何东西,所以我自己做了.
到目前为止它运行良好,直到我尝试在超级界面中使用泛型.如果我在带注释的界面中使用泛型,它可以正常工作(更多是偶然而非设计).但是,如果带注释的接口扩展了另一个采用泛型类型参数的接口,则该参数不会"绑定"到注释接口在扩展超级接口时使用的类型.例:
public interface TestFragment<E> {
void test(E dummy);
}
@CreateWrapper
public interface TestService extends TestFragment<String> {
double myOwnMethod();
}
Run Code Online (Sandbox Code Playgroud)
这会产生:
// ...
public void test(final E dummy) {
wrapped.test(dummy);
}
// ...
Run Code Online (Sandbox Code Playgroud)
而不是正确的:
// ...
public void test(final String dummy) {
wrapped.test(dummy);
}
// ...
Run Code Online (Sandbox Code Playgroud)
在生成的方法中生成参数的代码如下所示:
int count = 0;
for (VariableElement param : method.getParameters()) {
if (count > 0) {
pw.print(", ");
}
count++;
pw.printf("final %s %s", param.asType().toString(),
param.getSimpleName().toString());
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
您需要的是替换,给定类型变量到类型参数的映射。在这种情况下,E->String
。E
将任何类型中的任何替换为String
中没有这样的支持javax.lang.model.util.Types
,您需要自己推出。基本上
void print(TypeMirror type, Map<TypeVariable,TypeMirror> substitution)
if(substitution.containsKey(type)) // type is a var, E
print( substitution.get(type) ); // String
else if(type instanceof DeclaredType) // e.g. List<E>
print( type.asElement().getSimpleName() ); // List
for(TypeMirror arg : type.getTypeArguments() ) // E
print(arg, substitution)
etc. something like that
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3435 次 |
最近记录: |