package org.technozor.jzon;


import org.technozor.jzon.internal.processor.JzonConstants;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import static java.lang.invoke.MethodType.*;

/**
 * Created by slim on 4/24/14.
 */
public class JzonWriterFactory {


    public static <T> Writer<T> writer(Class<T> clazz) {

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        MethodHandles.Lookup lookup = MethodHandles.lookup();

        try {
            String className = clazz.getCanonicalName() + JzonConstants.MAPPER_CLASS_SUFFIX;
            Class<Writer<@Jzon T>> mapperImpl  = (Class<Writer<@Jzon T>>) classLoader.loadClass(className);
            MethodHandle constructor = lookup.findConstructor(mapperImpl, methodType(Void.class));
            return (Writer<T>) constructor.invoke();
        }  catch (Throwable throwable) {
            toRuntimeException(throwable);
        }
        return null;
    }
    @SuppressWarnings("unchecked")
    private static <T extends Throwable> void toRuntimeException(Throwable throwable) throws T{
        throw (T) throwable;
    }

}

@@ -426,71 +426,8 @@
 );%0A%0A
-        MethodHandles.Lookup lookup = MethodHandles.lookup();%0A%0A
     
@@ -680,14 +680,30 @@
 r = 
+MethodHandles.
 lookup
+()
 .fin
@@ -857,16 +857,22 @@
         
+return
  toRunti
@@ -909,29 +909,8 @@
   %7D%0A
-        return null;%0A
     
@@ -991,12 +991,9 @@
 le%3E 
-void
+T
  toR

add params