JDK 8 - Lambda - Reflection issues
Florian Weimer
fweimer at redhat.com
Tue Mar 25 08:54:40 UTC 2014
On 03/13/2014 09:56 AM, Pavel Bucek wrote:
> And then there is a Session object, which has method "void
> addMessageHandler(MessageHandler handler);". Obvious common use of this
> method is:
>
> session.addMessageHandler(new MessageHandler.Whole<String>() {
> @Override
> public void onMessage(String message) {
> // ...
> }
> });
>
> I can see my IDE automatically offers me to transform this to lambda
> expression (this is actually what worries me a little, because all users
> will see that and do it - because why not - it seems to be equivalent
> with anonymous class). When this suggestion is accepted, previous
> statement is transformed into:
>
> session.addMessageHandler((MessageHandler.Whole<String>) message -> {
> // ...
> });
>
> which looks prettier, but just does not work and cannot work :/
Why doesn't it work? Does the implementation try to extract the String
actual type argument? Then it's unsound because
session.addMessageHandler(new MessageHandler.Whole<T>() {
@Override
public void onMessage(T message) {
// ...
}
});
type-checks (assuming that T is a type parameter), but will not work at
run time, either.
--
Florian Weimer / Red Hat Product Security Team
More information about the jdk8-dev
mailing list