All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fashionbrot.tool.MethodUtil Maven / Gradle / Ivy

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy