类型isAssignable和isSubtype误解

时间:2012-10-05 15:32:41

标签: java annotation-processing

在使用Java 6 API编写注释处理器时,我遇到了以特定方式处理所有地图的需求,但我显然误解了API的目的或如何调用它。这是让我不高兴的代码:

import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.annotation.processing.ProcessingEnvironment;
...

public String doThing(Element el, ProcessingEnvironment processingEnv) {
    // Utilities from the ProcessingEnvironment
    Types typeUtils = processingEnv.getTypeUtils();
    Elements elementUtils = processingEnv.getElementUtils();

    // The type of the element I'm handling
    TypeMirror elType = el.asType();

    // Compare the element's type to Map
    TypeMirror mapType = elementUtils.getTypeElement("java.util.Map").asType();

    System.out.println(elType + " > " + mapType + " = " + typeUtils.isSubtype(elType, mapType));
    System.out.println(mapType + " > " + elType + " = " + typeUtils.isSubtype(mapType, elType));
    System.out.println(elType + " > " + mapType + " = " + typeUtils.isAssignable(elType, mapType));
    System.out.println(mapType + " > " + elType + " = " + typeUtils.isAssignable(mapType, elType));

    // Compare the element's type to HashMap
    TypeMirror hashmapType = elementUtils.getTypeElement("java.util.HashMap").asType();

    System.out.println(elType + " > " + hashmapType + " = " + typeUtils.isSubtype(elType, hashmapType));
    System.out.println(hashmapType + " > " + elType + " = " + typeUtils.isSubtype(hashmapType, elType));
    System.out.println(elType + " > " + hashmapType + " = " + typeUtils.isAssignable(elType, hashmapType));
    System.out.println(hashmapType + " > " + elType + " = " + typeUtils.isAssignable(hashmapType, elType));


    // Compare the element's type to Object
    TypeMirror objectType = elementUtils.getTypeElement("java.lang.Object").asType();

    System.out.println(elType + " > " + objectType + " = " + typeUtils.isSubtype(elType, objectType));
    System.out.println(objectType + " > " + elType + " = " + typeUtils.isSubtype(objectType, elType));
    System.out.println(elType + " > " + objectType + " = " + typeUtils.isAssignable(elType, objectType));
    System.out.println(objectType + " > " + elType + " = " + typeUtils.isAssignable(objectType, elType));
}

鉴于此,这是它的输出:

java.util.HashMap<K,V> > java.util.Map<K,V> = false
java.util.Map<K,V> > java.util.HashMap<K,V> = false
java.util.HashMap<K,V> > java.util.Map<K,V> = false
java.util.Map<K,V> > java.util.HashMap<K,V> = false

java.util.HashMap<K,V> > java.util.HashMap<K,V> = true
java.util.HashMap<K,V> > java.util.HashMap<K,V> = true
java.util.HashMap<K,V> > java.util.HashMap<K,V> = true
java.util.HashMap<K,V> > java.util.HashMap<K,V> = true

java.util.HashMap<K,V> > java.lang.Object = true
java.lang.Object > java.util.HashMap<K,V> = false
java.util.HashMap<K,V> > java.lang.Object = true
java.lang.Object > java.util.HashMap<K,V> = false

这对我来说非常有意义,除了我希望HashMap元素可以分配给Map的第一个块,我希望HashMap是Map的子类型。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:9)

我怀疑这是因为类型变量。 HashMap<String, String>可分配给Map<String, String>,但如果没有类型变量的具体实例,则无法确定任意HashMap<A,B>是否可分配给Map<X,Y>

如果使用通配符实例化变量,那么您应该得到预期的结果

DeclaredType wildcardMap = typeUtils.getDeclaredType(
    elementUtils.getTypeElement("java.util.Map"),
    typeUtils.getWildcardType(null, null),
    typeUtils.getWildcardType(null, null));

这将为您提供Map<?,?>的类型镜像,所有HashMap实例都可以分配给它。

答案 1 :(得分:2)

更新(2016年3月):根据@ user1643723的评论,2012年我似乎没有types.erasure(TypeMirror)库功能。


基于Ian's Answer,我现在使用以下方法匹配基本类型,就像我在问题中为Map描述的那样。

TypeElement COLLECTION = elementUtils.getTypeElement("java.util.Collection");
TypeElement MAP = elementUtils.getTypeElement("java.util.Map");
TypeElement VOID = elementUtils.getTypeElement("java.lang.Void");
WildcardType WILDCARD_TYPE_NULL = typeUtils.getWildcardType(null, null);
Map<String,DeclaredType> cachedParentTypes = new HashMap<String, DeclaredType>();

...

public static boolean isA(TypeMirror type, TypeElement typeElement) {

    // Have we used this type before?
    DeclaredType parentType = cachedParentTypes.get(typeElement.getQualifiedName().toString());
    if (parentType == null) {
        // How many generic type parameters does this typeElement require?
        int genericsCount = typeElement.getTypeParameters().size();

        // Fill the right number of types with nulls
        TypeMirror[] types = new TypeMirror[genericsCount];
        for (int i = 0; i < genericsCount; i++) {
            types[i] = WILDCARD_TYPE_NULL;
        }

        // Locate the correct DeclaredType to match with the type
        parentType = typeUtils.getDeclaredType(typeElement, types);

        // Remember this DeclaredType
        cachedParentTypes.put(typeElement.getQualifiedName().toString(), parentType);
    }

    // Is the given type able to be assigned as the typeElement?
    return typeUtils.isAssignable(type, parentType);
}
我调用的

if (isA(elType, VOID)) {
    isVoid = true;
} else if (isA(elType, COLLECTION) || elType.getKind() == TypeKind.ARRAY) {
    isCollectionOrArray = true;
} else if (isA(elType, MAP)){
    isMap = true;
}