注解的定义
package com.project;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name();
String color();
int age() default 23;
String value();
int value1();
int[] value2();
String[] value3();
Class<?> parameterType();
Class<?>[] parameterTypes();
Season season();
}
二.通过一个例子你就可以理解这个注解(Annotation)是如何作用在这个方法字段以及…
package com.project;
import jdk.jfr.Experimental;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.TYPE, ElementType.LOCAL_VARIABLE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface annotationCreat {
String value();
String name();
}
package com.project;
import org.junit.Test;
@annotationCreat(value = "1", name = "1")
public class AnnotationTest {
private int a;
@annotationCreat(value = "", name = "")
private String name;
@annotationCreat(value = "", name = "")
@Test
public void test1() {
@annotationCreat(value = "", name = "")
int i;
}
@annotationCreat(value = "", name = " ")
public AnnotationTest(int a, String name) {
this.a = a;
this.name = name;
}
@annotationCreat(value = "", name = "")
public AnnotationTest() {
}
@annotationCreat(value = "", name = "")
private void test2() {
}
private void test3(@annotationCreat(value = "", name = "") String... args) {
}
private void test4(@annotationCreat(value = "", name = "") String name, @annotationCreat(value = "", name = "") String password) {
}
}
三.对这个overwried注解的理解
package com.project;
import org.junit.Test;
public class AnnotationTest2 {
@Override
public String toString() {
return super.toString();
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
四加深一下对这个注解使用的理解(代码里面先对这个方法进行获取获取之后对这个方法上进行判断是否含有这个注解获取这个注解上的内容对这个注解上的内容进行调用)
package com.project;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface loginAnnotation {
String username();
String password();
}
package com.project;
import org.junit.Test;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
public class AnnotationUser {
@Test
public void testlogin() throws Exception
{
Class<?> aClass = Class.forName("com.project.User");
Method[] methods = aClass.getDeclaredMethods();
Object obj = aClass.newInstance();
for (Method method : methods) {
if (method.isAnnotationPresent(loginAnnotation.class)) {
loginAnnotation annotation = method.getAnnotation(loginAnnotation.class);
String password = annotation.password();
String username = annotation.username();
Object invoke = method.invoke(obj, username, password);
}else {
}
}
}
}
五进一步加深对这个注解的理解
package com.project;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = {ElementType.CONSTRUCTOR, ElementType.TYPE,ElementType.METHOD})
@Retention(value=RetentionPolicy.RUNTIME)
public @interface otherAnnotation {
int age();
String[] email();
Season[] seasonArrays();
}
package com.project;
import org.junit.Test;
@otherAnnotation(age = 12,email = "ds",seasonArrays = Season.SPRING)
public class OtherAnnotationTest {
@otherAnnotation(age = 12,email = "sd",seasonArrays = Season.SPRING1)
public OtherAnnotationTest() {
}
@Test
@otherAnnotation(age = 23,email = {"23@qq.com","张三.@qq.com"},seasonArrays = {Season.SPRING,Season.SPRING1})
public void test1()
{
}
}
package com.project;
import java.util.Arrays;
public class ReflectAnnotation {
public static void main(String[] args) throws Exception{
Class<?> aClass = Class.forName("com.project.OtherAnnotationTest");
boolean is = aClass.isAnnotationPresent(otherAnnotation.class);
otherAnnotation annotation = aClass.getAnnotation(otherAnnotation.class);
int age = annotation.age();
Season[] seasons = annotation.seasonArrays();
for (Season season : seasons) {
System.out.println(season);
}
String[] email = annotation.email();
Arrays.stream(email).forEach(s -> {
System.out.println(s);
});
}
}
六.最后还有一个注解就是使用这个注解你就可以知道这个类或者这个方法是废弃的在实际的应用当中还是十分的管用的
package com.project;
@Deprecated(since = "6")
public class AnnotationDeprecated {
public static void main(String[] args) {
test1();
}
@Deprecated
public static void test1()
{
}
public void test2()
{
}
}
class test{
public static void main(String[] args) {
AnnotationDeprecated.test1();
}
}