[英]How can I get access to a private array using Java reflection?
Ok I was trying to figure this out on my own but I am kinda stumped. 好的,我试图自己解决这个问题,但是我有点沮丧。 I am trying to fix some code errors in a mod for minecraft. 我正在尝试在Minecraft的Mod中修复一些代码错误。 I got a lot of the fixed except for this one. 除了这一点,我得到了很多修复。
Basically this guys mod wants to gain access to an array in the superclass which it extends called field_94586_c
. 基本上,这些家伙mod希望获得对其扩展的超类(称为field_94586_c
数组的访问。 When the code was written that variable was public scoped. 编写代码时,该变量是公共范围的。 Now in the later versions it has been changed to a private scope. 现在,在更高版本中,它已更改为私有范围。 I was playing around with Java reflection to try and gain access to the variable so I can write the needed data to it. 我正在尝试使用Java反射来尝试访问该变量,以便可以向其中写入所需的数据。
Here the code snippet that applies... 这里适用的代码段...
@Override
@SideOnly(Side.CLIENT)
/**
* Gets an icon index based on an item's damage value
*/
public Icon getIconFromDamage(int par1)
{
if (par1 < 0 || par1 >= skullTypes.length) {
par1 = 0;
}
return field_94586_c[par1];
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister par1IconRegister)
{
field_94586_c = new Icon[field_94587_a.length];
for (int i = 0; i < field_94587_a.length; ++i)
if (i >= 5)
field_94586_c[i] = par1IconRegister.registerIcon("iguanatweakstconstruct:" + field_94587_a[i]);
else
field_94586_c[i] = par1IconRegister.registerIcon(getIconString() + "_" + field_94587_a[i]);
}
Basically I need to write a modified version of the array to the superclass. 基本上,我需要将数组的修改版本写入超类。 He adds a few new items to the array. 他向阵列添加了一些新项目。
My original attempt was to use a "reflector" class to return an object to me. 我最初的尝试是使用“反射器”类向我返回一个对象。 But the issue I cannot figure out how to turn the Object into a Icon[]
. 但是我无法弄清楚如何将Object变成Icon[]
。 I tried casting it but Java complains that is not possible. 我尝试过转换,但是Java抱怨这是不可能的。 I don't have the original error. 我没有原始错误。
Here is my reflector class... 这是我的反射镜课...
package iguanaman.iguanatweakstconstruct.util;
import java.lang.reflect.Field;
/**
* Created by Ian on 4/10/2014.
*/
public class IguanaReflector {
public static Object modifyAccess(final String className, final String filedName) throws SecurityException, NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException {
final Field field = Class.forName(className).getDeclaredField(filedName);
field.setAccessible(true);
return field.get(Class.forName(className));
}
} }
Its not much and to be honest its based on some looking around on the net. 它的数量不多,说实话,它基于网上的一些情况。
The full package name I am trying to access is net.minecraft.item.ItemSkull
and the field is field_94586_c
. 我要访问的完整软件包名称是net.minecraft.item.ItemSkull
,字段是field_94586_c
。
Any help would be very appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!
An instance field is attached to an instance. 实例字段附加到实例。 It doesn't exist on its own. 它本身并不存在。
The Field#get(Object)
method javadoc states Field#get(Object)
方法javadoc指出
Returns the value of the field represented by this Field, on the specified object. 返回指定对象上此Field表示的字段的值。 The value is automatically wrapped in an object if it has a primitive type. 如果值具有原始类型,则该值将自动包装在一个对象中。
In other words, you need to call get
by passing the object (the instance) on which to get the field value (or null
if the field is static
). 换句话说,您需要通过传递要在其上获取字段值的对象(实例)来调用get
(如果字段为static
则为null
)。 You're passing the Class
object for the class that the field is declared in. 您正在为声明该字段的类传递Class
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.