Java自定义注解及解析基础应用
2014-08-22 00:00
465 查看
摘要: @Documented
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Yts {
public enum YtsType{util, entity, service, model}
public YtsType classType() default YtsType.util;
}
一. 定义HelloWorld、Yts注解
二. 定义注解解析类
三. 测试类
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Yts {
public enum YtsType{util, entity, service, model}
public YtsType classType() default YtsType.util;
}
一. 定义HelloWorld、Yts注解
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited public @interface HelloWorld { public String name() default ""; }
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Yts { public enum YtsType{util, entity, service, model} public YtsType classType() default YtsType.util; }
二. 定义注解解析类
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import MyAnnotation.HelloWorld; import MyAnnotation.Yts; import MyAnnotation.Yts.YtsType; public class ParseAnnotation { @SuppressWarnings({ "rawtypes", "unchecked" }) public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException{ Object obj = clazz.getConstructor().newInstance(); for(Method method : clazz.getDeclaredMethods()){ HelloWorld say = method.getAnnotation(HelloWorld.class); String name = ""; if(say != null){ name = say.name(); method.invoke(obj, name); } Yts yts = (Yts)method.getAnnotation(Yts.class); if(yts != null){ if(YtsType.util.equals(yts.classType())){ System.out.println("this is a util method"); }else{ System.out.println("this is a other method"); } } } } @SuppressWarnings({ "rawtypes", "unchecked" }) public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{ Yts yts = (Yts) clazz.getAnnotation(Yts.class); if(yts != null){ if(YtsType.util.equals(yts.classType())){ System.out.println("this is a util class"); }else{ System.out.println("this is a other class"); } } } }
三. 测试类
import java.lang.reflect.InvocationTargetException; import MyAnnotation.HelloWorld; import MyAnnotation.Yts; import MyAnnotation.Yts.YtsType; @Yts(classType =YtsType.util) public class SayHell { @HelloWorld(name = "小明 ") @Yts public void sayHello(String name){ if(name == null || name.equals("")){ System.out.println("hello world!"); }else{ System.out.println(name + "say hello world!"); } } public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException { ParseAnnotation parse = new ParseAnnotation(); parse.parseMethod(SayHell.class); parse.parseType(SayHell.class); } }
相关文章推荐
- java自定义注解学习(三)_注解解析及应用
- Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
- Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- java基础加强--自定义泛型方法及其应用
- Java注解解析(三)——解析自定义注解
- 手写服务器httpserver_xml配置文件_sax解析基础应用JAVA205-206
- java 基础总结--注解(Annotation)自定义注解入门
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
- Java注解应用,自定义注解映射实现方案说明.
- Java自定义注解基础知识
- java自定义注解原理深度解析
- Java注解教程:自定义注解示例,利用反射进行解析
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java注解教程:自定义注解示例,利用反射进行解析
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java自定义Annotation的应用(注解@)
- 框架基础——全面解析Java注解