org.gradle.internal.serialize.DefaultSerializerRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.internal.serialize;
import com.google.common.base.Objects;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class DefaultSerializerRegistry implements SerializerRegistry {
private final Map, Serializer>> serializerMap = new TreeMap, Serializer>>(new Comparator>() {
public int compare(Class> o1, Class> o2) {
return o1.getName().compareTo(o2.getName());
}
});
private final Set> javaSerialization = new HashSet>();
private final SerializerClassMatcherStrategy classMatcher;
public DefaultSerializerRegistry() {
this(true);
}
public DefaultSerializerRegistry(boolean supportClassHierarchy) {
this.classMatcher = supportClassHierarchy ? SerializerClassMatcherStrategy.HIERARCHY : SerializerClassMatcherStrategy.STRICT;
}
@Override
public void register(Class implementationType, Serializer serializer) {
serializerMap.put(implementationType, serializer);
}
@Override
public void useJavaSerialization(Class implementationType) {
javaSerialization.add(implementationType);
}
@Override
public boolean canSerialize(Class> baseType) {
for (Class> candidate : serializerMap.keySet()) {
if (classMatcher.matches(baseType, candidate)) {
return true;
}
}
for (Class> candidate : javaSerialization) {
if (classMatcher.matches(baseType, candidate)) {
return true;
}
}
return false;
}
@Override
public Serializer build(Class baseType) {
Map, Serializer>> matches = new LinkedHashMap, Serializer>>();
for (Map.Entry, Serializer>> entry : serializerMap.entrySet()) {
if (baseType.isAssignableFrom(entry.getKey())) {
matches.put(entry.getKey(), entry.getValue());
}
}
Set> matchingJavaSerialization = new LinkedHashSet>();
for (Class> candidate : javaSerialization) {
if (baseType.isAssignableFrom(candidate)) {
matchingJavaSerialization.add(candidate);
}
}
if (matches.isEmpty() && matchingJavaSerialization.isEmpty()) {
throw new IllegalArgumentException(String.format("Don't know how to serialize objects of type %s.", baseType.getName()));
}
if (matches.size() == 1 && matchingJavaSerialization.isEmpty()) {
return (Serializer) matches.values().iterator().next();
}
return new TaggedTypeSerializer(matches, matchingJavaSerialization);
}
private static class TypeInfo {
final int tag;
final boolean useForSubtypes;
final Serializer serializer;
private TypeInfo(int tag, boolean useForSubtypes, Serializer serializer) {
this.tag = tag;
this.useForSubtypes = useForSubtypes;
this.serializer = serializer;
}
}
private static class TaggedTypeSerializer extends AbstractSerializer {
private static final int JAVA_TYPE = 1; // Reserve 0 for null (to be added later)
private static final TypeInfo JAVA_SERIALIZATION = new TypeInfo(JAVA_TYPE, true, new DefaultSerializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy