0

I currently have a setup like this:

public abstract class Configurable<T> {
    //...
}
public class ConfigurableBoolean extends Configurable<Boolean> {
    //...
}

I'm looking to obtain a Boolean class from ConfigurableBoolean but Integer on a ConfigurableInt (similar setup)

1
  • ((ParameterizedType) ConfigurableBoolean.class.getGenericSuperclass()).getActualTypeArguments()[0] Commented Jul 28, 2020 at 23:45

1 Answer 1

2

If it's just "one step" (the direct superclass of the type you're inspecting is Configurable<C> where C is a non-parametrized class), then it's

@SuppressWarnings("unchecked") <T> Class<? extends T> getConfigType(Class<? extends Configurable<? extends T>> clazz) {
    String err = "configuration type of " + clazz + " is too hard to find";
    if(!(clazz.getGenericSuperclass() instanceof ParameterizedType)) throw new UnsupportedOperationException(err);
    var configurable = (ParameterizedType)clazz.getGenericSuperclass();
    if(configurable.getRawType() != Configurable.class) throw new UnsupportedOperationException(err);
    var args = configurable.getActualTypeArguments();
    if(args.length != 1 || !(args[0] instanceof Class)) throw new UnsupportedOperationException(err);
    return (Class<? extends T>)args[0];
}

You could modify this to search "harder": recursing up the superclasses until it finds Configurable, and maybe even tracking the assignments of type parameters so it can handle things like

class ConfigurableList<T> extends Configurable<List<T>> { }
class ConfigurableListBoolean extends ConfigurableList<Boolean> { }

but otherwise this seems to handle your case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.