Skip to content

Class.forName(Jdk9Platform) throws during warm up #9462

@xiaoyicao-ctrl

Description

@xiaoyicao-ctrl

When we try to warm up our Java binary by invoking Class.forName to pre-load all the classes we use, okhttp3.internal.platform.Jdk9Platform throws an ExceptionInInitializerError exception.

The root cause is that there is a cyclic initialization between Jdk9Platform and its parent Platform:

Initialize Jdk9Platform > Initialize Platform > Initialize Platform.Companion > eventually calls Jdk9Platform.buildIfSupported() > at this point, the companion object of Jdk9Platform has not been initialized yet.

One possible way to solve this is to make Platform.platform lazy (does not call getPlatform() immediately).

Test:

import org.junit.Test;

public class OkHttpBug {
  @Test
  public void testFailure() throws Exception {
    Class.forName("okhttp3.internal.platform.Jdk9Platform");

    // Expected Behavior:
    // The class okhttp3.internal.platform.Jdk9Platform should be loaded successfully without
    // throwing any exceptions.
    //
    // Actual Failure:
    // java.lang.ExceptionInInitializerError
    //     ...
    // Caused by: java.lang.NullPointerException: Cannot invoke
    // "okhttp3.internal.platform.Jdk9Platform$Companion.buildIfSupported()" because
    // "okhttp3.internal.platform.Jdk9Platform.Companion" is null

    System.out.println("Loaded Jdk9Platform successfully.");
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug in existing code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions