PlotSquared/src/main/java/com/plotsquared/bukkit/util/ReflectionUtil.java

149 lines
4.9 KiB
Java
Raw Normal View History

2015-07-26 16:51:12 +02:00
package com.plotsquared.bukkit.util;
2015-06-08 12:30:46 +02:00
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
2015-07-30 16:25:16 +02:00
import org.bukkit.Bukkit;
2015-06-08 12:30:46 +02:00
/**
* Reflection Utilities for minecraft
*
*/
2015-09-13 06:04:31 +02:00
public class ReflectionUtil {
public static Class<?> getNmsClass(final String name) {
2015-06-08 12:30:46 +02:00
final String className = "net.minecraft.server." + getVersion() + "." + name;
return getClass(className);
}
2015-09-13 06:04:31 +02:00
public static Class<?> getCbClass(final String name) {
2015-06-08 12:30:46 +02:00
final String className = "org.bukkit.craftbukkit." + getVersion() + "." + name;
return getClass(className);
}
2015-09-13 06:04:31 +02:00
public static Class<?> getUtilClass(final String name) {
try {
2015-06-08 12:30:46 +02:00
return Class.forName(name); //Try before 1.8 first
2015-09-13 06:04:31 +02:00
} catch (final ClassNotFoundException ex) {
try {
2015-06-08 12:30:46 +02:00
return Class.forName("net.minecraft.util." + name); //Not 1.8
2015-09-13 06:04:31 +02:00
} catch (final ClassNotFoundException ex2) {
2015-06-08 12:30:46 +02:00
return null;
}
}
}
2015-09-13 06:04:31 +02:00
public static String getVersion() {
2015-06-08 12:30:46 +02:00
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
return packageName.substring(packageName.lastIndexOf('.') + 1);
}
2015-09-13 06:04:31 +02:00
public static Object getHandle(final Object wrapper) {
2015-06-08 12:30:46 +02:00
final Method getHandle = makeMethod(wrapper.getClass(), "getHandle");
return callMethod(getHandle, wrapper);
}
2015-09-13 06:04:31 +02:00
2015-06-08 12:30:46 +02:00
//Utils
2015-09-13 06:04:31 +02:00
public static Method makeMethod(final Class<?> clazz, final String methodName, final Class<?>... paramaters) {
try {
2015-06-08 12:30:46 +02:00
return clazz.getDeclaredMethod(methodName, paramaters);
2015-09-13 06:04:31 +02:00
} catch (final NoSuchMethodException ex) {
2015-06-08 12:30:46 +02:00
return null;
2015-09-13 06:04:31 +02:00
} catch (final Exception ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-13 06:04:31 +02:00
2015-06-08 12:30:46 +02:00
@SuppressWarnings("unchecked")
2015-09-13 06:04:31 +02:00
public static <T> T callMethod(final Method method, final Object instance, final Object... paramaters) {
if (method == null) {
throw new RuntimeException("No such method");
}
2015-06-08 12:30:46 +02:00
method.setAccessible(true);
2015-09-13 06:04:31 +02:00
try {
2015-06-08 12:30:46 +02:00
return (T) method.invoke(instance, paramaters);
2015-09-13 06:04:31 +02:00
} catch (final InvocationTargetException ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex.getCause());
2015-09-13 06:04:31 +02:00
} catch (final Exception ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-13 06:04:31 +02:00
2015-06-08 12:30:46 +02:00
@SuppressWarnings("unchecked")
2015-09-13 06:04:31 +02:00
public static <T> Constructor<T> makeConstructor(final Class<?> clazz, final Class<?>... paramaterTypes) {
try {
2015-06-08 12:30:46 +02:00
return (Constructor<T>) clazz.getConstructor(paramaterTypes);
2015-09-13 06:04:31 +02:00
} catch (final NoSuchMethodException ex) {
2015-06-08 12:30:46 +02:00
return null;
2015-09-13 06:04:31 +02:00
} catch (final Exception ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-13 06:04:31 +02:00
public static <T> T callConstructor(final Constructor<T> constructor, final Object... paramaters) {
if (constructor == null) {
throw new RuntimeException("No such constructor");
}
2015-06-08 12:30:46 +02:00
constructor.setAccessible(true);
2015-09-13 06:04:31 +02:00
try {
2015-06-08 12:30:46 +02:00
return constructor.newInstance(paramaters);
2015-09-13 06:04:31 +02:00
} catch (final InvocationTargetException ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex.getCause());
2015-09-13 06:04:31 +02:00
} catch (final Exception ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-13 06:04:31 +02:00
public static Field makeField(final Class<?> clazz, final String name) {
try {
2015-06-08 12:30:46 +02:00
return clazz.getDeclaredField(name);
2015-09-13 06:04:31 +02:00
} catch (final NoSuchFieldException ex) {
2015-06-08 12:30:46 +02:00
return null;
2015-09-13 06:04:31 +02:00
} catch (final Exception ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-13 06:04:31 +02:00
2015-06-08 12:30:46 +02:00
@SuppressWarnings("unchecked")
2015-09-13 06:04:31 +02:00
public static <T> T getField(final Field field, final Object instance) {
if (field == null) {
throw new RuntimeException("No such field");
}
2015-06-08 12:30:46 +02:00
field.setAccessible(true);
2015-09-13 06:04:31 +02:00
try {
2015-06-08 12:30:46 +02:00
return (T) field.get(instance);
2015-09-13 06:04:31 +02:00
} catch (final Exception ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-13 06:04:31 +02:00
public static void setField(final Field field, final Object instance, final Object value) {
if (field == null) {
throw new RuntimeException("No such field");
}
2015-06-08 12:30:46 +02:00
field.setAccessible(true);
2015-09-13 06:04:31 +02:00
try {
2015-06-08 12:30:46 +02:00
field.set(instance, value);
2015-09-13 06:04:31 +02:00
} catch (final Exception ex) {
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-13 06:04:31 +02:00
public static Class<?> getClass(final String name) {
try {
2015-06-08 12:30:46 +02:00
return Class.forName(name);
2015-09-13 06:04:31 +02:00
} catch (final ClassNotFoundException ex) {
2015-06-08 12:30:46 +02:00
return null;
}
}
2015-09-13 06:04:31 +02:00
public static <T> Class<? extends T> getClass(final String name, final Class<T> superClass) {
try {
2015-06-08 12:30:46 +02:00
return Class.forName(name).asSubclass(superClass);
2015-09-13 06:04:31 +02:00
} catch (ClassCastException | ClassNotFoundException ex) {
2015-06-08 12:30:46 +02:00
return null;
}
}
}