Java的注解总结

注解的知识结构图(别人总结的)

Java的注解总结_第1张图片




一个注解demo,分别定义3个注解

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface VehicleName {

	String value() default "";
	
}


import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface VehicleColor {

	public enum VColor{RED,BLUE,GREEN,BLACK}
	
	VColor vehicleColor() default VColor.BLACK;
	
}

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface VehicleProvider {

	public int id() default -1;
	
	public String name() default "";
	
	public String phone() default "";
	
}


使用注解的类

import ww.VehicleColor.VColor;

public class Bicycle {

	@VehicleName("捷安特")
	private String bicycleName;
	
	@VehicleColor(vehicleColor=VColor.BLUE)
	private String bicycleColor;
	
	@VehicleProvider(id=101,name="Vehicle Store",phone="123456")
	private String vehicleProvider;

	public String getBicycleName() {
		return bicycleName;
	}

	public void setBicycleName(String bicycleName) {
		this.bicycleName = bicycleName;
	}

	public String getBicycleColor() {
		return bicycleColor;
	}

	public void setBicycleColor(String bicycleColor) {
		this.bicycleColor = bicycleColor;
	}

	public String getVehicleProvider() {
		return vehicleProvider;
	}

	public void setVehicleProvider(String vehicleProvider) {
		this.vehicleProvider = vehicleProvider;
	}
	

}


注解处理器


import java.lang.reflect.Field;

public class VehicleInfoUtil {

	public static void getVehicleInfo(Class clazz){
		
		String vehicleName = "vehicle name:";
		String vehicleColor = "vehicle color:";
		String vehicleProvider = "vehicle Provider:";
		
		// 返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。
		Field[] fields = clazz.getDeclaredFields();
		
		for(Field field:fields){
			
			// 判断该程序元素上是否包含指定类型的注解,存在则返回true,否则返回false.
			if(field.isAnnotationPresent(VehicleName.class)){
				VehicleName name = (VehicleName)field.getAnnotation(VehicleName.class);
				vehicleName = vehicleName + name.value();
				System.out.println(vehicleName);
				
			}else if(field.isAnnotationPresent(VehicleColor.class)){
				VehicleColor color = (VehicleColor)field.getAnnotation(VehicleColor.class);
				vehicleColor = vehicleColor + color.vehicleColor().toString();
				System.out.println(vehicleColor);
				
			}else if(field.isAnnotationPresent(VehicleProvider.class)){
				VehicleProvider provider = (VehicleProvider)field.getAnnotation(VehicleProvider.class);
				vehicleProvider = vehicleProvider + "Id:"+provider.id()+" Name:"+provider.name()+" Phone:"+provider.phone() ;
				System.out.println(vehicleProvider);
				
			}
			
			
		}
		
	}
	
}

执行类

public class VehicleApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		VehicleInfoUtil.getVehicleInfo(Bicycle.class);
		
	}

}




你可能感兴趣的:(编程语言)