Typeresolver库的Typoetools替代方案
我正在从事一个正在使用typeTools
库的项目,当我们试图升级到Java 17时,它因这个库而破裂了,似乎不再支持它,并且有一些最新的东西是Java版本。 因此,我们的目标是找到一种替代方案,但到目前为止我还没有取得任何成功。我尝试了其他一些库,甚至使用Java反射。
这是我们正在使用的代码,因此也许这里有人可以为我提供替代方案,也可以考虑其他任何方法。
private static class Listener<T extends IEvent> {
/**
* The consumer method
*/
private final Consumer<T> consumer;
/**
* The argument type of the consumer
*/
private final Class<?> argumentType;
/**
* Default constructor
*
* @param consumer The consumer to store
*/
private Listener(final Consumer<T> consumer) {
this.consumer = consumer;
// determine the parameter type
final Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Consumer.class, consumer.getClass());
argumentType = typeArgs[0];
}
public Consumer<T> getConsumer() {
return consumer;
}
/**
* Returns whether this listener should be notified about the event passed as parameter or not
*
* @param event The event to check the compatibility with
* @return True if the listener's argument is compatible with the event received as parameter, false otherwise
*/
public boolean isCompatibleWith(final IEvent event) {
return argumentType.isAssignableFrom(event.getClass());
}
}
一切都发生在这2行中: 最终类&lt;? consumer.getClass()); grongttype = typeargs [0];
我们在这里取得的目标是提取消费者&lt; t&gt; t&gt; t&gt; t&gt; t&gt;消费者
I am working on a project in which we are using typetools
library and when we've tried to upgrade to Java 17 it broke because of this library, it seems it's not supported anymore and something's going one with newest Java versions.
Therefore we are aiming to find an alternative but I haven't had any success so far. I tried some other libraries or even with Java reflection.
This is the code where we are using it so maybe someone here can give me an alternative or think any other way to do it.
private static class Listener<T extends IEvent> {
/**
* The consumer method
*/
private final Consumer<T> consumer;
/**
* The argument type of the consumer
*/
private final Class<?> argumentType;
/**
* Default constructor
*
* @param consumer The consumer to store
*/
private Listener(final Consumer<T> consumer) {
this.consumer = consumer;
// determine the parameter type
final Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Consumer.class, consumer.getClass());
argumentType = typeArgs[0];
}
public Consumer<T> getConsumer() {
return consumer;
}
/**
* Returns whether this listener should be notified about the event passed as parameter or not
*
* @param event The event to check the compatibility with
* @return True if the listener's argument is compatible with the event received as parameter, false otherwise
*/
public boolean isCompatibleWith(final IEvent event) {
return argumentType.isAssignableFrom(event.getClass());
}
}
Everything happens in these 2 lines:
final Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Consumer.class,
consumer.getClass());
argumentType = typeArgs[0];
What we achieve here is extracting the class given in the diamond operator of Consumer<T> consumer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论