package com.mindorks.placeholderview;

import com.mindorks.placeholderview.annotations.internal.BindingSuffix;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by janisharali on 24/02/18.
 */

public class Binding {

    private Binding() {
        // not to be instantiated in public
    }

    @SuppressWarnings("unchecked")
    private static <T, C> C getBinderInstance(T target, String suffix) {
        Class<?> targetClass = target.getClass();
        String className = targetClass.getName();
        try {
            Class<?> bindingClass = targetClass
                    .getClassLoader()
                    .loadClass(className + suffix);
            Constructor<?> classConstructor = bindingClass.getConstructor(targetClass);
            try {
                return (C) classConstructor.newInstance(target);
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Unable to invoke " + classConstructor, e);
            } catch (InstantiationException e) {
                throw new RuntimeException("Unable to invoke " + classConstructor, e);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                }
                if (cause instanceof Error) {
                    throw (Error) cause;
                }
                throw new RuntimeException("Unable to create instance.", cause);
            }
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Unable to find constructor for " + className, e);
        }
        return null;
    }

    protected static <T, V extends android.view.View> ViewBinder<T, V> bindViewResolver(T resolver) {
        return getBinderInstance(resolver, BindingSuffix.CLASS_VIEW_BINDER_SUFFIX);
    }

    protected static <T, V extends android.view.View> ExpandableViewBinder<T, V> bindExpandableViewResolver(T resolver) {
        return getBinderInstance(resolver, BindingSuffix.CLASS_EXPANDABLE_VIEW_BINDER_SUFFIX);
    }

    protected static <T,
            V extends SwipePlaceHolderView.FrameView,
            P extends SwipePlaceHolderView.SwipeOption,
            Q extends SwipeDecor> SwipeViewBinder<T, V, P, Q> bindSwipeViewResolver(T resolver) {
        return getBinderInstance(resolver, BindingSuffix.CLASS_SWIPE_VIEW_BINDER_SUFFIX);
    }

    protected static <T,
            V extends SwipePlaceHolderView.FrameView,
            P extends SwipeDirectionalView.SwipeDirectionalOption,
            Q extends SwipeDecor> SwipeDirectionalViewBinder<T, V, P, Q> bindSwipeDirectionalViewResolver(T resolver) {
        return getBinderInstance(resolver, BindingSuffix.CLASS_SWIPE_DIRECTIONAL_VIEW_BINDER_SUFFIX);
    }
}

@@ -1610,32 +1610,265 @@
 dException e) %7B%0A
+            throw new RuntimeException(%22Unable to find Class for %22 + className + %22%5Cn%22 +%0A                    %22PLEASE ADD %3E%3E annotationProcessor 'com.mindorks.android:placeholderview-compiler:%3CLATEST-VERSION%3E' %3C%3C in build.gradle%22, e);%0A
         %7D catch 
@@ -1887,32 +1887,32 @@
 dException e) %7B%0A
-
             thro
@@ -1981,24 +1981,24 @@
 ssName, e);%0A
+
         %7D%0A  
@@ -1999,29 +1999,8 @@
   %7D%0A
-        return null;%0A
     

Throw Exception When Compiler is not used