package com.wukongtv.utils;

import android.view.LayoutInflater;
import android.view.View;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * Some Reflections
 * Created by zhangge on 14-10-4.
 */
public class Reflections {
    public static void reflectView_onFinishInflate(View object) {
        try {
            Method m = View.class.getMethod("onFinishInflate");
            m.setAccessible(true);
            m.invoke(object);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static LayoutInflater.Factory2 reflectLayoutInflater_getPrivateFactory(LayoutInflater object) {
        try {
            Field f = LayoutInflater.class.getField("mPrivateFactory");
            f.setAccessible(true);
            Object obj = f.get(object);
            if (obj instanceof LayoutInflater.Factory2) {
                return (LayoutInflater.Factory2) obj;
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setField(Object target, String fieldName, Object value) {
        Class<?> clazz = target.getClass();
        Field f = null;
        try {
            f = clazz.getDeclaredField(fieldName);
            f.setAccessible(true);
            f.set(target, value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static Object getField(Object target, String fieldName)
            throws NoSuchFieldException, IllegalAccessException {
        Class<?> clazz = target.getClass();
        Field f = clazz.getDeclaredField(fieldName);
        f.setAccessible(true);
        return f.get(target);
    }
}

@@ -321,1055 +321,8 @@
 ns %7B
-%0A    public static void reflectView_onFinishInflate(View object) %7B%0A        try %7B%0A            Method m = View.class.getMethod(%22onFinishInflate%22);%0A            m.setAccessible(true);%0A            m.invoke(object);%0A        %7D catch (NoSuchMethodException e) %7B%0A            e.printStackTrace();%0A        %7D catch (InvocationTargetException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    public static LayoutInflater.Factory2 reflectLayoutInflater_getPrivateFactory(LayoutInflater object) %7B%0A        try %7B%0A            Field f = LayoutInflater.class.getField(%22mPrivateFactory%22);%0A            f.setAccessible(true);%0A            Object obj = f.get(object);%0A            if (obj instanceof LayoutInflater.Factory2) %7B%0A                return (LayoutInflater.Factory2) obj;%0A            %7D%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D catch (NoSuchFieldException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null;%0A    %7D
 %0A%0A  

remove reflections that used for getting private methods of layout inflator