Create an extension point to DescriptorSerializer

Extract the existing predicate logic into it
This commit is contained in:
Alexander Udalov
2013-07-05 21:09:14 +04:00
parent 4aeeafdff0
commit 548a2853aa
3 changed files with 39 additions and 14 deletions
@@ -16,8 +16,6 @@
package org.jetbrains.jet.descriptors.serialization;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
@@ -47,24 +45,24 @@ public class DescriptorSerializer {
};
private final NameTable nameTable;
private final Interner<TypeParameterDescriptor> typeParameters;
private final Predicate<ClassDescriptor> isSpecial;
private final SerializerExtension extension;
public DescriptorSerializer() {
this(Predicates.<ClassDescriptor>alwaysFalse());
this(SerializerExtension.DEFAULT);
}
public DescriptorSerializer(@NotNull Predicate<ClassDescriptor> isSpecial) {
this(new NameTable(), new Interner<TypeParameterDescriptor>(), isSpecial);
public DescriptorSerializer(@NotNull SerializerExtension extension) {
this(new NameTable(), new Interner<TypeParameterDescriptor>(), extension);
}
private DescriptorSerializer(NameTable nameTable, Interner<TypeParameterDescriptor> typeParameters, Predicate<ClassDescriptor> isSpecial) {
private DescriptorSerializer(NameTable nameTable, Interner<TypeParameterDescriptor> typeParameters, SerializerExtension extension) {
this.nameTable = nameTable;
this.typeParameters = typeParameters;
this.isSpecial = isSpecial;
this.extension = extension;
}
private DescriptorSerializer createChildSerializer() {
return new DescriptorSerializer(nameTable, new Interner<TypeParameterDescriptor>(typeParameters), Predicates.<ClassDescriptor>alwaysFalse());
return new DescriptorSerializer(nameTable, new Interner<TypeParameterDescriptor>(typeParameters), extension);
}
@NotNull
@@ -90,7 +88,7 @@ public class DescriptorSerializer {
builder.addTypeParameter(local.typeParameter(typeParameterDescriptor));
}
if (!isSpecial.apply(classDescriptor)) {
if (extension.hasSupertypes(classDescriptor)) {
// Special classes (Any, Nothing) have no supertypes
for (JetType supertype : classDescriptor.getTypeConstructor().getSupertypes()) {
builder.addSupertype(local.type(supertype));
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* 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.jetbrains.jet.descriptors.serialization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
public abstract class SerializerExtension {
public static final SerializerExtension DEFAULT = new SerializerExtension() {};
public boolean hasSupertypes(@NotNull ClassDescriptor descriptor) {
return true;
}
}
@@ -1,6 +1,5 @@
package org.jetbrains.jet.generators.builtins;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
@@ -59,12 +58,12 @@ public class BuiltInsSerializer {
System.err.println("Could not make directories: " + destDir);
}
final DescriptorSerializer serializer = new DescriptorSerializer(new Predicate<ClassDescriptor>() {
final DescriptorSerializer serializer = new DescriptorSerializer(new SerializerExtension() {
private final ImmutableSet<String> set = ImmutableSet.of("Any", "Nothing");
@Override
public boolean apply(ClassDescriptor classDescriptor) {
return set.contains(classDescriptor.getName().asString());
public boolean hasSupertypes(@NotNull ClassDescriptor classDescriptor) {
return !set.contains(classDescriptor.getName().asString());
}
});
ByteArrayOutputStream classNames = new ByteArrayOutputStream();