我是靠谱客的博主 犹豫猫咪,这篇文章主要介绍撸撸java注解,现在分享给大家,希望可以做个参考。

Annotation 组成部分

Annotation.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
package java.lang.annotation; public interface Annotation { boolean equals(Object obj); int hashCode(); String toString(); Class<? extends Annotation> annotationType(); }

ElementType.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package java.lang.annotation; public enum ElementType { TYPE, /* 类、接口(包括注释类型)或枚举声明 */ FIELD, /* 字段声明(包括枚举常量) */ METHOD, /* 方法声明 */ PARAMETER, /* 参数声明 */ CONSTRUCTOR, /* 构造方法声明 */ LOCAL_VARIABLE, /* 局部变量声明 */ ANNOTATION_TYPE, /* 注释类型声明 */ PACKAGE /* 包声明 */ }

RetentionPolicy.java

复制代码
1
2
3
4
5
6
7
8
9
package java.lang.annotation; public enum RetentionPolicy { SOURCE, /* Annotation信息仅存在于编译器处理期间,编译器处理完之后就没有该Annotation信息了 */ CLASS, /* 编译器将Annotation存储于类对应的.class文件中。默认行为 */ RUNTIME /* 编译器将Annotation存储于class文件中,并且可由JVM读入 */ }

注解类

复制代码
1
2
3
4
5
6
@Documented @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation1 { }

在反射中使用 Annotation

在反射的 Class, Method, Field 等函数中,有许多于 Annotation 相关的接口。

这也意味着,我们可以在反射中解析并使用 Annotation。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * Annotation在反射函数中的使用示例 */ @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String[] value() default "unknown"; } public class Person { /** * empty()方法同时被 "@Deprecated" 和 "@MyAnnotation(value={"a","b"})"所标注 * (01) @Deprecated,意味着empty()方法,不再被建议使用 * (02) @MyAnnotation, 意味着empty() 方法对应的MyAnnotation的value值是默认值"unknown" */ @MyAnnotation @Deprecated public void empty(){ System.out.println("nempty"); } /** * sombody() 被 @MyAnnotation(value={"girl","boy"}) 所标注, * @MyAnnotation(value={"girl","boy"}), 意味着MyAnnotation的value值是{"girl","boy"} */ @MyAnnotation(value={"girl","boy"}) public void somebody(String name, int age){ System.out.println("nsomebody: "+name+", "+age); } } import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class AnnotationTest { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { // 新建Person Person person = new Person(); // 获取Person的Class实例 Class<Person> c = Person.class; // 获取 somebody() 方法的Method实例 Method somebody = c.getMethod("somebody", new Class[]{String.class, int.class}); // 执行该方法 somebody.invoke(person, new Object[]{"lily", 18}); iteratorAnnotations(somebody); // 获取 somebody() 方法的Method实例 Method mEmpty = c.getMethod("empty", new Class[]{}); // 执行该方法 mEmpty.invoke(person, new Object[]{}); iteratorAnnotations(mEmpty); } private static void iteratorAnnotations(Method somebody) { if (somebody.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation annotation = somebody.getAnnotation(MyAnnotation.class); String[] values = annotation.value(); for (String str : values) System.out.printf(str + ", "); } } }

自定义注解使用案例1-模拟springmvc的requestMapping注解

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package springmvc; import java.lang.reflect.Method; public class ParserResult { private String path; private String classpath; private Method method; public String getPath() { return path; } public Method getMethod() { return method; } public void setPath(String path) { this.path = path; } public void setMethod(Method method) { this.method = method; } public String getClasspath() { return classpath; } public void setClasspath(String classpath) { this.classpath = classpath; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
package springmvc; import java.lang.annotation.*; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @interface RequestMapping { String path() default ""; }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package springmvc; @RequestMapping(path="/root") public class RequestMappingObject { @RequestMapping(path = "method1") public void method1(){ } @RequestMapping(path = "method2") public void method2(){ } @RequestMapping(path = "method3") public void method3(){ } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package springmvc; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; public class RequestMappingParser<T> { private final Class<T> tClass; public RequestMappingParser(Class<T> tClass) { this.tClass = tClass; } public List<ParserResult> parse() { Package packages = tClass.getPackage(); //反射获取类的注解路径 RequestMapping annotationField = tClass.getAnnotation(RequestMapping.class); String rootPath = annotationField != null ? annotationField.path() : ""; if (!rootPath.startsWith("/")) { rootPath = "/"; } //反射或者所有的方法 List<ParserResult> list = new ArrayList<ParserResult>(); Method[] methods = this.tClass.getMethods(); for (Method method : methods) { ParserResult parserResult = new ParserResult(); if (method == null) { continue; } RequestMapping annotation = method.getAnnotation(RequestMapping.class); if (annotation == null) { continue; } String path = annotation.path(); if ("".equals(path)) { continue; } if(path.startsWith("/")){ parserResult.setPath(rootPath+path); }else{ parserResult.setPath(rootPath+"/"+path); } Class<?> declaringClass = method.getDeclaringClass(); String declaringClassName = declaringClass.getName(); parserResult.setClasspath(declaringClassName); list.add(parserResult); } return list; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package springmvc; import com.alibaba.fastjson.JSONObject; import java.util.List; public class RequetMappingTest { public static void main(String[] args) { RequestMappingParser requestMappingParser = new RequestMappingParser(RequestMappingObject.class); List parse = requestMappingParser.parse(); System.out.println(JSONObject.toJSONString(parse)); } }

最后

以上就是犹豫猫咪最近收集整理的关于撸撸java注解的全部内容,更多相关撸撸java注解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部