英文:
Unhandled Exception while using reflection in java
问题
我目前正在制作一个Minecraft模组,我需要将我的浮点数设置为一个基类的私有浮点数。他们在代码中硬编码了一个方法,我基本上是在这个方法的基础上进行扩展,但它使用了私有浮点数"rendererUpdateCount"。
我试图将我的浮点数"updateCount"的值始终设为"rendererUpdateCount"的值。
问题是我似乎无法通过反射获取浮点数的值。
目前我正在尝试像这样做:
Class er = EntityRenderer.class;
Field field = er.getDeclaredField("rendererUpdateCount");
唯一的问题是它抛出了Unhandled exception type NoSuchFieldException
,暗示着该浮点数并不存在。
那么我该如何正确地做这件事呢?
英文:
I am currently making a minecraft mod, and I need to set my float to a private float of a base class. They hardcode a method and I am basically extending on it, but it uses the PRIVATE float "rendererUpdateCount"
I am trying to set my float "updateCount" to always equal the value of "rendererUpdateCount"
The problem is I can't seem to get the float's value with reflection.
Currently I am trying to do something like this:
Class er = EntityRenderer.class;
Field field = er.getDeclaredField("rendererUpdateCount");
The only problem is it throws Unhandled exception type NoSuchFieldException
implying said float does not exist.
So how can I do this correctly?
答案1
得分: 0
每当我们遇到 Unhandled exception...
,这意味着它是一个 checked Exception
并且需要使用 try/catch 进行处理。
> Checked: 是在编译时被检查的异常。如果方法内的某些代码抛出了一个 checked 异常,那么该方法必须处理异常,或者可以使用 throws 关键字指定异常。
>
>
>
> Unchecked: 是在编译时不被检查的异常。
在 Java 异常
中,Error
和 RuntimeException
类是 unchecked exceptions
,其他 throwable 下的异常都是 checked
。
英文:
Whenever we got Unhandled exception...
, it means it's a checked Exception
& demands try/catch ().
> Checked: are the exceptions that are checked at compile time. If some
> code within a method throws a checked exception, then the method must
> either handle the exception or it must specify the exception using
> throws keyword.
>
>
>
> Unchecked are the exceptions that are not checked at compiled time.
In Java exceptions
under Error
and RuntimeException
classes are unchecked exceptions
, everything else under throwable is checked
答案2
得分: 0
在Java中,您必须处理已检查的异常,其中之一是 NoSuchFieldException
。 "处理" 可以是捕获该异常,或者声明抛出它的方法。我们选择选项1,您可以同时捕获父异常类 ReflectiveOperationException
,因为在编码完成后,您可能会从使用反射引发多个异常类型,而不仅仅是 NoSuchFieldException
。
尝试这样做:
try {
Class er = EntityRenderer.class;
Field field = er.getDeclaredField("rendererUpdateCount");
field.setAccessible(true); // 忽略/绕过其 "private" 属性
field.set(myInstance, myFloat);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
英文:
In java, you must handle checked exceptions, of which NoSuchFieldException
is one. “Handling” is either catching it or declaring the method it’s thrown from to throw it. Let’s go with option 1, and you might as well catch the parent exception class ReflectiveOperationException
because by the time you’re finished coding you’ll be potentially throwing a few more exception types from using reflection than NoSuchFieldException
.
Try this:
try {
Class er = EntityRenderer.class;
Field field = er.getDeclaredField("rendererUpdateCount");
field.setAccessible(true); // ignore/bypass it being “private”
field.set(myInstance, myFloat);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
专注分享java语言的经验与见解,让所有开发者获益!
评论