spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 355 forks source link

Support lambda class serialization via hints #1670

Closed hanakeichen closed 1 year ago

hanakeichen commented 2 years ago

Currently lambdas serialization https://github.com/oracle/graal/commit/b455f23b624a1af149c7d5769443a4775bedd087 is supported in GraalVM native-image. This commit add lambdaCapturingTypes and lambdaCapturingTypeNames to conveniently add capturing classes to serialization configuration.

Example:

public class LambdaClassSerialization {
    private static void serialize(ByteArrayOutputStream byteArrayOutputStream, Serializable serializableObject) throws IOException {
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(serializableObject);
        objectOutputStream.close();
    }

    private static Object deserialize(ByteArrayOutputStream byteArrayOutputStream) throws IOException, ClassNotFoundException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        return objectInputStream.readObject();
    }

    public static void testLambdaClassSerialization() throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Function<Integer, String> function = (Function<Integer, String> & Serializable) (x) -> "Hello";

        serialize(byteArrayOutputStream, (Serializable) function);
        (Function<Integer, String>) deserialize(byteArrayOutputStream);
    }
}

// serialization-config.json output :
// 
// {
//   "types":[
//   ],
//   "lambdaCapturingTypes":[
//     {
//       "name":"com.xxx.LambdaClassSerialization"
//     }
//   ]
// }
@SerializationHint(lambdaCapturingTypes = {LambdaClassSerialization.class})
@SpringBootApplication
public class LambdaClassSerializationSpringApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(LambdaClassSerializationSpringApplication.class, args);
    }

}
pivotal-cla commented 2 years ago

@hanakeichen Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

pivotal-cla commented 2 years ago

@hanakeichen Thank you for signing the Contributor License Agreement!

sdeleuze commented 1 year ago

Since Spring Boot 3.0 GA has been released, Spring Native is not actively developed, so I will close this PR unmerged. I don't think we support lambda class serialization in Spring Framework 6, and not sure yet if we should support it or not yet. We should be sure to have enough use cases if we want to support it, so if you create a PR/issue on Spring Framework side, make sure to provide more details on your use cases.