【问题标题】:how to read class name passed to class annotation from the annotation如何从注释中读取传递给类注释的类名
【发布时间】:2018-06-18 11:04:05
【问题描述】:

我创建了自己的注解,这个注解接收到class.class。该类本身正在扩展另一个类。

我试图实现与使用@RunWith() 几乎相同的效果,但我不想运行该类,而是使用它在我的父类中获取class.class,所以我不需要将它发送到@Before注解。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface RunWithClass {
  /** @return the classes to be run */
  Class<?>[] value();
}

@RunWithClass(UnitTests.class)
public class UnitTests extends TestProxy {

and the method within TestProxy 

private void updateClassReference() {
/**
here I would like to get the class in the above case will be  UnitTests
(so I will be able to create an instance of the class for reflection
**/

}

【问题讨论】:

    标签: java junit java-annotations


    【解决方案1】:

    您可以简单地获取@RunWithClass注解的值并使用它如下:

    Class<?>[] classesToRun = this.getClass().getAnnotation(RunWithClass.class).value();
    

    如果您澄清您的问题,我可以尝试给出更好的答案。

    【讨论】:

    • 如果它解决了您的问题,您可以通过接受答案来感谢我。
    猜你喜欢
    • 1970-01-01
    • 2022-06-20
    • 2021-11-14
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    相关资源
    最近更新 更多