java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码(二)

2014-11-24 02:08:42 · 作者: · 浏览: 7
ass clazz=TestMyAnn2.class; Method method=clazz.getMethod("execute",new Class[]{}); System.out.println(method.getName()); //判断该方法是否包含MyAnnotation注解 if(method.isAnnotationPresent(MyAnn2.class)){ System.out.println("true"); //获取方法上的注解 MyAnn2 ann2=method.getAnnotation(MyAnn2.class); System.out.println("strValue:"+ann2.strValue()); System.out.println("eunmVal:"+ann2.myEnumType()); for(String str:ann2.arrayValue()){ System.out.println("arrayVal:"+str); } } //执行方法 method.invoke(new TestMyAnn2(), new Object[]{}); Annotation[] an=method.getAnnotations(); System.out.println(an.length+" "+an[0]); } }
package org.rui.annotation;

import java.lang.reflect.InvocationTargetException;

public class Test {


	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException {
		//java的反射机制   解析注解
		ParseAnnotation parse = new ParseAnnotation();
        parse.parseMethod(SayHello.class);
        parse.parseType(SayHello.class);
	

	}

}

package org.rui.annotation;

import org.rui.annotation.myAnnotation.ytsType;

@myAnnotation(classType=ytsType.util)
public class SayHello {

	
	public static void main(String[] args) {
		SayHello t=new SayHello();
		t.testHello(null);
	}
	
	@HelloWorld(name="林冲")
	@myAnnotation
	public void testHello(String name){
		if(name==null||"".equals(name)){
			System.out.println("hello wrold");
		}else{
			System.out.println(name+" say: hello wrold");
		}
		
	}

}

package org.rui.annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.rui.annotation.myAnnotation.ytsType;

public class ParseAnnotation {

  public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{
  
	  Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
    
	  for(Method method : clazz.getDeclaredMethods()){//获取类方法
		//通过类字节码   获得方法上面的注解实列
        HelloWorld say = method.getAnnotation(HelloWorld.class);
        String name = "";
        if(say != null){
        	//获得注解参数  
           name = say.name();
           //执行clazz类上面的  方法
           method.invoke(obj, name);
        }
        
      //通过类字节码   获得方法上面的注解实列
        myAnnotation myAn = (myAnnotation)method.getAnnotation(myAnnotation.class);
       if(myAn != null){
          if(ytsType.util.equals(myAn.classType())){
        	  System.out.println("myAn.classType() 默认的值   ytsType枚举中的  util");
        }else{
            System.out.println("this is a other method");
            }
        }
      }
    }
    @SuppressWarnings("unchecked")
    public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
    	
    	//通过类字节码   获得这个类上面的注解实列
    	myAnnotation myAn = (myAnnotation) clazz.getAnnotation(myAnnotation.class);
        if(myAn != null){
            if(ytsType.util.equals(myAn.classType())){
            	 System.out.println("myAn.classType() 默认的值   ytsType枚举中的  util");
               // System.out.println("this is a util class");
            }else{
                System.out.println("this is a other class");
            }
        }
    }
    
}


==========================listener====================================

package org.rui.listener;

im