Field::setLong 与类中的 Long 和 long 字段的行为不同

发布于 01-16 06:05 字数 1482 浏览 3 评论 0原文

我的代码有一个非常奇怪的问题。 当我运行它时,我得到异常:

线程“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 ,我也会在同一行得到相同的异常。我认为,这取决于类中使用的类型,longLong。但我认为,事情不应该这样。 为什么会发生这种情况?我有什么错吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空气里的味道2025-01-23 06:05:48

根据 JavaDoc for Field 您可以:

如果基础字段属于基本类型,则尝试展开转换以将新值转换为基本类型的值。如果此尝试失败,该方法将抛出 IllegalArgumentException。

将指定对象上的字段值设置为long。此方法相当于 set(obj, lObj),其中 lObj 是一个 Long 对象,并且 lObj.longValue() == l。

请注意,此描述提到了 set(Object, Object) 中发生的自动展开 - 但它没有给出可能允许自动包装的提示。

According to the JavaDoc for Field you can:

If the underlying field is of a primitive type, an unwrapping conversion is attempted to convert the new value to a value of a primitive type. If this attempt fails, the method throws an IllegalArgumentException.

Sets the value of a field as a long on the specified object. This method is equivalent to set(obj, lObj), where lObj is a Long object and lObj.longValue() == l.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文