ForgePatch/src/main/java/net/minecraftforge/fml/UnsafeHacks.java

79 lines
2.3 KiB
Java

/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* 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 Software Foundation version 2.1
* of the License.
*
* 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 A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class UnsafeHacks
{
private static final Unsafe UNSAFE;
static {
try
{
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
UNSAFE = (Unsafe)theUnsafe.get(null);
}
catch (IllegalAccessException | NoSuchFieldException e)
{
throw new RuntimeException("BARF!", e);
}
}
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> packetClass)
{
try
{
return (T) UNSAFE.allocateInstance(packetClass);
}
catch (InstantiationException e)
{
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public static <T> T getField(Field field, Object object) {
final long l = UNSAFE.objectFieldOffset(field);
return (T) UNSAFE.getObject(object, l);
}
public static void setField(Field data, Object object, Object buffer)
{
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putObject(object, offset, buffer);
}
public static int getIntField(Field f, Object obj)
{
long offset = UNSAFE.objectFieldOffset(f);
return UNSAFE.getInt(obj, offset);
}
public static void setIntField(Field data, Object object, int value)
{
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putInt(object, offset, value);
}
}