import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.function.Consumer;
public class User {
private <T> void test(Consumer<T> request) {
Type[] interfaces = request.getClass().getGenericInterfaces();
for (Type type : interfaces) {
if (type instanceof ParameterizedType) {
Type actualType = ((ParameterizedType) type).getActualTypeArguments()[0];
if (actualType instanceof Class) {
System.out.println(actualType);
return;
}
}
}
System.out.println("");
}
public static void main(String[] args) {
new User().test((Consumer<Integer>) integer -> {
});
new User().test(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
}
});
}
}
uses lambda expression calls and normal calls in main, and gets the generics of Consumer in User.test, which cannot be obtained when using lambda expressions, but different calls can. Should I modify it to support lambda? The main reason is that I want to get the generics of the incoming Consumer, regardless of whether it is in the lambda way or not, ask for advice