一、MyAnnotation 注解
package com.tydic.annotation;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
/*
ElementType.TYPE_PARAMETER
使用这个注解,就可以在方法参数上,使用注解@MyAnnotation("abc")
*/
@Repeatable(value = MyAnnotations.class)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ElementType.TYPE_PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "tydic";
}
二、MyAnnotations
package com.tydic.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotations {
MyAnnotation[] value();
}
三、TestMyAnnotation
package com.tydic.annotation;
import com.sun.istack.internal.Nullable;
import org.junit.Test;
import java.lang.reflect.Method;
/*
重复注解与类型注解
*/
public class TestMyAnnotation {
//java8还没有内置对象,使用框架(checker freamwork 提供了编译器)来使用。
private @Nullable
Object obj ;
@Test
public void test1() throws Exception {
Class<TestMyAnnotation> clazz = TestMyAnnotation.class;
Method m = clazz.getMethod("show");
MyAnnotation [] ms = m.getAnnotationsByType(MyAnnotation.class);
for (MyAnnotation myAnnotation : ms) {
System.out.println(myAnnotation.value());
}
}
@MyAnnotation("hello")
@MyAnnotation("world")
public void show(@MyAnnotation("abc") String str ){
System.out.println("aaaaaa");
}
}
最后
以上就是跳跃纸飞机最近收集整理的关于java8之重复注解与类型注解的全部内容,更多相关java8之重复注解与类型注解内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复