自定义注解的定义和解析_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 自定义注解的定义和解析

自定义注解的定义和解析

 2013/10/28 12:57:39  dreamoftch  程序员俱乐部  我要评论(0)
  • 摘要:thinkinjava的例子:首先是几个自定义注解packagecom.tch.test.annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;@Target(ElementType.TYPE)@Retention
  • 标签:注解 自定义 解析

?

think in java的例子

?

首先是几个自定义注解

?

class="java">package com.tch.test.annotation;

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 DBTable {

	public String name() default "";
	
}

?

package com.tch.test.annotation;

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)
public @interface Constraints {

	public boolean primaryKey() default false;
	
	public boolean allowNull() default false;
	
	public boolean unique() default false;
	
}

?

package com.tch.test.annotation;

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)
public @interface SQLInteger {

	String name() default "";
	
	Constraints constraints() default @Constraints;
	
}

?

package com.tch.test.annotation;

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)
public @interface SQLString {

	int value() default 0;
	
	String name() default "";
	
	Constraints constraints() default @Constraints;
	
}

?

然后使用这几个注解:

?

package com.tch.test.annotation;
@DBTable(name="member")
public class Member {
	@SQLString(30)
	private String firstName;
	
	@SQLString(50)
	private String lastName;
	
	@SQLInteger
	private Integer age;
	
	@SQLString(value=30,constraints=@Constraints(primaryKey=true))
	private String handle;
	
}

?

?

解析注解,生成SQL语句

?

package com.tch.test.annotation;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Test {

	public static <T> void annotationCheck(Class<T> clazz){
		DBTable c = clazz.getAnnotation(DBTable.class);
		if(c == null){
			System.out.println("没有注解");
		}
		Field[] field = clazz.getDeclaredFields();
		List<String> columnDef = new ArrayList<String>();
		for(Field f : field){
			SQLString ss = f.getAnnotation(SQLString.class);
			SQLInteger si = f.getAnnotation(SQLInteger.class);
			if(ss != null){
				columnDef.add(" "+f.getName()+" varchar("+ss.value()+") "+getConstraints(ss.constraints()));
			}
			if(si != null){
				columnDef.add(" "+f.getName()+" int "+getConstraints(si.constraints()));
			}
		}
		StringBuilder sb = new StringBuilder();
		sb.append("create table "+c.name()+"(");
		for(String s:columnDef){
			sb.append("\n"+s+" ,");
		} 
		String result = sb.substring(0, sb.length()-1);
		result = result+"\n)";
		System.out.println(result);
	}
	public static String getConstraints(Constraints con){
		String result = "";
		if(! con.allowNull()){
			result+=" not null ";
		}
		if(con.primaryKey()){
			result+=" primary key ";
		}
		if(con.unique()){
			result+=" unique ";
		}
		return result;
	}
	
	public static void main(String[] args) {
		
		annotationCheck(Member.class);
		
	}
	
}

?

?结果:

create table member(
 firstName varchar(30)  not null  ,
 lastName varchar(50)  not null  ,
 age int  not null  ,
 handle varchar(30)  not null  primary key  
)

?

?

?

发表评论
用户名: 匿名