package nz.net.ultraq.thymeleaf.internal;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.methodType;
import javax.annotation.Nonnull;
import nz.net.ultraq.thymeleaf.context.extensions.IContextExtensions;
import org.thymeleaf.context.IContext;
import org.thymeleaf.dialect.IProcessorDialect;

public class IContextDelegate {

    private static final MethodHandle GET_PREFIX_FOR_DIALECT;

    static {
        MethodHandle handle;
        MethodHandles.Lookup lookup = MethodHandles.publicLookup();
        try {
            handle = filterReturnValue(insertArguments(lookup.findStatic(Class.forName("org.codehaus.groovy.runtime.InvokerHelper"), "invokeMethod", methodType(Object.class, Object.class, String.class, Object.class)).asCollector(Object[].class, 1),
                    1, "getPrefixForDialect"),
                    lookup.findStatic(Class.forName("org.codehaus.groovy.runtime.typehandling.ShortTypeHandling"), "castToString", methodType(String.class, Object.class)));
        } catch (ReflectiveOperationException ex) {
            try {
                handle = lookup.findStatic(IContextExtensions.class, "getPrefixForDialect", methodType(String.class, IContext.class, Class.class));
            } catch (NoSuchMethodException | IllegalAccessException ex1) {
                throw new AssertionError(ex1);
            }
        }
        GET_PREFIX_FOR_DIALECT = handle;
    }

    public static String getPrefixForDialect(@Nonnull IContext self, Class<? extends IProcessorDialect> dialectClass) {
        try {
            return (String) GET_PREFIX_FOR_DIALECT.invoke(self, dialectClass);
        } catch (RuntimeException | Error ex) {
            throw ex;
        } catch (Throwable ex) {
            throw new java.lang.reflect.UndeclaredThrowableException(ex);
        }
    }

}

@@ -85,47 +85,8 @@
 ort 
-java.lang.invoke.MethodHandles;%0Aimport 
 stat
@@ -120,16 +120,62 @@
 andles.*
+;%0Aimport java.lang.invoke.MethodHandles.Lookup
 ;%0Aimport
@@ -510,31 +510,118 @@
 LECT
-;%0A%0A    static %7B%0A       
+ = init();%0A%0A    @SuppressWarnings(%7B%22UseSpecificCatch%22, %22BroadCatchBlock%22, %22TooBroadCatch%22%7D)%0A    private static
  Met
@@ -630,23 +630,24 @@
 dHandle 
-handle;
+init() %7B
 %0A       
@@ -647,30 +647,16 @@
         
-MethodHandles.
 Lookup l
@@ -667,29 +667,9 @@
 p = 
-MethodHandles.publicL
+l
 ooku
@@ -699,24 +699,44 @@
         
-handle =
+try %7B%0A                return
  filterR
@@ -976,16 +976,20 @@
         
+    
 1, %22getP
@@ -1027,16 +1027,20 @@
         
+   
+ 
 lookup.f
@@ -1184,32 +1184,36 @@
 ass)));%0A        
+    
 %7D catch (Reflect
@@ -1238,29 +1238,31 @@
 ion 
-ex) %7B%0A            try
+%7C LinkageError ignored)
  %7B%0A 
@@ -1280,16 +1280,14 @@
     
-handle =
+return
  loo
@@ -1423,66 +1423,38 @@
    %7D
- catch (NoSuchMethodException %7C IllegalAccessException
+%0A        %7D catch (Throwable
  ex
-1
 ) %7B%0A
@@ -1469,107 +1469,127 @@
     
-    throw new AssertionError(ex1);%0A            %7D%0A        %7D%0A        GET_PREFIX_FOR_DIALECT = handle;
+return dropArguments(throwException(String.class, Throwable.class).bindTo(ex), 0, Object.class, Class.class);%0A        %7D
 %0A   

Fix lookup