Hello all!
How do I get package name from raw type reference?
In short, here's what I do: I have a FieldReference. I get the Type of that by
calling:
Type type = sc.getSourceInfo().getField(x).getContainingClassType();
I then derive the package name of the type referenced, by:
if (type instanceof ClassType) {
ClassType ct = (ClassType) type;
String myPackageName = ct.getPackage().getFullName();
}
This works fine but not when the type reference in the target code is raw. In
such a case ct.getPackage() returns null.
How should I adjust my code to be able to derive the package name?
Thanks in advance for enlightning answers!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I guess it's arguable if an erased type should return the package that it's
base type belongs to. Because it's an implicitly defined type, it currently
does not (just like an ArrayType).
Well, there are basically two things yuo can do:
(i) check explicitly for "ct instanceof ErasedType", and then
((ErasedType)ct).getGenericType().getPackage();
(ii) call ct.getBaseClassType().getPackage(). But beware: If you call this on
a TypeParameter, you get the BaseClassType of its leftmost bound. An
IntersectionType will throw an UnsupportedOperationException (but an
IntersectionType you will not get in your above example, they are created only
when it comes to the trinary operator ("?") or by type inference of generic
methods).
Hope this helps!
Best regards,
Tobias
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello all!
How do I get package name from raw type reference?
In short, here's what I do: I have a FieldReference. I get the Type of that by
calling:
Type type = sc.getSourceInfo().getField(x).getContainingClassType();
I then derive the package name of the type referenced, by:
if (type instanceof ClassType) {
ClassType ct = (ClassType) type;
String myPackageName = ct.getPackage().getFullName();
}
This works fine but not when the type reference in the target code is raw. In
such a case ct.getPackage() returns null.
How should I adjust my code to be able to derive the package name?
Thanks in advance for enlightning answers!
Hej,
I guess it's arguable if an erased type should return the package that it's
base type belongs to. Because it's an implicitly defined type, it currently
does not (just like an ArrayType).
Well, there are basically two things yuo can do:
(i) check explicitly for "ct instanceof ErasedType", and then
((ErasedType)ct).getGenericType().getPackage();
(ii) call ct.getBaseClassType().getPackage(). But beware: If you call this on
a TypeParameter, you get the BaseClassType of its leftmost bound. An
IntersectionType will throw an UnsupportedOperationException (but an
IntersectionType you will not get in your above example, they are created only
when it comes to the trinary operator ("?") or by type inference of generic
methods).
Hope this helps!
Best regards,
Tobias
Hi Tobias!
Yes, it was of great help, thanks! I went for workaround (i) and it solved the
problem I had.