-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Add GsonBuilder.disableJdkUnsafe()
#1904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GsonBuilder.disableJdkUnsafe()
#1904
Conversation
3f67886 to
802d85f
Compare
21d0f85 to
2b70a54
Compare
2b70a54 to
bb5adf3
Compare
|
Maybe in the future it should also be considered to make usage of |
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is great! I think using Unsafe.allocateInstance is kind of horrifying, and it certainly shouldn't be the default behaviour. Unfortunately, it is, and changing that would probably break too many things. Making it at least possible to avoid Unsafe is probably the best we can do.
| * runtimes. For example Android does not provide {@code Unsafe}, or only with limited | ||
| * functionality. Additionally {@code Unsafe} creates instances without executing any | ||
| * constructor or initializer block, or performing initialization of field values. This can | ||
| * lead to surprising and difficult to debug errors.<br> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you start a new paragraph on the next line rather than <br>? Or just leave out <br>.
| if (useJdkUnsafe) { | ||
| return new ObjectConstructor<T>() { | ||
| private final UnsafeAllocator unsafeAllocator = UnsafeAllocator.create(); | ||
| @SuppressWarnings("unchecked") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you are here, could you reduce the scope of this?
@Override public T construct() {
...
@SuppressWarnings("unchecked")
T newInstance = (T) unsafeAllocator.newInstance(rawType);
return newInstance;
...
|
|
||
| public void testDisableJdkUnsafe() { | ||
| Gson gson = new GsonBuilder() | ||
| .disableJdkUnsafe() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nitpicky comment here, but these continuation lines should be indented +4 according to Google Java style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, such comments are fine
Resolves #445
Resolves partially #1387
Resolves partially #1695; should still be kept open because usage of
Unsafeshould be better documentedNo test is added to verify that by default
Unsafeis used because it appears 38 tests already depend on this behavior and would fail that was not the case.Note that for Android this actually disables more than just
sun.misc.Unsafeusage, because there Gson also tries internalObjectStreamClassandObjectInputStreammethods, but I don't think this is worth mentioning.