/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.logging.log4j;

import org.apache.logging.log4j.core.Timer;
import org.apache.logging.log4j.core.helpers.Loader;
import org.junit.BeforeClass;
import org.junit.Test;
import sun.reflect.Reflection;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Tests the cost of invoking Reflection.getCallerClass via reflection vs calling it directly.
 */
public class ReflectionComparison {

    private static Method getCallerClass;

    private static final int COUNT = 1000000;

    @BeforeClass
    public static void setupCallerCheck() {
        try {
            ClassLoader loader = Loader.getClassLoader();
            Class clazz = loader.loadClass("sun.reflect.Reflection");
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                int modifier = method.getModifiers();
                if (method.getName().equals("getCallerClass") && Modifier.isStatic(modifier)) {
                    getCallerClass = method;
                    break;
                }
            }
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
            throw  new RuntimeException(cnfe);
        }
    }

    @Test
    public void test1() {
        Timer timer = new Timer("Reflection", COUNT);
        timer.start();
        Object[] arr = new Object[1];
        arr[0] = 3;
        for (int i= 0; i < COUNT; ++i) {
            getCallerClass(arr);
        }
        timer.stop();
        System.out.println(timer.toString());
    }


    @Test
    public void test2() {
        Timer timer = new Timer("Reflection", COUNT);
        timer.start();
        for (int i= 0; i < COUNT; ++i) {

            Reflection.getCallerClass(3);
        }
        timer.stop();
        System.out.println(timer.toString());
    }

    private Class getCallerClass(Object[] array) {
        if (getCallerClass != null) {
            try {
                /*Object[] params = new Object[]{index}; */
                return (Class) getCallerClass.invoke(null, array);
            } catch (Exception ex) {
                fail(ex.getMessage());
                // logger.debug("Unable to determine caller class via Sun Reflection", ex);
            }
        }
        return null;
    }

}

@@ -922,24 +922,192 @@
 ers.Loader;%0A
+import org.apache.logging.log4j.message.Message;%0Aimport org.apache.logging.log4j.message.SimpleMessage;%0Aimport org.apache.logging.log4j.message.StringFormattedMessage;%0A
 import org.j
@@ -1124,16 +1124,16 @@
 eClass;%0A
-
 import o
@@ -1179,16 +1179,54 @@
 ction;%0A%0A
+import java.lang.reflect.Constructor;%0A
 import j
@@ -1597,16 +1597,101 @@
 00000;%0A%0A
+    private static Class%5B%5D paramTypes = new Class%5B%5D %7BString.class, Object%5B%5D.class%7D;%0A%0A
     @Bef
@@ -2991,24 +2991,690 @@
 ());%0A    %7D%0A%0A
+%0A    @Test%0A    public void createObjects() throws Exception %7B%0A        Timer timer = new Timer(%22NewObject%22, COUNT);%0A        timer.start();%0A        Message msg;%0A        for (int i = 0; i %3C COUNT; ++i) %7B%0A            msg = new StringFormattedMessage(%22Hello %251%22, i);%0A        %7D%0A        timer.stop();%0A        System.out.println(timer.toString());%0A        Class%3C? extends Message%3E clazz = StringFormattedMessage.class;%0A%0A        timer = new Timer(%22ReflectionObject%22, COUNT);%0A        timer.start();%0A%0A        for (int i = 0; i %3C COUNT; ++i) %7B%0A            createMessage(clazz, %22Hello %251%22, i);%0A        %7D%0A        timer.stop();%0A        System.out.println(timer.toString());%0A    %7D%0A%0A
     private 
@@ -4079,32 +4079,32 @@
     %7D%0A        %7D%0A
-
         return n
@@ -4115,10 +4115,263 @@
 %0A    %7D%0A%0A
+    private Message createMessage(Class%3C? extends Message%3E clazz, String msg, Object... params) throws Exception %7B%0A        Constructor constructor = clazz.getConstructor(paramTypes);%0A        return (Message) constructor.newInstance(msg, params);%0A    %7D%0A%0A
 %7D%0A

Add object creation test