Typeresolver库的Typoetools替代方案

发布于 2025-01-28 06:14:22 字数 1524 浏览 2 评论 0原文

我正在从事一个正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文