#Get supersuperclass type information

28 messages · Page 1 of 1 (latest)

chilly geode
#

I have a field x. x’s type is “SomeCoolObject”. SomeCoolObject extends CoolGeneric<String>. How could I get String from the x field? (Get the generic type of x’s super’s generic.)

Normally, I would do x.getGenericSuper(), but that won’t preserve the type information if SomeCoolObject also had separate generic types. (Eg. SomeCoolObject<T> extends CoolGeneric<String>)

remote vesselBOT
#

This post has been reserved for your question.

Hey @chilly geode! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

scenic bone
chilly geode
#

((ParameterizedType)xField.getType().getGenericSuperclass()).getActualTypeArguments()[0] -> returns T, rather than String

chilly geode
#

according to my testing it does

scenic bone
#

Well that's against what the javadoc says should be obtained and that's against my own tests. I'd want your exact code to be sure you actually have tests that contradict all this

chilly geode
#

alright one sec

#
public static ArrayList<Integer> fieldTest;
    public static void main(String[] args) throws NoSuchFieldException {
        System.out.println(((ParameterizedType) Main.class.getDeclaredField("fieldTest").getType().getGenericSuperclass()).getActualTypeArguments()[0]);
    }
#

or if discord butchered that indenting:

scenic bone
#

That isn't even close to your original question

chilly geode
#

how so?

scenic bone
#

ArrayList isn't in any way extends Something<String>

chilly geode
#

my original question was just an example...

chilly geode
scenic bone
#

Well your example is possible and what you're talking about now isn't. Because they're not the same

#

ArrayList<E> extends AbstractList<E>. It's the same E. It's not made in any way concrete

chilly geode
#

well yes, but what i'm doing is more dynamic.

I'm trying to detect the type of some field who's type is a Collection, and get the effective type of the Collection

scenic bone
#

What do you mean the effective type?

chilly geode
#

Collection<Blah> -> Blah
List<Bler> -> Bler

CoolClass extends ArrayList<Blar> {}
CoolClass -> Blar

#

obviously for wildcards i'll throw an error or something

scenic bone
#

Different cases -> different methods

#

There may exist libraries that perform that kind of analysis for you, though

chilly geode
#

ii'll look into it.