googleapis/java-storage-nio

LocalStorageHelper.getOption() returns a non-serializable StorageOptions

jianglai opened this issue · 0 comments

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

If you are still having issues, please include as much information as possible:

Environment details

  1. Specify the API at the beginning of the title. For example, "BigQuery: ...").
    General, Core, and Other are also allowed as types
  2. OS type and version: Arch Linux 5.4
  3. Java version: openjdk version "11.0.11" 2021-04-20
  4. storage_nio version(s): 0.123.2

Steps to reproduce

  1. Get a StorageOption from LocalStorageHelper.getOption()
  2. Try to serialize it.

Code example

    StorageOptions storageOptions = LocalStorageHelper.getOptions();
    byte[] bytes;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos)) {
      oos.writeObject(storageOptions);
      oos.flush();
      oos.close();
      bytes = baos.toByteArray();
    }
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
      assertThat(ois.readObject()).isEqualTo(storageOptions);
    }

Stack trace

Caused by: java.io.IOException: java.lang.IllegalAccessException: class com.google.cloud.ServiceOptions cannot access a member of class com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper$1 with modifiers ""
	at com.google.cloud.ServiceOptions.newInstance(ServiceOptions.java:716)
	at com.google.cloud.ServiceOptions.readObject(ServiceOptions.java:707)

Any additional information below

StorageOptions extends Serializable, so it should be. The reason this does not work is that the ServiceRpcFactory in it is set to be an anonymous inner class, which has a package private constructor. The solution is to explicitly define a public static nested class.