注解
package com.java.demo;
//忽略所有警告
@SuppressWarnings("all")
public class Demo1 {
@Override
public String toString(){
return super.toString();
}
public static void main(String[] args) {
//忽略所有警告
@SuppressWarnings("all")
Person p = new Person();
}
}
@SuppressWarnings("all")
class Person2{
}
class Person{
private int age;
@SuppressWarnings("all")
public int getAge() {
return age;
}
/**
* 此方法已经被废弃,请通过setAge2操作
* @param age
*/
@Deprecated
public void setAge(int age) {
this.age = age;
}
public void setAge2(int age) {
if(age<0 || age>150){
throw new RuntimeException("年龄不合理");
}
this.age = age;
}
}
package com.java.demo;
import java.lang.annotation.*;
@MyAnnotation(value = {"张三","李四"})
public class Demo2 {
public static void main(String[] args) {
}
public void add(){
}
}
//注解是否包含在文档中
@Documented
//用途类型
@Target({ElementType.TYPE, ElementType.METHOD})
//保存策略
@Retention(RetentionPolicy.RUNTIME)
//可以继承
@Inherited
@interface MyAnnotation{
String[] value() default "李四";
int num() default 1;
}
注解与反射
创建ColumnAnnotation 注解
package com.java.demo3;
import java.lang.annotation.*;
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ColumnAnnotation {
/**
* 描述列名
* @return
*/
String columnName();
/**
* 描述类型
* @return
*/
String type();
/**
* 描述数据的长度
* @return
*/
String length();
}
创建TabkeAnnotation 注解
package com.java.demo3;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TabkeAnnotation {
/**
* 用于标注类对应的表格名称
* @return
*/
String value();
}
创建一个被注解的Book类
package com.java.demo3;
import java.util.Objects;
@TabkeAnnotation("test_Book")
public class Book {
@ColumnAnnotation(columnName = "id",type = "int",length = "11")
private int id;
@ColumnAnnotation(columnName = "name",type = "varchar",length = "50")
private String name;
@ColumnAnnotation(columnName = "info",type = "varchar",length = "1000")
private String info;
public Book() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return id == book.id && Objects.equals(name, book.name) && Objects.equals(info, book.info);
}
@Override
public int hashCode() {
return Objects.hash(id, name, info);
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", info='" + info + '\'' +
'}';
}
public Book(int id, String name, String info) {
this.id = id;
this.name = name;
this.info = info;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
反射Book类注解
package com.java.demo3;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException {
Class c = Class.forName("com.java.demo3.Book");
/*Annotation[] as = c.getAnnotations();
for(Annotation a : as){
System.out.println(a);
}*/
TabkeAnnotation ta = (TabkeAnnotation) c.getAnnotation(TabkeAnnotation.class);
String value = ta.value();
System.out.println(value);
Field[] fs = c.getDeclaredFields();
for(Field f : fs){
ColumnAnnotation ca = f.getAnnotation(ColumnAnnotation.class);
System.out.println(f.getName()+"属性,对应数据库中的字段:" + ca.columnName()
+ "数据类型:" + ca.type() + "数据长度:" + ca.length());
}
}
}