Skip to content
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

JDK 17: InaccessibleObjectException when deserialized class has java.time.Instant field #1996

Closed
Or4ng3 opened this issue Oct 18, 2021 · 3 comments
Labels

Comments

@Or4ng3
Copy link

Or4ng3 commented Oct 18, 2021

Hi.

I'm using gson 2.8.8 with jdk 17. This code:

import com.google.gson.Gson;
import org.junit.jupiter.api.Test;

import java.time.Instant;

class DTO {
	private Instant instant;
}

public class GsonTest {
	@Test
	public void test() {
		var gson = new Gson();
		var dto = gson.fromJson("{}", DTO.class);
	}
}

Fails with exception:

java.lang.reflect.InaccessibleObjectException: Unable to make field private final long java.time.Instant.seconds accessible: module java.base does not "opens java.time" to unnamed module @73035e27

	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
	at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
	at com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.Gson.fromJson(Gson.java:931)
	at com.google.gson.Gson.fromJson(Gson.java:897)
	at com.google.gson.Gson.fromJson(Gson.java:846)
	at com.google.gson.Gson.fromJson(Gson.java:817)
	at com.test.GsonTest.test(GsonTest.java:17)
@Or4ng3 Or4ng3 added the bug label Oct 18, 2021
@Marcono1234
Copy link
Collaborator

See #1979 (comment) or related issues. You are deserializing (maybe by accident) an internal field of a JDK class; you have to write a custom TypeAdapter or you could try third-party libraries such as https://github.com/gkopff/gson-javatime-serialisers adding support for java.time classes.

@Or4ng3
Copy link
Author

Or4ng3 commented Oct 18, 2021

Yes, the problem with class java.time.Instant (it's the only field in example). Also I see same problem with LocalDate and LocalTime classes.
However, I think that java.time package should be supported since its classes are newer and better than java.util.Date

@Marcono1234
Copy link
Collaborator

However, I think that java.time package should be supported since its classes are newer and better than java.util.Date

See #1059

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants