2013년 1월 14일 월요일

Using reflection with Android

Use reflection to find such below symbol.

  • class and its information
  • inner class and its information
  • member field and its information
  • member method and its information

Sample Code


import java.lang.reflect.Field;


/**
 * Helper class for Java reflection
 *
 * @author tnkim
 *
 */
public class ReflectHelper {

    /**
     * Return flag that expresses existing a field or not
     *
     * @param clsName class name that is included package name
     * @param fldName field name
     * @return
     */
    public static boolean hasField(String clsName, String fldName) {
        Class<?> cls = ReflectHelper.getClass(clsName);
        if (cls == null)
            return false;

        Field fieldlist[] = cls.getDeclaredFields();
        for (Field fld : fieldlist) {
            if (fld.getName().equals(fldName))
                return true;
        }

        return false;
    }

    /**
     * Return flag that expresses existing the specific field
     * or not in the specific inner class
     *
     * @param clsName class name that is included package name
     * @param fldName field name
     * @return
     */
    public static boolean hasFieldInInnerClass(String clsName, String innerClsName, String fldName) {
        Class<?> cls = ReflectHelper.getInnerClass(clsName, innerClsName);
        if (cls == null)
            return false;

        Field fieldlist[] = cls.getDeclaredFields();
        for (Field fld : fieldlist) {
            if (fld.getName().equals(fldName))
                return true;
        }

        return false;
    }

    /**
     * Return flag that expresses existing the specific inner class
     * or not in the specific class
     *
     * @param clsName class name that is included package name
     * @param fldName field name
     * @return
     */
    public static boolean hasInnerClass(String clsName, String innerClsName) {
        Class<?> innerCls = getInnerClass(clsName, innerClsName);
        if (innerCls == null)
            return false;

        return true;
    }

    /**
     * Return a Class for the specific clsName.
     *
     * @param clsName
     * @return
     */
    public static Class<?> getClass(String clsName) {
        Class<?> cls = null;
        try {
            cls = Class.forName(clsName);
        } catch (ClassNotFoundException e) {
            cls = null;
        }
        return cls;
    }


    /**
     * Return an inner class for the specific clsName
     * and the specific innerClsName.
     *
     * @param clsName
     * @param innerClsName
     * @return
     */
    public static Class<?> getInnerClass(String clsName, String innerClsName) {
        Class<?> cls = ReflectHelper.getClass(clsName);
        if (cls == null)
            return null;

        StringBuilder builder = new StringBuilder(clsName);
        builder.append('$').append(innerClsName);
        String clsNInnerClsName = builder.toString();
        Class<?>[] classList = cls.getClasses();
        for (Class<?> tmpCls: classList) {
            if (tmpCls.getName().equals(clsNInnerClsName)) {
                return tmpCls;
            }
        }

        return null;
    }
}

댓글 없음: