Java解析注解

/**
 * @version 1.0 2016-09-25
 * @author Administrator
 * @demonstrate 通过反射获取类。函数或成员上的运行时注解信息,从而实现动态控制程序运行
 * 
 */

package AnnotationTest;

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

public class Test {

    public static String Query(Filter f)
    {
        //创建字符缓冲区存放sql语句
        StringBuilder sb=new StringBuilder();
        //根据反射机制,通过类的getClass()方法得到类型类,再通过此类获取该类中的相关信息
        Class c=f.getClass();

        //获取类中的注解信息
        boolean exists=c.isAnnotationPresent(Table.class);
        if(!exists)
            return null;
        //获取注解类
        Table t=(Table) c.getAnnotation(Table.class);
        //获取注解类的值(类名)
        String tableName=t.value();
        //拼凑SQL语句
        sb.append("select * from ").append(tableName).append("where 1=1");

        //获取成员变量信息
        Field[] fArray=c.getDeclaredFields();
        for(Field filed : fArray)
        {
            //判断成员变量是否有注解信息
            boolean fExists=filed.isAnnotationPresent(Column.class);
            if(!fExists)
                continue;
            Column column=filed.getAnnotation(Column.class);
            String columnName=column.value();
            //获取成员变量名
            String filedName=filed.getName();
            //拼凑成方法名其实也是可以通过getMethod()获取方法名
            String getMethodName="get"+filedName.substring(0, 1)
            .toUpperCase()+filedName.substring(1);

            Object filedValue=null;
            try {
                Method getMethod=c.getMethod(getMethodName);
                try {
                    filedValue=getMethod.invoke(f, null);
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    e.printStackTrace();
                }

            } catch (SecurityException | NoSuchMethodException e) {
                e.printStackTrace();
            }
            //去掉没有被赋值的
            if(filedValue==null || filedValue instanceof Integer && (Integer)filedValue==0)
                continue;
            sb.append(" and ").append(filedName).append("=").append(filedValue);

        }

        return sb.toString();
    }

    public static void main(String[] args) {


        Filter f1=new Filter();
        f1.setId(12);
        f1.setUserName("大白晓敏");

        Filter f2=new Filter();
        f2.setAge(21);
        f2.setId(11);

        Filter f3=new Filter();
        f3.setUserName("小夏");

        String sql1=Query(f1);
        String sql2=Query(f2);
        String sql3=Query(f3);

        System.out.println(sql1);
        System.out.println(sql2);
        System.out.println(sql3);

    }

}

package AnnotationTest;

@Table("user")
public class Filter {

    @Column("id")
    private int id;

    @Column("user_name")
    private String userName;

    @Column("age")
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

package AnnotationTest;

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

/*
 * FIELD作用于属性变量
 * TYPE作用于类
 */
@Target({ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)
public @interface Column {

    String value();

}

package AnnotationTest;

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

//注解的应用范围
@Target({ElementType.TYPE})

//运行时刻对注解进行解析
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {

    //没有任何参数,指定默认名必须为value
    String value();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值