有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用类型。是否可以正确识别?

我对来自注释处理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

共 (1) 个答案

  1. # 1 楼答案

    Types.isAssignable()

    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.