Java注释处理,在与注释类相同的包中创建源文件
我正在Eclipse中为Java编写注释处理器。我创建了一个注释Java注释处理,在与注释类相同的包中创建源文件,java,eclipse,annotations,Java,Eclipse,Annotations,我正在Eclipse中为Java编写注释处理器。我创建了一个注释@MyAnnotation,目的是任何类,比如A,用@MyAnnotation注释,都会自动生成一个“好友类”A_buddy,它与A位于同一个包中 因此,我正在创建注释处理器来完成这项工作。下面是代码 @SupportedAnnotationTypes("MyAnnotation") @SupportedSourceVersion(SourceVersion.RELEASE_7) public class MyAnnotationP
@MyAnnotation
,目的是任何类,比如A
,用@MyAnnotation
注释,都会自动生成一个“好友类”A_buddy
,它与A
位于同一个包中
因此,我正在创建注释处理器来完成这项工作。下面是代码
@SupportedAnnotationTypes("MyAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class MyAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
//find and process the annotated class
...
String annotatedClassName = ...; //the name of the annotated class.
PackageElement pkgElement = ...; //the package of the annotated class.
//create the source file
String buddyClassName = annotatedClassName + "_buddy";
JavaFileObject jfo = processingEnv.getFiler().createSourceFile(buddyClassName, pkgElement);
Writer writer = jfo.openWriter();
writer.write("Hello, buddy");
writer.close();
@SupportedAnnotationTypes(“MyAnnotation”)
@SupportedSourceVersion(SourceVersion.RELEASE_7)
公共类MyAnnotationProcessor扩展了AbstractProcessor{
@凌驾
公共布尔过程(设置打开项目属性对话框,进入Java编译器->注释处理器
。如果选中启用项目特定设置
,则可以编辑生成的源目录,告诉它将生成的类放在何处
或者,如果您只想引用其他代码中的类,可以将.apt_generated
文件夹作为源文件夹添加到项目属性Java构建路径
部分。为什么不编写eclipse插件?@nikpon事实上,我正在寻找一种方法,如果您能提出一些好的建议,我非常感谢初学者教程。