package com.soundcloud.android.lightcycle;

import android.util.Log;

import java.lang.reflect.Method;

public final class LightCycleBinder {
    private static final String TAG = LightCycleBinder.class.getSimpleName();
    private static final String ANDROID_PREFIX = "android.";
    private static final String JAVA_PREFIX = "java.";

    public static void bind(LightCycleDispatcher<?> target) {
        Method bindingMethod;
        try {
            bindingMethod = findViewBinderForClass(target.getClass());
            if (bindingMethod != null) {
                bindingMethod.invoke(null, target);
            }
        } catch (Exception e) {
            // no binder found, so ignore.
        }
    }

    private static String getInjectorClassName(String clsName) {
        return clsName + "$LightCycleBinder";
    }

    private static Method findViewBinderForClass(Class<?> cls) throws IllegalAccessException, InstantiationException, NoSuchMethodException {
        Method lightCycleInjectionMethod;
        String clsName = cls.getName();
        if (clsName.startsWith(ANDROID_PREFIX) || clsName.startsWith(JAVA_PREFIX)) {
            Log.d(TAG, "MISS: Reached framework class. Abandoning search.");
            return null;
        }
        try {
            Class<?> binder = Class.forName(getInjectorClassName(clsName));
            lightCycleInjectionMethod = binder.getMethod("bind", cls);
            Log.d(TAG, "HIT: Loaded LightCycle binder class.");
        } catch (ClassNotFoundException e) {
            Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName());
            lightCycleInjectionMethod = findViewBinderForClass(cls.getSuperclass());
        }
        return lightCycleInjectionMethod;
    }
}

@@ -460,36 +460,32 @@
 ingMethod = find
-View
 BinderForClass(t
@@ -846,28 +846,24 @@
  Method find
-View
 BinderForCla
@@ -878,16 +878,28 @@
 %3C?%3E cls)
+%0A           
  throws 
@@ -1656,12 +1656,8 @@
 find
-View
 Bind

Fix confusing method naming