注解
是1.5 java引入的
作用:可以完整的描述程序所需要的信息,简化了一些程序难以描述的信息吗,或者是简化的一些代码的编写
标准注解
@override:方法的重写
@Deprecated: 过时的
@Supresswarning:压制警告
元注解
@Taget:表示该注解可用的位置
- TYPE: Class, interface (including annotation type), or enum declaration
- FIELD:Field declaration (includes enum constants:常量)
- METHOD:Method declaration
- PARAMETER:Formal(形参) parameter declaration
- CONSTRUCTOR:Constructor declaration
- LOCAL_VARIABLE:Local variable declaration 用于描述局部变量
- ANNOTATION_TYPE:Annotation type declaration
- PACKAGE:Package declaration
- TYPE_PARAMETER:Type parameter declaration @since 1.8 用来标注类型参数
- TYPE_USE:Use of a type @since 1.8 能标注任何类型名称
@Retention : 表示要在什么级别保存该注解信息
- source:注解将被编译器丢弃
- class:注解在class文件中可用,会被vm丢弃
- runtime:vm在运行时注解也保留注解,可以通过反射来读取注解的信息
@Documented:表示包含在javadoc中
@ Inherited:允许子类继承
注解元素
注解元素可以使用的类型:
- 所有基本类型
- String
- Class
- enum
- Annotation
- 以上类型的数组
使用了其他的类型会报错
元素的默认值
- 不能有不明确的值,要么给定默认值,要么注解的时候声明
- 不能使用null,如果要表示不存在自定义一些标记 “ ”| -1 等等
- 注解中的value是默认可以不写
写的一个sql的生成的demo
//table的表名
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBTableTest {
public String name() default "";
}
//没用到column
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ConstraintsTest {
public String column() default "";
long length() default 0;
boolean primaryKey() default false;
boolean unique() default false;
boolean nullAble() default true;
}
//sql中的数据类型
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SqlIntegerTest {
String name() default "";
int value() default 0;
ConstraintsTest constraints() default @ConstraintsTest;
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SqlStringTest {
String name() default "";
String value() default "";
ConstraintsTest constraints() default @ConstraintsTest;
}
//可以给ConstraintsTest注解设置默认值
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UniquenessTest {
ConstraintsTest constraints ()default @ConstraintsTest(unique = true);
}
@DBTableTest(name = "person")
public class PersonTest {
@SqlStringTest(name = "name",constraints = @ConstraintsTest(length = 5))
private String name;
@SqlIntegerTest(name = "age",constraints = @ConstraintsTest(length = 5))
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
//sql生成类
public class TableCreatorTest {
public static void main(String[] args) throws Exception {
String className = "annotations.test.database.PersonTest";
Class<?> aClass = Class.forName(className);
DBTableTest dbTable = aClass.getAnnotation(DBTableTest.class);
String tableName = dbTable.name();
if ("".equals(tableName)) {
tableName = aClass.getName().toUpperCase();
}
final StringBuffer sb = new StringBuffer("create tasble ");
sb.append(tableName);
List<String> columnDef = new ArrayList<String>();
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
Annotation[] annos = field.getAnnotations();
if (annos.length == 0) {
continue;
} else {
for (Annotation anno : annos) {
if (anno instanceof SqlStringTest) {
SqlStringTest anno1 = (SqlStringTest) anno;
String columnName = anno1.name();
if (columnName.length() < 1) {
columnName = field.getName();
}
ConstraintsTest constraints = anno1.constraints();
long length = anno1.constraints().length() == 0 ? 5 : constraints.length();
columnDef.add(columnName + " varchar2(" + length + ") "+ getConstraints(constraints));
} else if (anno instanceof SqlIntegerTest) {
SqlIntegerTest anno1 = (SqlIntegerTest) anno;
String columnName = anno1.name();
if (columnName.length() < 1) {
columnName = field.getName();
}
ConstraintsTest constraints = anno1.constraints();
long length = constraints.length() == 0 ? 5 : constraints.length();
columnDef.add(columnName + " int " + getConstraints(constraints));
}
}
}
}
if (!columnDef.isEmpty()) {
for (int i = 0; i < columnDef.size(); i++) {
if (i == 0) {
sb.append("(");
}else if (i == columnDef.size()-1) {
sb.append(columnDef.get(i)+");");
break;
}
sb.append(columnDef.get(i)+",");
}
}else {
System.out.println("生成sql失败,请检查field上的注解");;
}
System.out.println(sb);
}
private static String getConstraints(ConstraintsTest constraints) {
String sql = "";
if (constraints.primaryKey()) {
sql +="PRIMARY KEY ";
}else if(constraints.unique()){
sql += "unique ";
}else if(constraints.nullAble()){
sql += "not null ";
}
return sql;
}
}