我在使用 Types.isAssignable()
时遇到问题来自注释处理 API。我有这两个变量:
TypeMirror someType; // with some value
TypeMirror objectType = elementUtils.getTypeElement("java.lang.Object").asType();
当我执行 typeUtils.isAssignable(someType, objectType)
时,它返回 false
。
为什么会这样?任何类型都可以分配给 Object
。我误会了什么?
注意:isAssignable
使用此技巧 返回预期值:
TypeElement typeElement = (TypeElement) typeUtils.asElement(someType);
String qualifiedName = typeElement.getQualifiedName().toString();
TypeMirror copy = elementUtils.getTypeElement(qualifiedName).asType();
typeUtils.isAssignable(copy, objectType); // returns true
最佳答案
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.
关于java - 如何正确使用 Types.isAssignable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28029248/