joh*_*hny 6 javascript java reflection jestjs ts-jest
我在 Java 中使用反射从 TestNG 注释中获取一些信息,如下面的代码片段所示:
Reflections reflections = new Reflections("", new MethodAnnotationsScanner())
Set<Method> annotated = reflections.getMethodsAnnotatedWith(Test.class)
Run Code Online (Sandbox Code Playgroud)
目前,我有一个带有笑话装饰插件的笑话框架,可以帮助使用注释。
我如何在 Java 中复制相同的反射方法,但在打字稿中从注释中收集此信息?
请看一下 Typescript 的Decorators + Reflect Metadata API,它也可以为 TS 编译器启用。它的工作方式与 Java 不完全相同,但您仍然可以收集有关带注释的实体的所需信息。如何收集带注释的类的小示例:
const myTypes: any[] = []
@annotation
class MyClass {
type = "report";
title: string;
constructor(t: string) {
this.title = t;
}
}
function annotation(constructor: Function) {
myTypes.push(constructor)
}
console.log('All annotated classes', myTypes);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12142 次 |
| 最近记录: |