java注解方法原理样例

MyValueBind.java

package com.yy.method;

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.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyValueBind {
	
	enum fieldType{
		String,INT
	};
	fieldType type();
	String value();

}




Student.java

package com.yy.method;

import com.yy.method.MyValueBind.fieldType;

public class Student {
	private String name;
	private int age;
	private String id;
	public String getName() {
		return name;
	}
	
	@MyValueBind(type=fieldType.String,value="牵手无奈")
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	@MyValueBind(type=fieldType.INT,value="23")
	public void setAge(int age) {
		this.age = age;
	}
	public String getId() {
		return id;
	}
	@MyValueBind(type=fieldType.String,value="1516651")
	public void setId(String id) {
		this.id = id;
	}
	
	

}


PersistStudent.java

package com.yy.method;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PersistStudent {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			try {
				persistAStudent();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void persistAStudent() throws ClassNotFoundException, NumberFormatException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
		Object o = Class.forName("com.yy.method.Student").newInstance();
		Method[] methods = o.getClass().getDeclaredMethods();
		for(Method method:methods){
			if(method.isAnnotationPresent(MyValueBind.class)){
				MyValueBind valueBind = method.getAnnotation(MyValueBind.class);
				String type = String.valueOf(valueBind.type());
				String value = String.valueOf(valueBind.value());
				if(type.equals("INT")){
					method.invoke(o, new Integer[]{new Integer(value)});
				}else{
					method.invoke(o, new String[]{value});
				}
			}
		}
		
		Student student = (Student)o;
		System.out.println("name="+student.getName()+"  age="+student.getAge()+"  id="+student.getId());
		
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值