gpt4 book ai didi

java - 方法引用的通用功能接口(interface)的方法签名

转载 作者:行者123 更新时间:2023-11-29 05:27:16 25 4
gpt4 key购买 nike

假设你有一个方法

void m(String s) {} 

通过使用新的方法引用,您可以创建一个 Consumer从中

Consumer<String> c = Class::m;

如果您现在通过反射查看创建的 Consumer 的类,您将看到它唯一声明的方法是

void accept(Object)

如果通过创建匿名内部类以旧方式创建消费者

Consumer<String> c = new Consumer<String>() {
public void accept(String s){}
}

会有桥接方法void accept(Object)以及void accept(String) .

现在假设我们必须传递这个消费者,从而失去它的通用类型。如果你实现了 Consumer<String>而不是 Lambda 表达式,您可以通过使用反射来访问它的方法来找回它。由于通过方法引用创建的消费者只有通用方法,因此无法正常工作。有没有办法获取方法引用创建的消费者的参数类型?

最佳答案

调用原始消费者(没有通用类型)总是会导致警告。你根本不应该这样做,因为你不知道类型。原始类型没有类型。 Java 8 并没有改变这一点。

Consumer 接口(interface)具有通用类型,但您在运行时不知道。即使不是 lambda。有些类(class)可能实际上有这些信息,但这对你没有帮助。如果您确实需要知道类型,那么只需创建这样一个接口(interface)即可。

  @FunctionalInterface
public static interface StringConsumer {
void consume(String s);
}

public static void main(String[] args) throws Throwable {
StringConsumer sc = System.out::println;
sc.consume("Hello");
Method m = StringConsumer.class.getMethods()[0]; // only one: "consume"
m.invoke(sc, m.getParameters()[0].getType().getSimpleName());
}

下面是一些示例代码和输出,以了解有哪些方法以及它们如何可以和不可以被调用:

package com.example.foo;

import static java.util.Arrays.asList;

import java.lang.reflect.Method;
import java.util.function.Consumer;

public class SomeClass {
@FunctionalInterface
static interface FI {
public void m(String s);
}

static final class MyRegularClass {
@SuppressWarnings("static-method")
public void m(String s) {
System.out.println("MyRegularClass: " + s);
};
}

static final class MyGenericClass<T> {
public void m(T s) {
System.out.println("MyGenericClass: " + s);
};
}

public static void main(String[] args) throws Exception {

Consumer<String> c1 = (s) -> {
System.out.println("Lambda: " + s);
};
Consumer<String> c2 = new Consumer<String>() {
public void accept(String s) {
System.out.println("Anonym: " + s);
}
};
Consumer<String> c3 = new MyRegularClass()::m;
Consumer<String> c4 = new MyGenericClass<String>()::m;

for (Consumer<String> c : asList(c1, c2, c3, c4)) {
c.accept("regular invocation of accept(String)");

for (Method m : c.getClass().getDeclaredMethods()) {
String n = m.getName() + "(" + m.getParameters()[0].getType().getSimpleName() + ")";
try {
m.invoke(c, n);
} catch (Exception e) {
System.out.println("Did not accept String: " + n + " => " + e);
}
try {
m.setAccessible(true);
m.invoke(c, new StringBuilder("StringBuilder of ").append(n));
} catch (Exception e) {
System.out.println("Did not accept StringBuilder: " + n + " => " + e);
}
}
System.out.println("-----");
}
}
}

/* ==========================================
Output:

Lambda: regular invocation of accept(String)
Lambda: accept(Object)
Did not accept StringBuilder: accept(Object) => java.lang.reflect.InvocationTargetException
-----
Anonym: regular invocation of accept(String)
Anonym: accept(String)
Did not accept StringBuilder: accept(String) => java.lang.IllegalArgumentException: argument type mismatch
Anonym: accept(Object)
Did not accept StringBuilder: accept(Object) => java.lang.reflect.InvocationTargetException
-----
MyRegularClass: regular invocation of accept(String)
MyRegularClass: accept(Object)
Did not accept StringBuilder: accept(Object) => java.lang.reflect.InvocationTargetException
Did not accept String: get$Lambda(MyRegularClass) => java.lang.IllegalAccessException: Class com.example.foo.SomeClass can not access a member of class com.example.foo.SomeClass$$Lambda$2/1175962212 with modifiers "private static"
Did not accept StringBuilder: get$Lambda(MyRegularClass) => java.lang.IllegalArgumentException: argument type mismatch
-----
MyGenericClass: regular invocation of accept(String)
MyGenericClass: accept(Object)
Did not accept StringBuilder: accept(Object) => java.lang.reflect.InvocationTargetException
Did not accept String: get$Lambda(MyGenericClass) => java.lang.IllegalAccessException: Class com.example.foo.SomeClass can not access a member of class com.example.foo.SomeClass$$Lambda$3/617901222 with modifiers "private static"
Did not accept StringBuilder: get$Lambda(MyGenericClass) => java.lang.IllegalArgumentException: argument type mismatch
-----
*/

关于java - 方法引用的通用功能接口(interface)的方法签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22254949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com