java注解原理

写一个类名为 ID 的注解
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Id {}



根据反射得到字段,然后isAnnotationPresent判断这个字段上面是否标识了Id这个注解
如果标识了,就如何如何。。。。
		Field[] fields = clazz.getDeclaredFields();
		for (Field f : fields) {
			if (f.isAnnotationPresent(Id.class)) {
				// 业务逻辑			}
		}




带参数的注解类
@Target({METHOD, FIELD}) 
@Retention(RUNTIME)
public @interface Column {

    /**
     * (Optional) The name of the column. Defaults to 
     * the property or field name.
     */
    String value() default "";


可以用如下方式取值
Column column = (Column) f.getAnnotation(Column.class);
				if(!column.value().isEmpty())
					mapNames.put(f.getName(), column.value());
				else{
					mapNames.put(f.getName(), f.getName());
				}


未完。。。

你可能感兴趣的:(java注解)