获取Java类的类型类使用反射

huangapple go评论94阅读模式
英文:

Obtaining type class from java class using reflections

问题

I currently have a setup like this:

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

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

英文:

I currently have a setup like this:

  1. public abstract class Configurable&lt;T&gt; {
  2. //...
  3. }
  1. public class ConfigurableBoolean extends Configurable&lt;Boolean&gt; {
  2. //...
  3. }

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

答案1

得分: 2

Here is the translated content:

如果它只是"一步"(你要检查的类型的直接父类是Configurable<C>,其中C是一个非参数化类),那么它就是

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

你可以修改这个来搜索"更深":递归查找父类,直到找到Configurable,甚至可以跟踪类型参数的赋值,以处理类似于

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

但除此之外,这似乎能够处理你的情况。

英文:

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

  1. @SuppressWarnings(&quot;unchecked&quot;) &lt;T&gt; Class&lt;? extends T&gt; getConfigType(Class&lt;? extends Configurable&lt;? extends T&gt;&gt; clazz) {
  2. String err = &quot;configuration type of &quot; + clazz + &quot; is too hard to find&quot;;
  3. if(!(clazz.getGenericSuperclass() instanceof ParameterizedType)) throw new UnsupportedOperationException(err);
  4. var configurable = (ParameterizedType)clazz.getGenericSuperclass();
  5. if(configurable.getRawType() != Configurable.class) throw new UnsupportedOperationException(err);
  6. var args = configurable.getActualTypeArguments();
  7. if(args.length != 1 || !(args[0] instanceof Class)) throw new UnsupportedOperationException(err);
  8. return (Class&lt;? extends T&gt;)args[0];
  9. }

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

  1. class ConfigurableList&lt;T&gt; extends Configurable&lt;List&lt;T&gt;&gt; { }
  2. class ConfigurableListBoolean extends ConfigurableList&lt;Boolean&gt; { }

but otherwise this seems to handle your case.

huangapple
  • 本文由 发表于 2020年7月29日 07:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63144245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定