- //TestInfo.java
- package ann;
- import java.lang.annotation.Documented;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- @Documented
- @Retention(value = RetentionPolicy.RUNTIME)
- public @interface TestInfo {
- public String key(); // ±£´ækey
- public String value(); // ±£´ævalue
- }
- //PrintUserInfo.java
- package entity;
- import ann.TestInfo;
- public class PrintUserInfo {
- @TestInfo(key = "mojianfeng", value = "莫建锋")
- public void printInfo() {
- System.out.println("这是注解信息");
- }
- private String name;
- private String pass;
- public String getName() {
- return name;
- }
- @TestInfo(key = "NAME", value = "name")
- public void setName(String name) {
- System.out.println("setName我在实体类中");
- this.name = name;
- }
- public String getPass() {
- return pass;
- }
- @TestInfo(key = "PASS", value = "pass")
- public void setPass(String pass) {
- System.out.println("setPass我在实体类中");
- this.pass = pass;
- }
- public PrintUserInfo(String name, String pass) {
- super();
- this.name = name;
- this.pass = pass;
- }
- public PrintUserInfo() {
- super();
- }
- }
- //MainTest.java
- package main;
- import java.lang.annotation.Annotation;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
- import ann.TestInfo;
- import entity.PrintUserInfo;
- public class MainTest {
- public static void main(String[] args) throws SecurityException,
- NoSuchMethodException, IllegalArgumentException,
- IllegalAccessException, InvocationTargetException,
- InstantiationException {
- Annotation ann[] = PrintUserInfo.class.getMethod("printInfo")
- .getAnnotations();
- TestInfo testInfo = (TestInfo) ann[0];
- System.out.println("key:" + testInfo.key());
- System.out.println("value:" + testInfo.value());
- showInfo(new PrintUserInfo());
- }
- public static Object showInfo(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
- Object object = null; //返回的对象
- Object oobj = obj.getClass().newInstance();
- // 模拟数据库的内容
- List<Object[]> dataList = new ArrayList<Object[]>();
- for (int i = 0; i < 500; i++) {
- dataList.add(new Object[] { "key" + i, "value" + i });
- }
- // 如果数据库的字段跟实体类的属性对应 则添加信息(通过注解实现) 模拟数据库字段
- Method[] methods = obj.getClass().getDeclaredMethods();
- for (int i = 0; i < methods.length; i++) {
- String methodName = methods[i].getName();// 方法名字
- Annotation[] tation = methods[i].getAnnotations();// 注解名字
- if (tation.length > 0) { // 有注解
- TestInfo info = (TestInfo) tation[0];
- if (methodName.contains("set")) { // setter方法
- if (methodName.substring(3, methodName.length())
- .equalsIgnoreCase(info.key())) {
- Method method = obj.getClass().getDeclaredMethod(
- methodName, methods[i].getParameterTypes()[0]);
- System.out.println("找到匹配值!我要进行实例化!");
- method.invoke(oobj,"f");
- }
- }
- }
- }
- return object;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1006201512838.html
来源: http://www.codesnippet.cn/detail/1006201512838.html