gpt4 book ai didi

Java反射派生类

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:29 25 4
gpt4 key购买 nike

这是我的场景“我有一个抽象类。有许多派生类使用注释扩展这个抽象类。此外,我有一个抽象类的方法,它反射(reflect)了一个特定派生类中的所有符号”。

// Here's a definition of annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SampleAnnotation {
int sample();
}

public abstract class A {

// Here's a method to reflect all annotations
// in particular derived class like B or C
@Override
public void foo() {

}}

public class B extends A {
@SampleAnnotation (sample = 1)
public void step1() {}

@SampleAnnotation (sample = 2)
public void step2() {}

}

public class C extends A {
@SampleAnnotation (sample = 1)
public void step1() {}

@Sample (stage = 2)
public void step2() {}
}

如何使用 java 反射来反射(reflect)特定派生类(如 B 或 C)中的所有注释?

最佳答案

也许你想到的是这个Reflections图书馆。

Using Reflections you can query your metadata such as:

  • get all subtypes of some type
  • get all types/methods/fields annotated with some annotation, w/o annotation parameters matching
  • get all resources matching matching a regular expression

关于Java反射派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11843412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com