Java注解(二)
2018-04-26 本文已影响0人
Sandy_678f
Demo目录结构:
image.png
代码:
package annotationdemotwo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Sandy
* @version ClassInfo.java, v 0.1 2018-04-26 下午6:25
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassInfo {
String value();
}
package annotationdemotwo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Sandy
* @version MethodInfo.java, v 0.1 2018-04-26
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodInfo {
String name() default "long";
String data();
int age() default 27;
}
package annotationdemotwo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Sandy
* @version FieldInfo.java, v 0.1 2018-04-26
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldInfo {
int[] value();
}
package annotationdemotwo;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
/**
* @author Sandy
* @version TestRuntimeAnnotation.java, v 0.1 2018-04-26
*/
@ClassInfo("Test Class")
public class TestRuntimeAnnotation {
@FieldInfo(value = {1,2})
public String filedInfo = "FieldInfo";
@FieldInfo(value = {10086})
public int i = 100;
@MethodInfo(name = "Monica", data = "Big")
public static String getMethodInfo(){
return TestRuntimeAnnotation.class.getSimpleName();
}
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
Class clazz = TestRuntimeAnnotation.class;
Method[] methods = clazz.getDeclaredMethods();
Field[] fields = clazz.getFields();
Constructor[] constructor = clazz.getConstructors();
//获取Class类型注解
stringBuilder.append("Class注解:").append("\n");
Annotation[] annotations = clazz.getAnnotations();
if (annotations != null)
{
stringBuilder.append(Modifier.toString(clazz.getModifiers())).append(" ")
.append(clazz.getSimpleName()).append("\n");
stringBuilder.append("注解值:").append(Arrays.toString(annotations)).append("\n\n");
}
//获取Method类型的注解
stringBuilder.append("Method注解:").append("\n");
for(Method method : methods){
annotations = method.getDeclaredAnnotations();
if (annotations != null){
stringBuilder.append(Modifier.toString(method.getModifiers())).append(" ")
.append(method.getReturnType().getSimpleName()).append(" ")
.append(method.getName()).append("\n");
stringBuilder.append("注解值:").append(Arrays.toString(annotations)).append("\n\n");
}
}
//获取Field类型的注解
stringBuilder.append("Field注解:").append("\n");
for(Field field : fields){
annotations = field.getDeclaredAnnotations();
if (annotations != null){
stringBuilder.append(Modifier.toString(field.getModifiers())).append(" ")
.append(field.getType().getSimpleName()).append(" ")
.append(field.getName()).append("\n");
stringBuilder.append("注解值:").append(Arrays.toString(annotations)).append("\n\n");
}
}
System.out.println(stringBuilder.toString());
}
}
输出:
image.png