2025年撸撸java注解

撸撸java注解Annotation 组成部分 Annotation java package java lang annotation public interface Annotation boolean equals Object obj int hashCode String toString

大家好,我是讯享网,很高兴认识大家。

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)); } } 
小讯
上一篇 2025-02-14 14:47
下一篇 2025-03-18 19:14

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/33768.html