com.github.houbb.hibernate.util.ReflectionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate Show documentation
Show all versions of hibernate Show documentation
The simple database orm hibernate.
The newest version!
/*
* Copyright (c) 2018. houbinbin Inc.
* fake All rights reserved.
*/
package com.github.houbb.hibernate.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class ReflectionUtil {
public static Boolean isType(Field field, Class clazz) {
return field.getType().equals(clazz);
}
public static String getClassName(T t) {
Class clazz = t.getClass();
return clazz.getSimpleName();
}
public static Field[] getFieldList(T t) {
Class clazz = t.getClass();
return clazz.getDeclaredFields();
}
public static Object getFieldValueForce(T t, String fieldName) {
Class clazz = t.getClass();
Object value = null;
try {
Field field = clazz.getDeclaredField(fieldName);
value = getFieldValue(t, field);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return value;
}
private static Object getFieldValue(T t, Field field) {
Object value = null;
field.setAccessible(true);
try {
value = field.get(t);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return value;
}
public static Annotation getAnnotation(T t, Class annotationClass) {
Class clazz = t.getClass();
return clazz.getAnnotation(annotationClass);
}
public static Annotation getAnnotation(Field field, Class annotationClass) {
return field.getAnnotation(annotationClass);
}
}