[英]Check return type of ExecutableElement is subtype of Collection
I have an ExecutableElement
representing a getter, an example is the one below.我有一个代表 getter 的
ExecutableElement
,下面是一个例子。
public List<String> getStrings();
The only method that allows me to get the details of the return type is ExecutableElement.getReturnType() .允许我获取返回类型详细信息的唯一方法是ExecutableElement.getReturnType() 。 It gives me back a TypeMirror .
它给了我一个TypeMirror 。
I could not find anything that allows me to check if TypeMirror
returned is a subtype of Collection .我找不到任何可以让我检查返回的
TypeMirror
是否是Collection的子类型的东西。 What can I do to verify that?我能做些什么来验证这一点? I am trying to generate the source code to call one of the methods in Collection .
我正在尝试生成源代码以调用Collection中的方法之一。
You could have a look at org.netbeans.modules.java.hints.jdk.mapreduce.PreconditionsChecker你可以看看 org.netbeans.modules.java.hints.jdk.mapreduce.PreconditionsChecker
private boolean isIterbale(ExpressionTree expression) {
TypeMirror tm = workingCopy.getTrees().getTypeMirror(TreePath.getPath(workingCopy.getCompilationUnit(), expression));
if (!Utilities.isValidType(tm)) {
return false;
}
if (tm.getKind() == TypeKind.ARRAY) {
return false;
} else {
tm = workingCopy.getTypes().erasure(tm);
TypeElement typeEl = workingCopy.getElements().getTypeElement("java.util.Collection");
if (typeEl != null) {
TypeMirror collection = typeEl.asType();
collection = workingCopy.getTypes().erasure(collection);
if (this.workingCopy.getTypes().isSubtype(tm, collection)) {
return true;
}
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.