spring-native icon indicating copy to clipboard operation
spring-native copied to clipboard

Support lambda class serialization via hints

Open hanakeichen opened this issue 1 year ago • 2 comments

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);
	}

}

hanakeichen avatar Jul 13 '22 07:07 hanakeichen

@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 avatar Jul 13 '22 07:07 pivotal-cla

@hanakeichen Thank you for signing the Contributor License Agreement!

pivotal-cla avatar Jul 13 '22 07:07 pivotal-cla

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.

sdeleuze avatar Nov 28 '22 14:11 sdeleuze