Minecraft-SSHD/Minecraft-SSHD-Bukkit/src/main/java/com/ryanmichela/sshd/ReflectionUtil.java

120 lines
3.3 KiB
Java
Raw Normal View History

2013-11-14 07:17:51 +00:00
package com.ryanmichela.sshd;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* Copyright 2013 Ryan Michela
*/
public class ReflectionUtil {
2018-05-06 16:42:57 +00:00
2019-11-24 05:53:58 +00:00
public static void setProtectedValue(Object o, String field, Object newValue)
{
2013-11-14 07:17:51 +00:00
setProtectedValue(o.getClass(), o, field, newValue);
}
2019-11-24 05:53:58 +00:00
public static void setProtectedValue(Class c, String field, Object newValue)
{
2013-11-14 07:17:51 +00:00
setProtectedValue(c, null, field, newValue);
}
2019-11-24 05:53:58 +00:00
public static void setProtectedValue(Class c, Object o, String field, Object newValue)
{
try
{
2013-11-14 07:17:51 +00:00
Field f = c.getDeclaredField(field);
f.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
f.set(o, newValue);
2019-11-24 05:53:58 +00:00
}
catch (NoSuchFieldException | IllegalAccessException ex)
{
2013-11-14 07:17:51 +00:00
System.out.println("*** " + c.getName() + ":" + ex);
}
}
2019-11-24 05:53:58 +00:00
public static <T> T getProtectedValue(Object obj, String fieldName)
{
try
{
2013-11-14 07:17:51 +00:00
Class c = obj.getClass();
2019-11-24 05:53:58 +00:00
while (c != Object.class)
{
2013-11-14 07:17:51 +00:00
Field[] fields = c.getDeclaredFields();
2019-11-24 05:53:58 +00:00
for (Field f : fields)
{
if (f.getName() == fieldName)
{
2013-11-14 07:17:51 +00:00
f.setAccessible(true);
return (T) f.get(obj);
}
}
c = c.getSuperclass();
}
System.out.println("*** " + obj.getClass().getName() + ":No such field");
return null;
2019-11-24 05:53:58 +00:00
}
catch (Exception ex)
{
2013-11-14 07:17:51 +00:00
System.out.println("*** " + obj.getClass().getName() + ":" + ex);
return null;
}
}
2019-11-24 05:53:58 +00:00
public static <T> T getProtectedValue(Class c, String field)
{
try
{
2013-11-14 07:17:51 +00:00
Field f = c.getDeclaredField(field);
f.setAccessible(true);
return (T) f.get(c);
2019-11-24 05:53:58 +00:00
}
catch (Exception ex)
{
2013-11-14 07:17:51 +00:00
System.out.println("*** " + c.getName() + ":" + ex);
return null;
}
}
2019-11-24 05:53:58 +00:00
public static Object invokeProtectedMethod(Class c, String method, Object... args)
{
2013-11-14 07:17:51 +00:00
return invokeProtectedMethod(c, null, method, args);
}
2019-11-24 05:53:58 +00:00
public static Object invokeProtectedMethod(Object o, String method, Object... args)
{
2013-11-14 07:17:51 +00:00
return invokeProtectedMethod(o.getClass(), o, method, args);
}
2019-11-24 05:53:58 +00:00
public static Object invokeProtectedMethod(Class c, Object o, String method, Object... args)
{
try
{
2013-11-14 07:17:51 +00:00
Class[] pTypes = new Class[args.length];
2019-11-24 05:53:58 +00:00
for (int i = 0; i < args.length; i++)
{
if (args[i] instanceof Integer)
2013-11-14 07:17:51 +00:00
pTypes[i] = int.class;
2019-11-24 05:53:58 +00:00
else
2013-11-14 07:17:51 +00:00
pTypes[i] = args[i].getClass();
}
Method m = c.getDeclaredMethod(method, pTypes);
m.setAccessible(true);
return m.invoke(o, args);
2019-11-24 05:53:58 +00:00
}
catch (Exception ex)
{
2013-11-14 07:17:51 +00:00
System.out.println("*** " + c.getName() + "." + method + "(): " + ex);
return null;
}
}
}