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 15: Illegal reflective access to java.time classes #1807

Closed
EldarAgalarov opened this issue Oct 19, 2020 · 5 comments
Closed

JDK 15: Illegal reflective access to java.time classes #1807

EldarAgalarov opened this issue Oct 19, 2020 · 5 comments

Comments

@EldarAgalarov
Copy link

JDK 15 produces many Illegal reflective access warnings like following on serializing LocalTime, LocalDateTime, YearMonth, MonthYear and so on:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.gson.internal.reflect.UnsafeReflectionAccessor (file:/home/eldar/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.6/9180733b7df8542621dc12e21e87557e8c99b8cb/gson-2.8.6.jar) to field java.time.MonthDay.month
WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.reflect.UnsafeReflectionAccessor
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

Code example

Gson gson = new GsonBuilder().create();
System.out.println(gson.toJson(LocalDate.now()));
System.out.println(gson.toJson(LocalDateTime.now()));
System.out.println(gson.toJson(YearMonth.now()));
System.out.println(gson.toJson(MonthDay.now()));
@Marcono1234
Copy link
Collaborator

Same as #1059. Gson currently has no built-in support for classes added in Java 7+. It therefore falls back to the reflection based approach for serializing these classes, which is likely not what you want and also makes your code dependent on the implementation details of the JDK (which is why you are seeing these warnings).

So you would have to write your own TypeAdapters to get support for these classes or use libraries which add support for them. https://github.com/gkopff/gson-javatime-serialisers might be helpful though I have not used it yet.

@EldarAgalarov
Copy link
Author

Same as #1059. Gson currently has no built-in support for classes added in Java 7+. It therefore falls back to the reflection based approach for serializing these classes, which is likely not what you want and also makes your code dependent on the implementation details of the JDK (which is why you are seeing these warnings).

So you would have to write your own TypeAdapters to get support for these classes or use libraries which add support for them. https://github.com/gkopff/gson-javatime-serialisers might be helpful though I have not used it yet.

Yes, as a workaround, I wrote my own adapters like below:

public final class LocalDateTimeJsonAdapter implements JsonSerializer<LocalDateTime>, JsonDeserializer<LocalDateTime> {

    public static final LocalDateTimeJsonAdapter INSTANCE = new LocalDateTimeJsonAdapter();


    private LocalDateTimeJsonAdapter() {
    }

    @Override
    public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        if (!json.isJsonNull()) {
            return LocalDateTime.parse(json.getAsString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }
        return null;
    }

    @Override
    public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
        if (src != null) {
            return new JsonPrimitive(src.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        }
        return JsonNull.INSTANCE;
    }
}

@Marcono1234
Copy link
Collaborator

Note that it is recommended to prefer TypeAdapter over JsonSerializer and JsonDeserializer since it reads the JSON data in a streaming way instead of parsing it as an in-memory representation of JsonElements. The code for it will be pretty similar to your current adapter.

@EldarAgalarov
Copy link
Author

I will look at it. Thank you!

@Marcono1234
Copy link
Collaborator

Can probably be closed as duplicate of #1059.

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

No branches or pull requests

3 participants