我正在尝试使用谷歌反射扫描类中的字段以获取我的自定义注释.我不知道为什么,但结果总是空集.
测试类
public class AnnotationParser {
public void parse() {
String packagename = "com.security.rest.client";
final Reflections reflections = new Reflections(packagename);
Set<Field> subTypes = reflections.getFieldsAnnotatedWith(Wire.class);
for (Field field : subTypes) {
System.out.println("Class : " + field.getName());
}
}
public static void main(String[] args) {
new AnnotationParser().parse();
}
}
Run Code Online (Sandbox Code Playgroud)
带注释的类
public class AuthenticationClient {
@Wire
public KerberosAPI kerberosAPI;
@Wire
public KerberosSessionManager kerberosSessionManager;
}
Run Code Online (Sandbox Code Playgroud)
自定义注释
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Wire {
}
Run Code Online (Sandbox Code Playgroud)
如果需要任何其他信息,请告诉我.
我遇到了一个问题的思考.我正在尝试Set使用Reflections#getFieldsAnnotatedWith方法得到一个字段,但是当我运行单元测试时,它什么也没有返回,有人可以告诉我为什么吗?(我正在使用IntelliJ IDE)
这是我正在使用的课程,这是非常基础的.
//The test class run with junit
public class ReflectionTestingTest {
@Test
public void test() {
Reflections ref = new Reflections(AnnotatedClass.class);
assertEquals(2, ref.getFieldsAnnotatedWith(TestAnnotation.class).size());
Set<Field> fields = ref.getFieldsAnnotatedWith(TestAnnotation.class);
}
}
//The class with the annotated fields I want to have in my Set.
public class AnnotatedClass {
@TestAnnotation
public int annotatedField1 = 123;
@TestAnnotation
public String annotatedField2 = "roar";
}
//And the @interface itself
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {}
Run Code Online (Sandbox Code Playgroud)
测试失败,并显示以下消息: …
Class.getDeclaredFields()那个文档上说
返回数组中的元素未排序,并且不按任何特定顺序。
但我发现结果实际上是按完美的字母顺序而不是声明顺序排序的。发生什么了?我的JDK版本是1.8。
PS:我想知道这一点,因为我现在正在实现一种依赖于声明字段的声明顺序的对象序列化方式,这种方式不受代码混淆的影响。有更好的主意吗?
我需要在maven构建过程中为特定接口创建一个子类列表,然后在运行时使用它来加载这些类.我在我的webapp pom中添加了reflection-maven(来自谷歌代码反射),但在maven构建期间,它只包括来自Web应用程序的类,而不包括该应用程序web-inf/lib文件夹中的打包jar中的类.以下是我使用的直接配置.我查看了插件源代码,它似乎扫描了以下内容:getProject().getBuild().getOutputDirectory().
反正我是否可以配置插件来扫描项目的相关jar文件?
<plugin>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
<version>0.9.9-RC1</version>
<executions>
<execution>
<goals>
<goal>reflections</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud) 给定一个接口:
public interface A {};
Run Code Online (Sandbox Code Playgroud)
具有继承接口:
public interface B extends A {}
public interface C extends A {}
Run Code Online (Sandbox Code Playgroud)
如何以编程方式扫描以查找B和C?即如何做到这一点:
Type[] types = findAllSubInterfacesOfA(); // Returns [B.class, C.class]
Run Code Online (Sandbox Code Playgroud)
注意:我试图在这里找到接口,而不是类或实例。
我正在通过 Java 代码运行声纳云,它有一些已弃用的构造函数。我成功更新了其中大部分,但没有更新这个:
import org.reflections.util.ClasspathHelper;
private static final String PACKAGE_ENTITIES = "my.package";
new Reflections(PACKAGE_ENTITIES, new SubTypesScanner(false), ClasspathHelper.forClassLoader());
Run Code Online (Sandbox Code Playgroud)
'org.reflections.scanners.SubTypesScanner' is deprecated是官方声纳消息。
我的猜测是Scanners.SubTypes (s -> true)
有小费吗?
提前致谢。
reflections ×6
java ×5
annotations ×2
reflection ×2
class ×1
interface ×1
jar ×1
maven ×1
spring ×1
unit-testing ×1