当一个类返回lambda表达式为什么不反思工作 [英] Why doesn't reflection work when a class returns Lambdas
问题描述
我曾经遇到过一个有些奇怪的行为。我使用标注与特定的目的,以纪念某一类,然后我用org.reflections库来找到所有与特定注释的类。然而,当一个类实现返回lambda函数的方法,反射不会再找到注解的类。类的签名是不变的。
I have encountered a somewhat strange behaviour. I use annotations to mark certain classes with a particular purpose, and then I use org.reflections library to find all the classes with the particular annotation. However when a class implements a method that returns a lambda function, the reflection won't find the annotated class anymore. The signature of the class is unchanged.
注释的例子:
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String someValue();
}
使用注释(编辑 - 添加了BaseClass的):
Using the annotation (EDIT - added the BaseClass):
public class BaseClass {
public Consumer<Integer> doSomething(){ return null;}
}
@MyAnnotation(someValue = "a")
public class FooBar extends BaseClass {
@Override
public Consumer<Integer> doSomething() {
return null;
}
}
@MyAnnotation(someValue = "bazbar")
public class BarClass extends BaseClass {
@Override
public Consumer<Integer> doSomething(){
return (x) -> {};
}
}
查找注释
private static final Reflections reflections = new Reflections("com.mypackage");
Set<Class<?>> clazzes = reflections.getTypesAnnotatedWith(MyAnnotation.class);
现在 - 设定clazzes仅包含 1单班的FooBar的类。如果我从BazBar类中删除拉姆达返回值,一个也显示了,但只要一方法的类返回一个lambda反射不会工作。
Now - the set 'clazzes' only contains 1 single class, the FooBar class. If I remove the lambda return value from the BazBar class that one too shows up, but as soon as one method in the class returns a lambda the reflection wont work.
我迷路了,任何想法?
推荐答案
从的问题列表看来,这个库不适合用于Java尚未8功能。我认为最好的办法是报告这一问题,也许提供了一个补丁。
From the issues list it seems that this library isn't fit for Java 8 features yet. I think your best bet is reporting this problem and maybe provide a fix.
这篇关于当一个类返回lambda表达式为什么不反思工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!