Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Support lambda class serialization via hints #1670

Closed
wants to merge 1 commit into from

Conversation

hanakeichen
Copy link

@hanakeichen hanakeichen commented Jul 13, 2022

Currently lambdas serialization oracle/graal@b455f23 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
Copy link

@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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 13, 2022
@pivotal-cla
Copy link

@hanakeichen Thank you for signing the Contributor License Agreement!

@sdeleuze
Copy link
Contributor

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 sdeleuze closed this Nov 28, 2022
@sdeleuze sdeleuze added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Development

Successfully merging this pull request may close these issues.

4 participants