Simplified code in functions hierarchy.

Minimized constructors variety and reduced code duplication.
This commit is contained in:
Evgeny Gerashchenko
2014-02-14 19:37:33 +04:00
parent 537c03fb1a
commit cb0a85bc50
6 changed files with 46 additions and 87 deletions
@@ -219,7 +219,7 @@ public class DescriptorDeserializer {
@NotNull
private CallableMemberDescriptor loadFunction(@NotNull Callable proto) {
int flags = proto.getFlags();
DeserializedSimpleFunctionDescriptor function = new DeserializedSimpleFunctionDescriptor(
DeserializedSimpleFunctionDescriptor function = DeserializedSimpleFunctionDescriptor.create(
containingDeclaration, proto,
deserializers,
nameResolver
@@ -17,6 +17,7 @@
package org.jetbrains.jet.descriptors.serialization.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.descriptors.serialization.DescriptorDeserializer;
import org.jetbrains.jet.descriptors.serialization.Flags;
import org.jetbrains.jet.descriptors.serialization.NameResolver;
@@ -35,20 +36,7 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
private DeserializedSimpleFunctionDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind,
@NotNull ProtoBuf.Callable functionProto,
@NotNull NameResolver nameResolver
) {
super(containingDeclaration, annotations, name, kind);
this.functionProto = functionProto;
this.nameResolver = nameResolver;
}
private DeserializedSimpleFunctionDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull SimpleFunctionDescriptor original,
@Nullable SimpleFunctionDescriptor original,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind,
@@ -59,45 +47,17 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
this.nameResolver = nameResolver;
}
public DeserializedSimpleFunctionDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull ProtoBuf.Callable functionProto,
@NotNull Deserializers deserializers,
@NotNull NameResolver nameResolver
) {
this(containingDeclaration,
DescriptorDeserializer.getAnnotations(containingDeclaration, functionProto, functionProto.getFlags(),
Deserializers.AnnotatedCallableKind.FUNCTION, deserializers.getAnnotationDeserializer(),
nameResolver),
nameResolver.getName(functionProto.getName()),
DescriptorDeserializer.memberKind(Flags.MEMBER_KIND.get(functionProto.getFlags())),
functionProto,
nameResolver);
}
@Override
protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal, Kind kind) {
if (preserveOriginal) {
return new DeserializedSimpleFunctionDescriptor(
newOwner,
getOriginal(),
getAnnotations(),
getName(),
kind,
functionProto,
nameResolver
);
}
else {
return new DeserializedSimpleFunctionDescriptor(
newOwner,
getAnnotations(),
getName(),
kind,
functionProto,
nameResolver
);
}
return new DeserializedSimpleFunctionDescriptor(
newOwner,
preserveOriginal ? getOriginal() : null,
getAnnotations(),
getName(),
kind,
functionProto,
nameResolver
);
}
@NotNull
@@ -113,4 +73,24 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
public NameResolver getNameResolver() {
return nameResolver;
}
public static DeserializedSimpleFunctionDescriptor create(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull ProtoBuf.Callable functionProto,
@NotNull Deserializers deserializers,
@NotNull NameResolver nameResolver
) {
Annotations annotations = DescriptorDeserializer.getAnnotations(containingDeclaration, functionProto, functionProto.getFlags(),
Deserializers.AnnotatedCallableKind.FUNCTION,
deserializers.getAnnotationDeserializer(),
nameResolver);
return new DeserializedSimpleFunctionDescriptor(
containingDeclaration,
null,
annotations,
nameResolver.getName(functionProto.getName()),
DescriptorDeserializer.memberKind(Flags.MEMBER_KIND.get(functionProto.getFlags())),
functionProto,
nameResolver);
}
}
@@ -40,7 +40,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
}
public ConstructorDescriptorImpl(@NotNull ClassDescriptor containingDeclaration, @NotNull Annotations annotations, boolean isPrimary, Kind kind) {
super(containingDeclaration, annotations, NAME, kind);
super(containingDeclaration, null, annotations, NAME, kind);
this.isPrimary = isPrimary;
}
@@ -50,22 +50,12 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
protected FunctionDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@Nullable FunctionDescriptor original,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind) {
super(containingDeclaration, annotations, name);
this.original = this;
this.kind = kind;
}
protected FunctionDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull FunctionDescriptor original,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind) {
super(containingDeclaration, annotations, name);
this.original = original;
this.original = original == null ? this : original;
this.kind = kind;
}
@@ -28,7 +28,7 @@ import java.util.List;
public class ScriptCodeDescriptor extends FunctionDescriptorImpl {
public ScriptCodeDescriptor(@NotNull ScriptDescriptor containingDeclaration) {
super(containingDeclaration, Annotations.EMPTY, Name.special("<script-code>"), Kind.DECLARATION);
super(containingDeclaration, null, Annotations.EMPTY, Name.special("<script-code>"), Kind.DECLARATION);
setVisibility(Visibilities.LOCAL);
}
@@ -38,12 +38,12 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme
@NotNull Name name,
@NotNull Kind kind
) {
super(containingDeclaration, annotations, name, kind);
this(containingDeclaration, null, annotations, name, kind);
}
protected SimpleFunctionDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull SimpleFunctionDescriptor original,
@Nullable SimpleFunctionDescriptor original,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind) {
@@ -76,30 +76,19 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme
@Override
protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal, Kind kind) {
if (preserveOriginal) {
return new SimpleFunctionDescriptorImpl(
newOwner,
getOriginal(),
// TODO : safeSubstitute
getAnnotations(),
getName(),
kind);
}
else {
return new SimpleFunctionDescriptorImpl(
newOwner,
// TODO : safeSubstitute
getAnnotations(),
getName(),
kind);
}
return new SimpleFunctionDescriptorImpl(
newOwner,
preserveOriginal ? getOriginal() : null,
// TODO : safeSubstitute
getAnnotations(),
getName(),
kind);
}
@NotNull
@Override
public SimpleFunctionDescriptor copy(DeclarationDescriptor newOwner, Modality modality, Visibility visibility, Kind kind, boolean copyOverrides) {
SimpleFunctionDescriptorImpl copy = (SimpleFunctionDescriptorImpl)doSubstitute(TypeSubstitutor.EMPTY, newOwner, modality, visibility, false, copyOverrides, kind);
return copy;
return (SimpleFunctionDescriptorImpl)doSubstitute(TypeSubstitutor.EMPTY, newOwner, modality, visibility, false, copyOverrides, kind);
}
@NotNull