您的位置:首页 > 编程语言 > Java开发

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注解

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: