简体   繁体   English

如何使用Java反射访问私有数组?

[英]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.

相关问题 使用Java反射,我可以访问类型为private静态嵌套类的private字段吗? - Using Java reflection, can I access a private field of type private static nested class? 如何使用Java中的反射访问抽象类中的私有方法? - How access private methods in abstract class using reflection in Java? 使用Java反射API访问私有对象 - Access private object using java reflection api 如何在Java中使用反射获取枚举字段名称? - How can I get Enum fields names using reflection in Java? 如何使用反射(Java)调用私有静态方法? - How do I invoke a private static method using reflection (Java)? 使用反射在Java中获取静态私有哈希图 - Using reflection get a static private hashmap in java 我需要防止使用反射来从超类获取私有的Properties字段。 如何使用Java安全管理器执行此操作 - I need to prevent the use of reflection to get a private Properties field from a superclass. How can I use the Java security manager to do this 方法引发了“java.lang.IllegalArgumentException”异常。 如何使用 java 反射设置私有最终值 - Method threw 'java.lang.IllegalArgumentException' exception. how can I set private final value using java reflection Java反射无法访问动态更改的私有字段值 - Java reflection can't access dynamically changed private field values 使用Java反射访问类中的私有对象实例属性 - Access private object instance attribute in class using java reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM