首页 > 编程 > Java > 正文

java的annotation自定义

2019-11-08 01:33:53
字体:
来源:转载
供稿:网友

在写java时,注解随处可见。

python 的 类似注解的方式比较简单

def log(PRefix): def log_decorator(f): def wrapper(*args, **kw): print '[%s] %s()...' % (prefix, f.__name__) return f(*args, **kw) return wrapper return log_decorator@log('INFO')def test(): passprint test()

打印出来的结果:

[INFO] test()...None

java是怎样的?

一个简单的注解

有三个类,一个是定义annotatoin,一个是使用annotation,一个测试类 定义annotaoin

@Retention(RetentionPolicy.RUNTIME)@Target( {ElementType.TYPE }) //类可用public @interface OneAnnotation { String name();}

使用annotation

@OneAnnotation(name="userAnnotation")public class UserAnnotation {}

测试类

public class OneTest { @Test public void test(){ UserAnnotation u = new UserAnnotation(); Annotation[] annotations = u.getClass().getAnnotations(); for (int i = 0; i <annotations.length ; i++) { Annotation tmpa = annotations[i]; if(tmpa instanceof OneAnnotation){ System.out.println(((OneAnnotation) tmpa).name()); //打印,结果为userAnnotation } } }}

上面是加到class上,怎么加到方法上?属性上?….

可以用到哪里,用@Target, @Target( { ElementType.METHOD, ElementType.TYPE }) ,表示加到方法和类都行,具体的请看后面的 附

注解处理器是什么,能做啥?

注解处理器是运行它自己的虚拟机JVM中。是的,你没有看错,javac启动一个完整Java虚拟机来运行注解处理器。这对你意味着什么?你可以使用任何你在其他java应用中使用的的东西。使用guava。如果你愿意,你可以使用依赖注入工具,例如dagger或者其他你想要的类库。1

对注解处理器了解不多,这里把例子复述一遍 两个工程,一个用来做成jar,另一个用来测试.效果是测试工程编译的时候,会把jar中的内容给显示出来.

我用maven 做jar 工程, 这里写图片描述

PrintMe.java

@Retention(RetentionPolicy.SOURCE)public @interface PrintMe {}

MyProcessor.java

@SupportedAnnotationTypes({"com.yp.annotation.PrintMe"})public class MyProcessor extends AbstractProcessor { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) { System.out.println("这是annotation的process啊!!!!!!"); return true; } @Override public synchronized void init(ProcessingEnvironment processingEnv) { System.out.println("这是annotation的init啊!!!!!!"); super.init(processingEnv); } @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); }}

javax.annotation.processing.Processor文件

com.yp.annotation.MyProcessor

上面的工程打成jar包

测试工程,随便一个地方加上 @PrintMe,比如

@PrintMepublic class DJsonUtils { //...}

测试工程也是用maven 的,在pom.xml 引入上面生成的jar

<dependency> <groupId>com.yp</groupId> <artifactId>annotation</artifactId> <version>1.0-SNAPSHOT</version> <scope>system</scope> <systemPath>${project.basedir}/lib/annotation-1.0-SNAPSHOT.jar</systemPath> </dependency>

编译的时候,会打印下面的情况

[INFO] ------------------------------------------------------------------------[INFO] Building lang 1.5[INFO] ------------------------------------------------------------------------[WARNING] The artifact commons-email:commons-email:jar:1.1 has been relocated to org.apache.commons:commons-email:jar:1.1[INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ lang ---[debug] execute contextualize[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Copying 3 resources[INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ lang ---[INFO] Compiling 9 source files to E:/codeplace/idea-own/d-common-lang/target/classes这是annotation的init啊!!!!!!这是annotation的process啊!!!!!!这是annotation的process啊!!!!!!

process中的方法能做些什么?

//TODO

process能不能在运行过程中使用呢?比如,打印出一个方法的运行时间? 2

//TODO


附annotation 的annotation 相关参数

注解 Are
@Target 定义注解的作用目标
@Retention 定义注解的保留策略。RetentionPolicy.SOURCE:注解仅存在于源码中,在class字节码文件中不包含;RetentionPolicy.CLASS:默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得;RetentionPolicy.RUNTIME:注解会在class字节码文件中存在,在运行时可以通过反射获取到。
@Document 说明该注解将被包含在javadoc中
@Inherited 说明子类可以继承父类中的该注解

Target类型 说明
ElementType.TYPE 定义注解的作用目标
ElementType.FIELD 字段、枚举的常量
ElementType.METHOD 方法
ElementType.PARAMETER 方法参数
ElementType.CONSTRUCTOR 方法参数
ElementType.CONSTRUCTOR 构造函数
ElementType.LOCAL_VARIABLE 局部变量
ElementType.ANNOTATION_TYPE 注解
ElementType.PACKAGE

http://blog.csdn.net/albertfly/article/details/52402684/ ↩http://www.race604.com/annotation-processing/ ↩
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表