如何正确确定对象是lambda? [英] How to correctly determine that an object is a lambda?

查看:230
本文介绍了如何正确确定对象是lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到lambda的类是 isSynthetic()&& !isLocalOrAnonymousClass(),但我认为代理类可能也是如此。

I see that the class of a lambda is isSynthetic() && !isLocalOrAnonymousClass(), but I presume that the same may be true for proxy classes.

当然,我可以检查 getDeclaredMethods()。length == 1 并将 regexp 应用于班级名称。

Of course, I could check that getDeclaredMethods().length == 1 and apply regexp to the class name.

但是我想知道是否有一个更优雅和更健壮的选项来确定给定的对象是否是一个lambda。

However I want to know if there is a more elegant and robust option to find out if a given object is a lambda.

推荐答案

根据设计,没有正式的方法可以做到这一点。 Lambdas是语言的一部分;并通过功能接口集成到类型系统中。应该没有必要区分 Runnable 开始生命为lambda,命名类或内部类 - 它们都是Runnables。如果你认为你必须通过拆分类文件来处理lambda,你几乎肯定做错了什么!

There is no official way to do this, by design. Lambdas are part of the language; and are integrated into the type system through functional interfaces. There should be no need to distinguish a Runnable that began life as a lambda, a named class, or an inner class -- they're all Runnables. If you think you have to "deal with lambda" by taking apart the class file, you're almost certainly doing something wrong!

这篇关于如何正确确定对象是lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆