Field::setLong 与类中的 Long 和 long 字段的行为不同
我的代码有一个非常奇怪的问题。 当我运行它时,我得到异常:
线程“main”中出现异常 java.lang.IllegalArgumentException:无法将 java.lang.Long 字段 Lesson12.TestReflectionRepository.Main$TestSetLong.LongField 设置为 (long)23132 在java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) 在java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:195) 在 java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.setLong(UnsafeObjectFieldAccessorImpl.java:120) 在 java.base/java.lang.reflect.Field.setLong(Field.java:1021) 在 Lesson12.TestReflectionRepository.Main.main(Main.java:15)
我不明白这是什么意思。如果我对变量 l
使用类型 Long
,我也会在同一行得到相同的异常。我认为,这取决于类中使用的类型,long
或Long
。但我认为,事情不应该这样。 为什么会发生这种情况?我有什么错吗?
public class Main {
public static class TestSetLong {
public Long LongField;
public long longField;
}
public static void main(String[] args) throws Exception {
TestSetLong obj = new TestSetLong();
Class cobj = obj.getClass();
Field longField = cobj.getField("longField"), LongField = cobj.getField("LongField");
long l = 23132L;//if I use Long I also get this exception on the same line
longField.setLong(obj, l);
LongField.setLong(obj, l);
}
}
我使用OpenJDK 11.0.12+7-b1504.40 amd64,在Intellij IDEA 2021.2.3中运行它。
I have one very strange problem with my code.
When I run it, I get exception:
Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.Long field lesson12.TestReflectionRepository.Main$TestSetLong.LongField to (long)23132
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:195)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.setLong(UnsafeObjectFieldAccessorImpl.java:120)
at java.base/java.lang.reflect.Field.setLong(Field.java:1021)
at lesson12.TestReflectionRepository.Main.main(Main.java:15)
I don't understand what it means. If I use type Long
for the variable l
I also get the same exception on the same line. I think, it depends on what type is used in the class, long
or Long
. But I think, it shouldn't work like this.
Why does it happen? What's wrong I do?
public class Main {
public static class TestSetLong {
public Long LongField;
public long longField;
}
public static void main(String[] args) throws Exception {
TestSetLong obj = new TestSetLong();
Class cobj = obj.getClass();
Field longField = cobj.getField("longField"), LongField = cobj.getField("LongField");
long l = 23132L;//if I use Long I also get this exception on the same line
longField.setLong(obj, l);
LongField.setLong(obj, l);
}
}
I use OpenJDK 11.0.12+7-b1504.40 amd64, run it in Intellij IDEA 2021.2.3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

根据 JavaDoc for
Field
您可以:Field.set(Object, Object)
设置引用字段和原始字段Field.setLong(Object, long)
仅用于设置(原始)类型long
的字段请注意,此描述提到了 set(Object, Object) 中发生的自动展开 - 但它没有给出可能允许自动包装的提示。
According to the JavaDoc for
Field
you can:Field.set(Object, Object)
to set reference field and primitive fieldsField.setLong(Object, long)
only to set fields of (primitive) typelong
Note that this description mentions the automatic unwrapping that occurs in
set(Object, Object)
- but it nowhere gives a hint that automatic wrapping might be allowed.