概述
Annotation 组成部分
Annotation.java
package java.lang.annotation;
public interface Annotation {
boolean equals(Object obj);
int hashCode();
String toString();
Class<? extends Annotation> annotationType();
}
ElementType.java
package java.lang.annotation;
public enum ElementType {
TYPE, /* 类、接口(包括注释类型)或枚举声明 */
FIELD, /* 字段声明(包括枚举常量) */
METHOD, /* 方法声明 */
PARAMETER, /* 参数声明 */
CONSTRUCTOR, /* 构造方法声明 */
LOCAL_VARIABLE, /* 局部变量声明 */
ANNOTATION_TYPE, /* 注释类型声明 */
PACKAGE /* 包声明 */
}
RetentionPolicy.java
package java.lang.annotation;
public enum RetentionPolicy {
SOURCE, /* Annotation信息仅存在于编译器处理期间,编译器处理完之后就没有该Annotation信息了 */
CLASS, /* 编译器将Annotation存储于类对应的.class文件中。默认行为 */
RUNTIME /* 编译器将Annotation存储于class文件中,并且可由JVM读入 */
}
注解类
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation1 {
}
在反射中使用 Annotation
在反射的 Class, Method, Field 等函数中,有许多于 Annotation 相关的接口。
这也意味着,我们可以在反射中解析并使用 Annotation。
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注解
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;
}
}
package springmvc;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@interface RequestMapping {
String path() default "";
}
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(){
}
}
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;
}
}
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注解所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复