简体   繁体   English

使用使用者时无法解析方法

[英]Cannot resolve method when using consumer

I have an InputStream that load properties file. 我有一个InputStream加载属性文件。 Normally I use properties.load(is); 通常我使用properties.load(is); But I would like to change it to Consumer as I would like to avoid using catches so I created an ThrowingConsumer that's avoid id, but the problem is that even I use normal Consumer it seems to be working when I declare that will be an InputStream, but my ThrowingConsumer is generic so it looks like that 但是我想将其更改为Consumer,因为我想避免使用catch,所以我创建了一个避免id的ThrowingConsumer,但是问题是,即使我使用普通的Consumer,当我声明它将为InputStream时似乎也可以正常工作,但是我的ThrowingConsumer是通用的,所以看起来像

@FunctionalInterface
public interface ThrowingConsumer<T, E extends Throwable>
{
    void accept(T t) throws E;

    static <T, E extends Throwable> Consumer<T> unchecked(ThrowingConsumer<T, E> consumer)
    {
        return t ->
        {
            try
            {
                consumer.accept(t);
            }
            catch (Throwable e)
            {
                throw new UnsupportedOperationException(e);
            }
        };
    }
}

And when I use ThrowingConsumer.unchecked(properties::load).accept(is); 当我使用ThrowingConsumer.unchecked(properties::load).accept(is); it's screaming that cannot resolve method load. 尖叫无法解决方法负载。 How could I avoid it? 我该如何避免呢?

The load() method is overloaded, so inference can't choose between the two overloads. load()方法已重载,因此无法在两个重载之间进行选择。

Use a lambda: 使用lambda:

ThrowingConsumer.unchecked((InputStream i) -> properties.load(i)).accept(is);

or cast your method reference to the right type: 或将您的方法引用转换为正确的类型:

ThrowingConsumer.unchecked((ThrowingConsumer<InputStream, IOException>) properties::load).accept(is);

Quite frankly, I think you're abusing consumers here, and that a good old try catch block would make things much easier to read. 坦率地说,我认为您是在滥用消费者,而且一个好的老式尝试挡块会使事情更容易阅读。 UnsupportedOperationException is also not at all the correct exception to load when you can't read from an InputStream. 当您无法从InputStream读取时,UnsupportedOperationException也不是要加载的正确异常。 UncheckedIOException would be much cleaner. UncheckedIOException将更加整洁。 And your consumer also catches exceptions that it shouldn't catch (such as NullPointerException, or OutOfMemoryError, etc.), without even chaining the original cause, making it very very hard to diagnose the problem at runtime. 您的使用者还捕获了它不应该捕获的异常(例如NullPointerException或OutOfMemoryError等),甚至没有链接到原始原因,这使得在运行时很难诊断问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用泛型时无法解析方法 - Cannot Resolve Method when using generics 使用 Picasso 库时无法解析方法 get() - Cannot resolve method get() when using the Picasso library 传递具体类时无法解析方法 - Cannot resolve method when passing a concrete class 使用 join() 方法时出现错误无法解析 &#39;String&#39; 中的方法 &#39;join&#39; - Getting the error Cannot resolve method 'join' in 'String' when using the join() method 无法使用 observable 解析方法 handleResponse - Cannot resolve method handleResponse using observable 使用anyMatch无法解析方法startsWith - Cannot resolve method startsWith by using anyMatch 无法使用流解析“对象”中的方法“get” - Cannot resolve method 'get' in 'Object' using streams 尝试使用此处广泛提倡的方法在非活动方法中执行 getResources 时,“无法解析方法 getResources” - "Cannot resolve method getResources" when trying to execute getResources in non-activity method using method widely advocated here 使用 lambda 表达式时无法解析方法“split(java.lang.String)” - Cannot resolve method 'split(java.lang.String)' when using lambda expression 在 JUnit5 中使用 Testcontainers 和 gradle 时,无法解析“DockerImageName”中的方法“parse” - Cannot resolve method 'parse' in 'DockerImageName' when using Testcontainers and gradle in JUnit5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM