123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- package com.nuliji.tools.mybatis;
- import java.beans.PropertyDescriptor;
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- /**
- * Created by gaojie on 2017/11/9.
- */
- public class EntityField {
- private String name;
- private Field field;
- private Class<?> javaType;
- private Method setter;
- private Method getter;
- /**
- * 构造方法
- *
- * @param field 字段
- * @param propertyDescriptor 字段name对应的property
- */
- public EntityField(Field field, PropertyDescriptor propertyDescriptor) {
- if (field != null) {
- this.field = field;
- this.name = field.getName();
- this.javaType = field.getType();
- }
- if (propertyDescriptor != null) {
- this.name = propertyDescriptor.getName();
- this.setter = propertyDescriptor.getWriteMethod();
- this.getter = propertyDescriptor.getReadMethod();
- this.javaType = propertyDescriptor.getPropertyType();
- }
- }
- public void set(Object o, Object v){
- if(this.setter != null){
- try {
- this.setter.invoke(o, v);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }else if(this.field != null){
- try {
- this.field.set(o, v);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- }
- }
- public Object get(Object o){
- if(this.getter != null){
- try {
- return this.getter.invoke(o);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }else if(this.field != null){
- try {
- return this.field.get(o);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * 先创建field,然后可以通过该方法获取property等属性
- *
- * @param other
- */
- public void copyFromPropertyDescriptor(EntityField other) {
- this.setter = other.setter;
- this.getter = other.getter;
- this.javaType = other.javaType;
- this.name = other.name;
- }
- /**
- * 是否有该注解
- *
- * @param annotationClass
- * @return
- */
- public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
- boolean result = false;
- if (field != null) {
- result = field.isAnnotationPresent(annotationClass);
- }
- if (!result && setter != null) {
- result = setter.isAnnotationPresent(annotationClass);
- }
- if (!result && getter != null) {
- result = getter.isAnnotationPresent(annotationClass);
- }
- return result;
- }
- /**
- * 获取指定的注解
- *
- * @param annotationClass
- * @param <T>
- * @return
- */
- public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
- T result = null;
- if (field != null) {
- result = field.getAnnotation(annotationClass);
- }
- if (result == null && setter != null) {
- result = setter.getAnnotation(annotationClass);
- }
- if (result == null && getter != null) {
- result = getter.getAnnotation(annotationClass);
- }
- return result;
- }
- /**
- * 字段属性名
- *
- * @return
- */
- public String getName() {
- return name;
- }
- /**
- * 获取javaType
- *
- * @return
- */
- public Class<?> getJavaType() {
- return javaType;
- }
- /**
- * 设置javaType
- *
- * @param javaType
- */
- public void setJavaType(Class<?> javaType) {
- this.javaType = javaType;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- EntityField that = (EntityField) o;
- return !(name != null ? !name.equals(that.name) : that.name != null);
- }
- @Override
- public int hashCode() {
- return name != null ? name.hashCode() : 0;
- }
- }
|