{"record":{"id":"136f7e08e89587cc","repo":"google/guava","slug":"serialization-failed-on-return-value-of-s","errorCode":null,"errorMessage":"Serialization failed on return value of %s","messagePattern":"Serialization failed on return value of (.+?)","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java","lineNumber":514,"sourceCode":"\n    /**\n     * Runs serialization test on the return values of the static methods.\n     *\n     * <p>Test fails if default value cannot be determined for a constructor or factory method\n     * parameter, or if the constructor or factory method throws exception.\n     *\n     * @return this tester\n     */\n    @CanIgnoreReturnValue\n    @SuppressWarnings(\"CatchingUnchecked\") // sneaky checked exception\n    public FactoryMethodReturnValueTester testSerializable() throws Exception {\n      for (Invokable<?, ?> factory : getFactoriesToTest()) {\n        Object instance = instantiate(factory);\n        if (instance != null) {\n          try {\n            reserialize(instance);\n          } catch (Exception e) { // sneaky checked exception\n            throw new AssertionError(\n                \"Serialization failed on return value of \" + factory, e.getCause());\n          }\n        }\n      }\n      return this;\n    }\n\n    /**\n     * Runs equals and serialization test on the return values.\n     *\n     * <p>Test fails if default value cannot be determined for a constructor or factory method\n     * parameter, or if the constructor or factory method throws exception.\n     *\n     * @return this tester\n     */\n    @CanIgnoreReturnValue\n    @SuppressWarnings(\"CatchingUnchecked\") // sneaky checked exception\n    public FactoryMethodReturnValueTester testEqualsAndSerializable() throws Exception {","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/google/guava/blob/94f39958baf7ad51ddf9c70e406ed6b188194daa/android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java#L496-L532","documentation":"Thrown by FactoryMethodReturnValueTester.testSerializable() when reserialize(instance) throws for an object returned by a static factory. The returned object is not serializable (missing Serializable, non-serializable field, or write/readObject mismatch). The cause is unwrapped via getCause() and wrapped with the factory name.","triggerScenarios":"Calling ClassSanityTester.testSerializable() (or testAll()) on a class whose static factory returns a non-Serializable instance, or an instance whose transient graph fails to round-trip.","commonSituations":"A class meant to be serializable but missing implements Serializable; fields holding non-serializable collaborators (lambdas, anonymous classes, JDBC connections); custom writeObject/readObject that are not symmetric.","solutions":["Read the wrapped cause to find the offending field/class.","Make the declaring class implement java.io.Serializable and mark non-serializable fields transient (with custom readResolve/writeReplace if needed).","If the class is intentionally non-serializable, exclude it from the testSerializable() pass rather than letting it fail."],"exampleFix":"// before\npublic final class Token { private final DataSource ds; ... }\n\n// after\npublic final class Token implements Serializable {\n  private final String id;\n  private transient DataSource ds; // not serialized\n  private Object readResolve() { ds = lookupDataSource(); return this; }\n}","handlingStrategy":"validation","validationCode":"Object instance = factory.invoke(null); // static factory\nif (instance != null && !(instance instanceof java.io.Serializable))\n  throw new IllegalStateException(factory + \" returns non-Serializable instance\");","typeGuard":"static boolean isSerializableReturn(java.lang.reflect.Method m) {\n  return java.io.Serializable.class.isAssignableFrom(m.getReturnType());\n}","tryCatchPattern":null,"preventionTips":["Implement Serializable on types meant to be serialized; mark non-serializable fields transient.","Reserve testSerializable() for classes that are contractually serializable."],"tags":["guava-testlib","serialization","reflection","class-sanity"],"backgroundTag":null,"analyzedSha":"94f39958baf7ad51ddf9c70e406ed6b188194daa","analyzedAt":"2026-08-13T22:50:30.265Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}