Skip to content

Commit

Permalink
Make ExecutionEnvironment serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jul 6, 2024
1 parent 3a3eba1 commit be6adac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*******************************************************************************/
package org.eclipse.jdt.debug.tests.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
Expand Down Expand Up @@ -259,4 +264,18 @@ public void testEEHomeVariableInvalidArgument() throws Exception {
}
assertNotNull("Test should have thrown an exception", null);
}

public void testSerializability() throws Exception {
IExecutionEnvironment ee = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("JavaSE-21");
ByteArrayOutputStream bs = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(bs);) {
out.writeObject(ee);
}
byte[] bytes = bs.toByteArray();
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));) {
IExecutionEnvironment readEE = (IExecutionEnvironment) in.readObject();
assertSame(ee, readEE);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -58,7 +60,19 @@
*
* @since 3.2
*/
class ExecutionEnvironment implements IExecutionEnvironment {
class ExecutionEnvironment implements IExecutionEnvironment, Serializable {

record SerializationSurrogate(String id) implements Serializable {
@SuppressWarnings("unused") // called during de-serialization
Object readResolve() throws ObjectStreamException {
return JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(id);
}
}

@SuppressWarnings("unused") // called during serialization
Object writeReplace() throws ObjectStreamException {
return new SerializationSurrogate(getId());
}

/**
* Add a VM changed listener to clear cached values when a VM changes or is removed
Expand Down

0 comments on commit be6adac

Please sign in to comment.