首页 > 编程语言 >java注解

java注解

时间:2022-11-20 16:05:37浏览次数:49  
标签:java String fieldValue 注解 class append Description


@Override:复写、覆盖父类方法
@Deprecated:方法已经过时了

1、普通引入类与注解:
1.1 普通的引入类使用:


2、注解分类
2.1 源码注解:只在源码中存在,编译成.class后就不存在了
2.2 编译时注解:在源码与.class中存在(比如@Override,编译时候就检查等)
2.3 运行时注解:运行阶段还会起作用,甚至影响程序运行的结果逻辑(@Autowired,
程序运行自动注入)


3、自定义注解

【注】:
成员类型受限制,合法类型包括,原始类型、String、Class、Annotation、Enumeration;
如果注解只有一个成员,成员名必须是value(),在使用时候可以忽略成员名和赋值号“=";
注解类可以没有成员,没有成员的注解称为标识注解;


!


4、使用自定义注解



5、解析注解
文件Child.java:
package com.study;
@Description("I am a class")
public class Child{
@Description("I am a function")
public void sing() {

}
}
===============================================================================
文件main.java:
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Demo {

public static void main(String[] args) {

try{
Class c = Class.forName("com.study.Child");

//1、获取类注解
boolean isBool = c.isAnnotationPresent(Description.class);
if(isBool){
Description d = (Description)c.getAnnotation(Description.class);
System.out.println(d.value()); //打印注解
}

//2、获取方法注解
//2.1 获取方法注解(方法一)
Method[] ms = c.getMethods();
for (Method m : ms){
boolean isBoolTwo = m.isAnnotationPresent(Description.class);
if(isBoolTwo){
Description d2 = (Description)m.getAnnotation(Description.class);
System.out.println(d2.value());
}
}
//2.2获取方法注解(方法二)
Method[] ms2 = c.getMethods();
for (Method m : ms2){
Annotation[] annotations = m.getAnnotations();
for(Annotation annotation:annotations){
if(annotation instanceof Description){
System.out.println(((Description) annotation).value());
}
}
}

}catch (Exception e){
e.printStackTrace();
}

}
}


6 子类继承父类(注意不是接口,接口注解不会集成)的时候,会继承父类的注解,不会继承父类方法的注解

7、自己实现注解,字段映射表:
package com.lxj.test;


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

public class Demo {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer();
sb.append("select * from ");
Filter f = new Filter();
f.setEmail("[email protected],[email protected]");
f.setId(1);
f.setNickname("aaa");
Class c = f.getClass();
boolean isBool = c.isAnnotationPresent(Table.class);
if(isBool){
Table table = (Table)c.getAnnotation(Table.class);
String tableName = table.value();
sb.append(tableName).append(" WHERE 1=1");
if(tableName != "" && tableName != null){
Field[] fields = c.getDeclaredFields();
for(Field field:fields){
boolean isBolean = field.isAnnotationPresent(Column.class);
if(!isBolean){
continue;
}
//获取当前列的表名字
Column column = field.getAnnotation(Column.class);
String columnName = column.value();

//获取类中映射的字段属性
String fieldName = field.getName();
//获取该字段对用的值 ,通过方法getXxx()
String getMethodName = "get"+fieldName.substring(0,1).toUpperCase() +fieldName.substring(1);

Object fieldValue = null;
try {
Method getMethod = c.getMethod(getMethodName);
fieldValue = getMethod.invoke(f); //取出来该字段对应的值
if(fieldValue == null){ //如果该字段没有值就过
continue;
}
sb.append(" and `");
if(fieldValue instanceof String){
if(((String) fieldValue).contains(",")){
sb.append(columnName).append("` in ").append("("+ fieldValue + ")");
}else{
sb.append(columnName).append("`=").append( "'"+ fieldValue + "'");
}

}else{
sb.append(columnName).append("`=").append(fieldValue);
}

} catch (Exception e) {
e.printStackTrace();
}
}

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

}else{
System.out.println("表名为空");
}

}else{
System.out.println("不存在Table映射关系");
}
}
}


标签:java,String,fieldValue,注解,class,append,Description
From: https://blog.51cto.com/u_15882671/5871546

相关文章