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> t) {

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

        MethodHandles.Lookup lookup = MethodHandles.lookup();

        try {
            String s = t.getCanonicalName() + JzonConstants.MAPPER_CLASS_SUFFIX;
            Class<Writer<@Jzon T>> mapperImpl  = (Class<Writer<@Jzon T>>) classLoader.loadClass(s);
            MethodHandle constructor = lookup.findConstructor(mapperImpl, methodType(Void.class));
            return (Writer<T>) constructor.invoke();
        }  catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

}

@@ -329,17 +329,21 @@
 lass%3CT%3E 
-t
+clazz
 ) %7B%0A%0A   
@@ -522,13 +522,25 @@
 ing 
-s = t
+className = clazz
 .get
@@ -688,17 +688,25 @@
 adClass(
-s
+className
 );%0A     
@@ -910,33 +910,35 @@
    t
-hrowable.printStackTrace(
+oRuntimeException(throwable
 );%0A 
@@ -972,12 +972,178 @@
 l;%0A    %7D
+%0A    @SuppressWarnings(%22unchecked%22)%0A    private static %3CT extends Throwable%3E void toRuntimeException(Throwable throwable) throws T%7B%0A        throw (T) throwable;%0A    %7D
 %0A%0A%7D%0A

add params