자바 사용자 정의 주석
6119 단어 사용자 정의 주석
package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) //
public @interface TestAnnField {
public String value() default "";
}
package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //
public @interface TestAnnoMethod {
String types() default "";// , types , @TestAnnoMethod(username = "cyss") ,
String username() ;
}
package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
// , 。 @Target(ElementType.FIELD) FIELD: ,
/*
* 1.CONSTRUCTOR: 2.FIELD: 3.LOCAL_VARIABLE:
* 4.METHOD: 5.PACKAGE: 6.PARAMETER:
* 7.TYPE: 、 ( ) enum
*/
public @interface TestAnnoType {
Class<?> type();
String method();//
Class<?>[] args();
String value() default "";
}
package com.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@TestAnnoType(args = { Test.class,Test2.class }, method = "getUserAll", type = Test.class)
public class Test {
@TestAnnField(value = " ")
private String name;
@TestAnnField(value = "sfd")
private String age;
@TestAnnField(value = " ")
private String sex;
private String adr;
@TestAnnoMethod(username = "cyss")
public void methodAnno(){
System.out.println("Method||01");
}
@TestAnnoMethod(username = "xxxxxx")
public void methodAnno2(){
System.out.println("Method||02");
}
public static void main(String[] args){
Test.aminType();
try {
Class<Test> c = Test.class;
//
Field field = c.getDeclaredField("name");
//
if(field.isAnnotationPresent(TestAnnField.class)){
System.out.println("true");
}
Test s = (Test)Class.forName("com.annotation.Test").newInstance();
System.out.println(" :"+s.name);
} catch (Exception e) {
System.out.println("NO");
e.printStackTrace();
}
}
/**
*
*/
public static void aminType(){
Test test = new Test();
try {
Class<Test> c = Test.class;
Class classs = Class.forName("com.annotation.Test");
TestAnnoType annoType = (TestAnnoType) classs.getAnnotation(TestAnnoType.class);
String methodStr = annoType.method();
Class classType = annoType.type();
Class[] classTypes = annoType.args();
System.out.println("&&"+methodStr);
System.out.println("&&"+classType);
System.out.println("&&"+classTypes[0]);
System.out.println("&&"+classTypes[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
*/
public static void mainMethod() {
Test test = new Test();
try {
//
Class<Test> c = Test.class;
Method method = c.getMethod("methodAnno", new Class[] {});
TestAnnoMethod anoMethod = method.getAnnotation(TestAnnoMethod.class);
String name = anoMethod.username();
System.out.println("methodAnno :"+name);
//
for (Method method2 : c.getDeclaredMethods()) {
TestAnnoMethod anoMethod2 = method2.getAnnotation(TestAnnoMethod.class);
if (anoMethod2 != null) {
System.out.println(" :"+method2.getName()+" :"+anoMethod2.username());
}
}
//
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(" "+annotation);
}
} catch (Exception e) {
e.printStackTrace();
}
test.methodAnno();
}
/**
*
*/
public static void mainFields() {
Test test = new Test();
try {
Field[] methods = Test.class.getDeclaredFields();
for (Field method : methods) {
boolean hasAnnotation = method
.isAnnotationPresent(TestAnnField.class);
if (hasAnnotation) {
TestAnnField value = method
.getAnnotation(TestAnnField.class);
method.set(test, value.value());
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("||"+test.name);
System.out.println("||"+test.age);
System.out.println("||"+test.sex);
System.out.println("||"+test.adr);
}
}