-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support for lambda class serialization #3792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bf5e456
1753252
541c200
221904b
c45e8f6
186af89
584076d
ae4744f
e6df7fa
fca81bc
79b1624
7e580bb
f06a76e
1c6530b
8934767
258dd8b
82af5a4
c3ae380
e3de038
8765cdc
93e47e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ | |
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import com.oracle.svm.configure.json.JsonPrintable; | ||
import org.graalvm.compiler.java.LambdaUtils; | ||
import org.graalvm.nativeimage.impl.ConfigurationCondition; | ||
import org.graalvm.nativeimage.impl.RuntimeSerializationSupport; | ||
|
||
|
@@ -40,36 +42,60 @@ | |
|
||
public class SerializationConfiguration implements ConfigurationBase, RuntimeSerializationSupport { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you're using subclassing so you can store two different kinds of types together here that would really better be separated in two maps: captured types and capturing types. The two should also be separated in the configuration file. Then two classes would also be sufficient: a type and a captured type with a custom constructor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separated as suggested. |
||
|
||
private final Set<SerializationConfigurationType> serializations = ConcurrentHashMap.newKeySet(); | ||
private final Set<SerializationConfigurationType> serializationTypes = ConcurrentHashMap.newKeySet(); | ||
private final Set<SerializationConfigurationLambdaCapturingType> lambdaSerializationCapturingTypes = ConcurrentHashMap.newKeySet(); | ||
|
||
public SerializationConfiguration() { | ||
} | ||
|
||
public SerializationConfiguration(SerializationConfiguration other) { | ||
serializations.addAll(other.serializations); | ||
serializationTypes.addAll(other.serializationTypes); | ||
lambdaSerializationCapturingTypes.addAll(other.lambdaSerializationCapturingTypes); | ||
} | ||
|
||
public void removeAll(SerializationConfiguration other) { | ||
serializations.removeAll(other.serializations); | ||
serializationTypes.removeAll(other.serializationTypes); | ||
lambdaSerializationCapturingTypes.removeAll(other.lambdaSerializationCapturingTypes); | ||
} | ||
|
||
public boolean contains(ConfigurationCondition condition, String serializationTargetClass, String customTargetConstructorClass) { | ||
return serializations.contains(createConfigurationType(condition, serializationTargetClass, customTargetConstructorClass)); | ||
return serializationTypes.contains(createConfigurationType(condition, serializationTargetClass, customTargetConstructorClass)) || | ||
lambdaSerializationCapturingTypes.contains(createLambdaCapturingClassConfigurationType(condition, serializationTargetClass)); | ||
} | ||
|
||
@Override | ||
public void printJson(JsonWriter writer) throws IOException { | ||
writer.append('[').indent(); | ||
writer.append('{').indent().newline(); | ||
List<SerializationConfigurationType> listOfCapturedClasses = new ArrayList<>(serializationTypes); | ||
Collections.sort(listOfCapturedClasses); | ||
printSerializationClasses(writer, "types", listOfCapturedClasses); | ||
writer.append(",").newline(); | ||
List<SerializationConfigurationLambdaCapturingType> listOfCapturingClasses = new ArrayList<>(lambdaSerializationCapturingTypes); | ||
listOfCapturingClasses.sort(new SerializationConfigurationLambdaCapturingType.SerializationConfigurationLambdaCapturingTypesComparator()); | ||
printSerializationClasses(writer, "lambdaCapturingTypes", listOfCapturingClasses); | ||
writer.unindent().newline(); | ||
writer.append('}'); | ||
} | ||
|
||
private static void printSerializationClasses(JsonWriter writer, String types, List<? extends JsonPrintable> serializationConfigurationTypes) throws IOException { | ||
writer.quote(types).append(":"); | ||
writer.append('['); | ||
writer.indent(); | ||
|
||
printSerializationTypes(serializationConfigurationTypes, writer); | ||
|
||
writer.unindent().newline(); | ||
writer.append("]"); | ||
} | ||
|
||
private static void printSerializationTypes(List<? extends JsonPrintable> serializationConfigurationTypes, JsonWriter writer) throws IOException { | ||
String prefix = ""; | ||
List<SerializationConfigurationType> list = new ArrayList<>(serializations); | ||
Collections.sort(list); | ||
for (SerializationConfigurationType type : list) { | ||
|
||
for (JsonPrintable type : serializationConfigurationTypes) { | ||
writer.append(prefix).newline(); | ||
type.printJson(writer); | ||
prefix = ","; | ||
} | ||
writer.unindent().newline(); | ||
writer.append(']'); | ||
} | ||
|
||
@Override | ||
|
@@ -91,17 +117,27 @@ public void registerWithTargetConstructorClass(ConfigurationCondition condition, | |
|
||
@Override | ||
public void registerWithTargetConstructorClass(ConfigurationCondition condition, String className, String customTargetConstructorClassName) { | ||
serializations.add(createConfigurationType(condition, className, customTargetConstructorClassName)); | ||
serializationTypes.add(createConfigurationType(condition, className, customTargetConstructorClassName)); | ||
} | ||
|
||
@Override | ||
public void registerLambdaCapturingClass(ConfigurationCondition condition, String lambdaCapturingClassName) { | ||
lambdaSerializationCapturingTypes.add(createLambdaCapturingClassConfigurationType(condition, lambdaCapturingClassName.split(LambdaUtils.LAMBDA_SPLIT_PATTERN)[0])); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return serializations.isEmpty(); | ||
return serializationTypes.isEmpty() && lambdaSerializationCapturingTypes.isEmpty(); | ||
} | ||
|
||
private static SerializationConfigurationType createConfigurationType(ConfigurationCondition condition, String className, String customTargetConstructorClassName) { | ||
String convertedClassName = SignatureUtil.toInternalClassName(className); | ||
String convertedCustomTargetConstructorClassName = customTargetConstructorClassName == null ? null : SignatureUtil.toInternalClassName(customTargetConstructorClassName); | ||
return new SerializationConfigurationType(condition, convertedClassName, convertedCustomTargetConstructorClassName); | ||
} | ||
|
||
private static SerializationConfigurationLambdaCapturingType createLambdaCapturingClassConfigurationType(ConfigurationCondition condition, String className) { | ||
String convertedClassName = SignatureUtil.toInternalClassName(className); | ||
return new SerializationConfigurationLambdaCapturingType(condition, convertedClassName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
package com.oracle.svm.configure.config; | ||
|
||
import java.io.IOException; | ||
import java.util.Comparator; | ||
import java.util.Objects; | ||
|
||
import com.oracle.svm.configure.json.JsonPrintable; | ||
import org.graalvm.nativeimage.impl.ConfigurationCondition; | ||
|
||
import com.oracle.svm.configure.json.JsonWriter; | ||
import com.oracle.svm.core.configure.SerializationConfigurationParser; | ||
|
||
public class SerializationConfigurationLambdaCapturingType implements JsonPrintable { | ||
private final ConfigurationCondition condition; | ||
private final String qualifiedJavaName; | ||
|
||
public SerializationConfigurationLambdaCapturingType(ConfigurationCondition condition, String qualifiedJavaName) { | ||
assert qualifiedJavaName.indexOf('/') == -1 : "Requires qualified Java name, not the internal representation"; | ||
Objects.requireNonNull(condition); | ||
this.condition = condition; | ||
Objects.requireNonNull(qualifiedJavaName); | ||
this.qualifiedJavaName = qualifiedJavaName; | ||
} | ||
|
||
@Override | ||
public void printJson(JsonWriter writer) throws IOException { | ||
writer.append('{').indent().newline(); | ||
ConfigurationConditionPrintable.printConditionAttribute(condition, writer); | ||
|
||
writer.quote(SerializationConfigurationParser.NAME_KEY).append(":").quote(qualifiedJavaName); | ||
writer.unindent().newline().append('}'); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
SerializationConfigurationLambdaCapturingType that = (SerializationConfigurationLambdaCapturingType) o; | ||
return condition.equals(that.condition) && | ||
qualifiedJavaName.equals(that.qualifiedJavaName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(condition, qualifiedJavaName); | ||
} | ||
|
||
public static final class SerializationConfigurationLambdaCapturingTypesComparator implements Comparator<SerializationConfigurationLambdaCapturingType> { | ||
|
||
@Override | ||
public int compare(SerializationConfigurationLambdaCapturingType o1, SerializationConfigurationLambdaCapturingType o2) { | ||
int compareName = o1.qualifiedJavaName.compareTo(o2.qualifiedJavaName); | ||
if (compareName != 0) { | ||
return compareName; | ||
} | ||
return o1.condition.compareTo(o2.condition); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we use this one for all we need?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the explanation is in the comment above.