com.github.fashionbrot.tool.MethodUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mars-tool Show documentation
Show all versions of mars-tool Show documentation
mars-tool 工具包 https://github.com/fashionbrot/mars-tool
The newest version!
package com.github.fashionbrot.tool;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@Slf4j
public class MethodUtil {
private MethodUtil(){
}
private static final String METHOD="get";
private static final String BOOLEAN_METHOD="is";
/**
* 把一个字符串的第一个字母大写、效率是最高的
* @param filedName
* @return String
*/
public static String getMethodName(String filedName){
byte[] items = filedName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
public static Object getFieldValue(Field field,Object object){
if (field!=null && !Modifier.isStatic(field.getModifiers())){
//打开私有访问
field.setAccessible(true);
try {
return field.get(object);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return null;
}
public static Object getInstance(Class clazz){
try {
return clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static boolean checkDeclaredMethod(Class> clazz,String method){
if (clazz!=null){
Method[] methods = clazz.getDeclaredMethods();
if (methods!=null && methods.length>0){
for(int i=0;i