/*
 * Decompiled with CFR 0.152.
 */
package com.hst.framework.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.StringTokenizer;

public class HstReflectUtil {
    public static String getMethodName(String fieldName) {
        return "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }

    public static Method[] getMethods(Class cl, String[] fieldNames) throws IllegalArgumentException, NoSuchMethodException {
        Method[] getMethods = new Method[fieldNames.length];
        int i = 0;
        while (i < fieldNames.length) {
            getMethods[i] = cl.getMethod(HstReflectUtil.getMethodName(fieldNames[i]), new Class[0]);
            ++i;
        }
        return getMethods;
    }

    public static Method getMethod(Class cl, String fieldName) throws IllegalArgumentException, NoSuchMethodException {
        return cl.getMethod(HstReflectUtil.getMethodName(fieldName), new Class[0]);
    }

    public static Object getFieldValue(Object instance, String fieldName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Method getMethod = HstReflectUtil.getMethod(instance.getClass(), fieldName);
        getMethod.setAccessible(true);
        return getMethod.invoke(instance, new Object[0]);
    }

    public static Object getFieldExtend(Object instance, String fieldName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Method getMethod = null;
        Object oFieldValue = instance;
        StringTokenizer st = new StringTokenizer(fieldName, ".");
        while (st.hasMoreElements()) {
            getMethod = HstReflectUtil.getMethod(oFieldValue.getClass(), st.nextToken());
            getMethod.setAccessible(true);
            oFieldValue = getMethod.invoke(oFieldValue, new Object[0]);
        }
        return oFieldValue;
    }

    public static void setFieldValue(Object instance, String fieldName, String value) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        String sMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        Class[] aClazz = new Class[]{String.class};
        Object[] aoValues = new Object[]{value};
        Method setMethod = instance.getClass().getMethod(sMethodName, aClazz);
        setMethod.setAccessible(true);
        setMethod.invoke(instance, aoValues);
    }

    public static void setFieldValue(Object target, String fname, Class ftype, Object fvalue) {
        if (target == null || fname == null || "".equals(fname) || fvalue != null && !ftype.isAssignableFrom(fvalue.getClass())) {
            return;
        }
        Class<?> clazz = target.getClass();
        try {
            Method method = clazz.getDeclaredMethod("set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), ftype);
            if (!Modifier.isPublic(method.getModifiers())) {
                method.setAccessible(true);
            }
            method.invoke(target, fvalue);
        }
        catch (Exception exception) {
            try {
                Field field = clazz.getDeclaredField(fname);
                if (!Modifier.isPublic(field.getModifiers())) {
                    field.setAccessible(true);
                }
                field.set(target, fvalue);
            }
            catch (Exception exception2) {}
        }
    }

    public static String getAClassName(Class a_class) {
        if (a_class.getPackage() == null) {
            return a_class.getName();
        }
        String packageName = a_class.getPackage().getName();
        String className = a_class.getName();
        return className.substring(packageName.length() + 1);
    }

    public static Boolean containsFiled(Class cl, String field) {
        try {
            cl.getDeclaredField(field);
            return true;
        }
        catch (NoSuchFieldException noSuchFieldException) {
            return false;
        }
    }
}
