레이블이 java인 게시물을 표시합니다. 모든 게시물 표시
레이블이 java인 게시물을 표시합니다. 모든 게시물 표시

2013년 1월 14일 월요일

What is reflection of Java?

The Java reflection API allows you to do exactly that by giving you a window into the fundamental features of the language -- classes and fields and methods -- via an ordinary Java API.


Refer to this below link
  in English. Reflections on Java Reflection
  in Korean. Java 리플렉션에 대한 재고

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;
    }
}