/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.registry;

import org.spongepowered.common.SpongeImpl;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.function.Function;

public class RegistryHelper {

    public static boolean mapFields(Class<?> apiClass, Map<String, ?> mapping) {
        return mapFields(apiClass, fieldName -> mapping.get(fieldName.toLowerCase()));
    }

    public static boolean mapFields(Class<?> apiClass, Function<String, ?> mapFunction) {
        boolean mappingSuccess = true;
        for (Field f : apiClass.getDeclaredFields()) {
            try {
                Object value = mapFunction.apply(f.getName());
                if (value == null) {
                    SpongeImpl.getLogger().warn("Skipping {}.{}", f.getDeclaringClass().getName(), f.getName());
                    continue;
                }
                f.set(null, value);
            } catch (Exception e) {
                SpongeImpl.getLogger().error("Error while mapping {}.{}", f.getDeclaringClass().getName(), f.getName(), e);
                mappingSuccess = false;
            }
        }
        return mappingSuccess;
    }

    public static boolean setFactory(Class<?> apiClass, Object factory) {
        try {
            apiClass.getDeclaredField("factory").set(null, factory);
            return true;
        } catch (Exception e) {
            SpongeImpl.getLogger().error("Error while setting factory on {}", apiClass, e);
            return false;
        }
    }

    public static void setFinalStatic(Class<?> clazz, String fieldName, Object newValue) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            Field modifiers = field.getClass().getDeclaredField("modifiers");
            modifiers.setAccessible(true);
            modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, newValue);
        } catch (Exception e) {
            SpongeImpl.getLogger().error("Error while setting field {}.{}", clazz.getName(), fieldName, e);
        }
    }
}

@@ -1285,16 +1285,100 @@
 istry;%0A%0A
+import org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A
 import o
@@ -1413,16 +1413,16 @@
 geImpl;%0A
-
 %0Aimport 
@@ -1571,16 +1571,323 @@
 lper %7B%0A%0A
+    private static final Logger logger;%0A%0A    static %7B%0A        Logger l;%0A        try %7B%0A            l = SpongeImpl.getLogger();%0A        %7D catch (ExceptionInInitializerError %7C IllegalStateException e) %7B%0A            l = LogManager.getLogger(%22Sponge%22); // Running test suite%0A        %7D%0A        logger = l;%0A    %7D%0A%0A
     publ
@@ -2375,38 +2375,22 @@
         
-SpongeImpl.getL
+l
 ogger
-()
 .warn(%22S
@@ -2588,38 +2588,22 @@
         
-SpongeImpl.getL
+l
 ogger
-()
 .error(%22
@@ -3008,38 +3008,22 @@
         
-SpongeImpl.getL
+l
 ogger
-()
 .error(%22
@@ -3601,38 +3601,22 @@
         
-SpongeImpl.getL
+l
 ogger
-()
 .error(%22

Fix logger not being available when in test suite