white__cat的专栏

http://www.jeeweb.cn/

java annotation processing 自动生成代码

package example;

import java.io.*;
import javax.tools.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;

@SupportedAnnotationTypes(value = { "*" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class ExampleProcessor extends AbstractProcessor {

	private Filer filer;

	public void init(ProcessingEnvironment env) {
		filer = env.getFiler();
	}

	public boolean process(Set elements, RoundEnvironment env) {
		// Discover anything marked with @SuppressWarnings
		for (Element element : env
				.getElementsAnnotatedWith(SuppressWarnings.class)) {
			if (element.getKind() == ElementKind.METHOD) {
				// For any methods we find, create an aspect:
				String methodName = element.getSimpleName().toString();
				String aspectText = "public aspect Advise_" + methodName
						+ " {\n" + "  before(): execution(* " + methodName
						+ "(..)) {\n" + "    System.out.println(\""
						+ methodName + " running\");\n" + "  }\n" + "}\n";
				try {
					JavaFileObject file = filer.createSourceFile("Advise_"
							+ methodName, element);
					file.openWriter().append(aspectText).close();
					System.out.println("Generated aspect to advise "
							+ methodName);
				} catch (IOException ioe) {
				}
			}
		}
		return true;
	}
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/white__cat/article/details/40922115
个人分类: java web
上一篇C# Winform如何弹出一个模式窗口来显示进度条
下一篇C#子窗口与父窗口交互(使用委托和事件)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭