3

I'm having problems with Types.isAssignable() from annotations processing API. I have this two variables:

TypeMirror someType; // with some value
TypeMirror objectType = elementUtils.getTypeElement("java.lang.Object").asType();

When I execute typeUtils.isAssignable(someType, objectType) it returns false.

Why does this happen? Any type could be assigned to an Object. What am I misunderstanding?


Note: isAssignable returns the expected value with this trick:

TypeElement typeElement = (TypeElement) typeUtils.asElement(someType);
String qualifiedName = typeElement.getQualifiedName().toString();
TypeMirror copy = elementUtils.getTypeElement(qualifiedName).asType();
typeUtils.isAssignable(copy, objectType); // returns true
Brais Gabin
  • 5,827
  • 6
  • 57
  • 92

1 Answers1

0

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.

bNd
  • 7,512
  • 7
  • 39
  • 72
  • This doesn't explain why a `POJO` class cannot be assigned to `Object`. – Brais Gabin Jan 19 '15 at 16:58
  • I don't have exact answer,`Class.isAssignableFrom() essentially checks the subtyping relation. "subtype" and "subclass" are two different concepts. ` more detail refer it http://stackoverflow.com/questions/1650931/why-object-class-isassignablefromstring-class-true it may help you. – bNd Jan 19 '15 at 17:21