    001/*
    002 * The FML Forge Mod Loader suite. Copyright (C) 2012 cpw
    003 *
    004 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
    005 * Software Foundation; either version 2.1 of the License, or any later version.
    006 *
    007 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    008 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    009 *
    010 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
    011 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    012 */
    013package cpw.mods.fml.common;
    014
    015import java.util.Arrays;
    016import java.util.logging.Level;
    017
    018import cpw.mods.fml.relauncher.ReflectionHelper;
    019import cpw.mods.fml.relauncher.ReflectionHelper.UnableToAccessFieldException;
    020import cpw.mods.fml.relauncher.ReflectionHelper.UnableToFindFieldException;
    021
    022/**
    023 * Some reflection helper code.
    024 *
    025 * @author cpw
    026 *
    027 */
    028public class ObfuscationReflectionHelper
    029{
    030    public static boolean obfuscation;
    031
    032    @SuppressWarnings("unchecked")
    033    public static <T, E> T getPrivateValue(Class<? super E> classToAccess, E instance, int fieldIndex)
    034    {
    035        try
    036        {
    037            return ReflectionHelper.getPrivateValue(classToAccess, instance, fieldIndex);
    038        }
    039        catch (UnableToAccessFieldException e)
    040        {
    041            FMLLog.log(Level.SEVERE, e, "There was a problem getting field index %d from %s", fieldIndex, classToAccess.getName());
    042            throw e;
    043        }
    044    }
    045
    046    @SuppressWarnings("unchecked")
    047    public static <T, E> T getPrivateValue(Class<? super E> classToAccess, E instance, String... fieldNames)
    048    {
    049        try
    050        {
    051            return ReflectionHelper.getPrivateValue(classToAccess, instance, fieldNames);
    052        }
    053        catch (UnableToFindFieldException e)
    054        {
    055            FMLLog.log(Level.SEVERE,e,"Unable to locate any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
    056            throw e;
    057        }
    058        catch (UnableToAccessFieldException e)
    059        {
    060            FMLLog.log(Level.SEVERE, e, "Unable to access any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
    061            throw e;
    062        }
    063    }
    064
    065    @Deprecated
    066    public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, int fieldIndex, E value)
    067    {
    068        setPrivateValue(classToAccess, instance, value, fieldIndex);
    069    }
    070
    071    public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, int fieldIndex)
    072    {
    073        try
    074        {
    075            ReflectionHelper.setPrivateValue(classToAccess, instance, value, fieldIndex);
    076        }
    077        catch (UnableToAccessFieldException e)
    078        {
    079            FMLLog.log(Level.SEVERE, e, "There was a problem setting field index %d on type %s", fieldIndex, classToAccess.getName());
    080            throw e;
    081        }
    082    }
    083
    084    @Deprecated
    085    public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, String fieldName, E value)
    086    {
    087        setPrivateValue(classToAccess, instance, value, fieldName);
    088    }
    089
    090    public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, String... fieldNames)
    091    {
    092        try
    093        {
    094            ReflectionHelper.setPrivateValue(classToAccess, instance, value, fieldNames);
    095        }
    096        catch (UnableToFindFieldException e)
    097        {
    098            FMLLog.log(Level.SEVERE, e, "Unable to locate any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
    099            throw e;
    100        }
    101        catch (UnableToAccessFieldException e)
    102        {
    103            FMLLog.log(Level.SEVERE, e, "Unable to set any field %s on type %s", Arrays.toString(fieldNames), classToAccess.getName());
    104            throw e;
    105        }
    106    }
    107
    108    /**
    109     *
    110     */
    111    public static void detectObfuscation(Class<?> clazz)
    112    {
    113        obfuscation = !clazz.getSimpleName().equals("World");
    114    }
    115
    116}