标签:etc 类型 编译 警告 tde doc warnings present not
@Deprecated //不推荐使用的过时方法
@Deprecated
public void badMethod(){
System.out.println("I am a old function");
}
@Override //必须是覆盖父类(接口)的函数
@Override
public String toString(){
return "override toString()";
}
@SuppressWarnings //关闭不恰当的编译期间警告
a.没有任何元素的注解---标记注解
b.普通注解定义、普通注解的使用
/**
* 是否是类
*/
public @interface IsClass {
String value();
boolean isClass() default true;
}
解释:该注解有两个元素value和isClass,isClass元素的目的是标记对象是否为类,该元素默认为真。
当注解的元素都有默认值的时候,名为value的元素赋值时不用写名称
@Target(ElementType.TYPE)
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface IsClass {
String value();
boolean isClass() default true;
}
说明:元注解是用来修饰注解的注解,限定了注解的注释范围,注解的生命周期【源码级,运行时级别,Class文件级】,是否可以继承,是否创建文档等
像Spring一类的注解都与运行时注解有很大的关系,下面是一个利用注解,获取二维表的表名称,表的行名称,表的列值三个属性
import java.lang.annotation.*;
/**
* Created by yangyun on 2016/12/27.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Key{
String value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface Table {
String value();//表格的名称
}
import java.util.List;
/**
* Created by yangyun on 2016/12/27.
*/
@Empty(field = "Not Empty")
@IsClass("value属性的值")
public class Annotation {
public static void main(String[] args) {
System.out.format("tableName:%s\n",GetInformation.getName(Tables.class));
try {
System.out.format("key:%s\n",GetInformation.getKey(Tables.class));
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
System.out.format("columns:%s\n",GetInformation.getCols(Tables.class));
}
}
@Table("students")
class GetInformation{
public static String getName(Class<Tables> tablesClass){
String strTableName=null;
if(tablesClass.isAnnotationPresent(Table.class)){
Table tableName=tablesClass.getAnnotation(Table.class);//表格名称
strTableName=tableName.value();
}
return strTableName;
}
public static String getKey(Class<Tables> tablesClass) throws NoSuchFieldException {
Field field=tablesClass.getDeclaredField("key");//获取private属性
String strKey=null;
if(field!=null){
Key key=field.getAnnotation(Key.class);
if(key!=null){
strKey=key.value();
}
}
return strKey;
}
public static List<String> getCols(Class<Tables> tablesClass){
List<String> columns=new ArrayList<String>();
Field[] fields=tablesClass.getDeclaredFields();
if(fields!=null) {
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
columns.add(field.getAnnotation(Column.class).value());
}
}
}
return columns;
}
}
@Table("Students")
class Tables{
@Key("Id")
private String key;
@Column("name")
private String col1;
@Column("sex")
private String col2;
}
4.3 主要的API介绍(不细说属于反射的知识)
标签:etc 类型 编译 警告 tde doc warnings present not
原文地址:http://www.cnblogs.com/yangyunnb/p/6227711.html