码迷,mamicode.com
首页 > 编程语言 > 详细

java 注解分析及自定义注解

时间:2018-05-20 00:47:00      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:tty   ado   元素   动态   类加载   动态控制   构造器   java   tin   

注解概念:

   java提供了一种原程序中的元素关联任何信息和任何元数据的途径与方法。

注解分类:

   运行机制分类:源码注解,编译时注解,运行时注解。

  来源分类:JDK的注解,第三方注解,自定义注解。

自定义注解语法要求: 

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

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String name();
int age() default 18;
}

1.使用@interface关键字定义注解。

2.name()成员变量无参无异常。

3.可用default 为成员变量指定默认值

4.成员变量是受限的,合法的类型包括原始类型及String,Class,Annotation,Enumeration。

5.如果注解只有一个成员变量,则成员变量必须取名为value(),在使用时可以忽略成员变量名与赋值号(=)。

6.注解类可以没有成员变量,没成员变量的注解称为标识注解。

元注解:

   @Target(ElementType类型:CONSTURCTOR构造器方法声明,FIELD字段声明,LOCAL_VARI局部变量声明,METHOD方法声明,PACKGE包声明,PARAMETER参数声明,TYPE类接口声明)

  @Retention(RetentionPolicy类型:SOURCE只在源码中显示,编译时丢失,CLASS编译是记录到class中,运行时忽略,RUNTIME运行时存在,可以通过反射读取)

  @Inherited允许子类继承,解继承只能是类的注解,接口不能继承注解,方法上的注解也不可以被继承。

  @Documented生成javadoc文档会生成注解

解析注解:

  通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序的运行逻辑。

自定义注解案例:自定义实现sql中的@Table与@Cloumm

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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Table {
String value();
}

 

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

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cloumm {
String value();
}

 

@Table("user")
public class User {

@Cloumm("id")
int id;

@Cloumm("name")
String name;

@Cloumm("age")
int age;

@Cloumm("email")
String email;

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 int getAge() {
return age;
}

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

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}

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

public class UserTest {
public static void main(String[] args) {
User user = new User();
user.setId(10);
user.setName("zs");
user.setEmail("232142@qq.com,24343@qq.com,343222@163.com");
query(user);
}

private static void query(User user) {
//1.获取类加载器
Class c= user.getClass();
//2.获取类上的注解
boolean isExist = c.isAnnotationPresent(Table.class);
StringBuffer sb =new StringBuffer();
if(isExist) {
Table table = (Table)c.getAnnotation(Table.class);
String tableName = table.value();
sb.append("select * from "+ tableName + " where 1=1");
}
//3.获取所有字段
Field[] fields = c.getDeclaredFields();
for(Field f:fields) {
boolean fExist = f.isAnnotationPresent(Cloumm.class);
if(fExist) {
String tableName = f.getAnnotation(Cloumm.class).value();
//4获取字段值
//4.1反射获取字段方法
String fieldname = f.getName();
String methodName = "get"+fieldname.substring(0,1).toUpperCase()+fieldname.substring(1);
Object fieldValue = null;
try {
Method method = c.getMethod(methodName);
//4.2通过反射方法获取字段值
fieldValue = method.invoke(user);
} catch (Exception e) {
e.printStackTrace();
}
//4.3拼装sql
if(fieldValue==null || fieldValue instanceof Integer && (Integer)fieldValue == 0) {
continue;

}
if(fieldValue instanceof Integer) {
sb.append(" and "+tableName+" = "+fieldValue);
}
else if(fieldValue instanceof String){
if(((String) fieldValue).contains(",")) {
String[] values = ((String) fieldValue).split(",");
sb.append(" "+ tableName +" in (");
for(String v : values) {
sb.append("‘"+v+"‘,");
}
sb.deleteCharAt(sb.length()-1);
sb.append(")");
}else {
sb.append(" and "+tableName+" = ‘"+fieldValue+"‘");
}
}

System.out.println(sb.toString());
}
}
}
}

 

java 注解分析及自定义注解

标签:tty   ado   元素   动态   类加载   动态控制   构造器   java   tin   

原文地址:https://www.cnblogs.com/Wellszyw/p/9062227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!