How to determine if jambda JDK8 can be assigned for input?

Edit : this question is distorted to such an extent that I cannot fix it, and this is a mistake somewhere in my project. The main reason for my problem is that myLambda.getClass () should not throw a ClassNotFoundException, and the lambda works as expected.

Given the interface class

Class myIf = MyIf.class; 

And an instance of lambda

 Object myLambda; 

How to determine if myLambda can be assigned MyIf without exception exception? This is a bit perplexing since myLambda.getClass () throws an exception. And the "obvious"

 myIf.isInstance(myLambda) 

returns false

+6
source share
1 answer

Correct me if my assumptions were wrong, but you would do it as usual,

 static Class<?> clazz = MyIf.class; public static void main(String[] args) throws Exception { method(() -> System.out.println("hello")); // lambda } public static void method(MyIf myIf) { System.out.println(clazz.isAssignableFrom(myIf.getClass())); } static interface MyIf { public void execute(); } 

prints

 true 

In fact, you never refer to a lambda expression, the compiler generates a (synthetic?) Class, and an instance of it is passed to your method.

+5
source

All Articles