Skip to content

Instantly share code, notes, and snippets.

@hishidama
Created September 26, 2017 05:49
Show Gist options
  • Save hishidama/17b7d6673a7f07ffa3771ba75f937c2f to your computer and use it in GitHub Desktop.
Save hishidama/17b7d6673a7f07ffa3771ba75f937c2f to your computer and use it in GitHub Desktop.
getなのに、なぜエラーメッセージが「set」なのか?
package example;
import java.lang.reflect.Field;
public class FieldExample {
public static void main(String[] args) throws Exception {
Field field = Integer.class.getDeclaredField("value");
field.setAccessible(true);
field.getInt("");
}
}
Exception in thread "main" java.lang.IllegalArgumentException: Can not set final int field java.lang.Integer.value to java.lang.String
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.getInt(UnsafeQualifiedIntegerFieldAccessorImpl.java:58)
at java.lang.reflect.Field.getInt(Field.java:574)
at example.FieldExample.main(FieldExample.java:10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment