日期:2014-05-18 浏览次数:20691 次
package cn.it.test;
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;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited//希望子类可以继承
public @interface myannotation{
public String value();
}
package cn.it.test;
public abstract class AbstructClas {
@myannotation("check")//对于注解属性名为value的情况,可以不写属性名
public void test() {
}
}
package cn.it.test;
import org.junit.Test;
public class ExtendClas extends AbstructClas {
@Test
@Override
public void test() {
System.out.println("here is myannotation test!");
try {
if(this.getClass().getMethod("test",null).isAnnotationPresent(myannotation.class)){
System.out.println("这里有指定的注解!");
}else{
System.out.println("没有指定的注解!");
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}