/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jdbi.v3.core.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class AnnotationFactory {
    private AnnotationFactory() {}

    public static <T extends Annotation> T create(Class<T> annotationType) {
        @SuppressWarnings("unchecked")
        T annotation = (T) Proxy.newProxyInstance(
            Thread.currentThread().getContextClassLoader(),
            new Class[]{annotationType},
            getInvocationHandler(annotationType));
        return annotation;
    }

    private static <T extends Annotation> InvocationHandler getInvocationHandler(Class<T> annotationType) {
        return (proxy, method, args) -> {
            String name = method.getName();
            if ("annotationType".equals(name) && method.getParameterCount() == 0) {
                return annotationType;
            }

            if ("equals".equals(name) && method.getParameterCount() == 1
                && Object.class.equals(method.getParameterTypes()[0])) {
                Annotation that = (Annotation) args[0];
                return annotationType.equals(that.annotationType());
            }

            if ("hashCode".equals(name) && method.getParameterCount() == 0) {
                return 0;
            }

            if ("toString".equals(name) && method.getParameterCount() == 0) {
                return "@" + annotationType.getCanonicalName() + "()";
            }

            throw new IllegalStateException("Unknown method " + method + " for annotation type " + annotationType);
        };
    }
}

@@ -862,101 +862,33 @@
     
-@SuppressWarnings(%22unchecked%22)%0A        T annotation = (T) Proxy.newProxyInstance(%0A           
+ClassLoader classLoader =
  Thr
@@ -930,17 +930,17 @@
 Loader()
-,
+;
 %0A       
@@ -944,23 +944,29 @@
     
-    new Class%5B%5D
+Class%5B%5D interfaces = 
 %7Bann
@@ -977,17 +977,17 @@
 ionType%7D
-,
+;
 %0A       
@@ -991,51 +991,220 @@
     
-    getInvocationHandler(annotationType)
+InvocationHandler invocationHandler = getInvocationHandler(annotationType);%0A%0A        @SuppressWarnings(%22unchecked%22)%0A        T annotation = (T) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler
 );%0A
+%0A
     

Refactor AnnotationFactory.create()