KT-2819 Duplicate toString() method generated in data class

#KT-2819 Fixed
This commit is contained in:
Andrey Breslav
2012-09-26 12:32:16 +04:00
parent b5145f1a87
commit 92ac865c72
9 changed files with 188 additions and 6 deletions
@@ -37,10 +37,7 @@ import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.*;
import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE;
@@ -200,4 +197,41 @@ public class CodegenUtil {
throw new IllegalStateException("code generation for synthesized members should be handled separately");
}
}
@Nullable
public static FunctionDescriptor getDeclaredFunctionByRawSignature(
@NotNull ClassDescriptor owner,
@NotNull Name name,
@NotNull ClassDescriptor returnedClass,
@NotNull ClassDescriptor... valueParameterClasses
) {
Collection<FunctionDescriptor> functions = owner.getDefaultType().getMemberScope().getFunctions(name);
for (FunctionDescriptor function : functions) {
if (function.getKind() == CallableMemberDescriptor.Kind.DECLARATION
&& function.getTypeParameters().isEmpty()
&& valueParameterClassesMatch(function.getValueParameters(), Arrays.asList(valueParameterClasses))
&& rawTypeMatches(function.getReturnType(), returnedClass)) {
return function;
}
}
return null;
}
private static boolean valueParameterClassesMatch(
@NotNull List<ValueParameterDescriptor> parameters,
@NotNull List<ClassDescriptor> classes) {
if (parameters.size() != classes.size()) return false;
for (int i = 0; i < parameters.size(); i++) {
ValueParameterDescriptor parameterDescriptor = parameters.get(i);
ClassDescriptor classDescriptor = classes.get(i);
if (!rawTypeMatches(parameterDescriptor.getType(), classDescriptor)) {
return false;
}
}
return true;
}
private static boolean rawTypeMatches(JetType type, ClassDescriptor classDescriptor) {
return type.getConstructor().getDeclarationDescriptor().getOriginal() == classDescriptor.getOriginal();
}
}
@@ -50,7 +50,9 @@ import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
import org.jetbrains.jet.lang.resolve.java.kt.DescriptorKindUtils;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.lexer.JetTokens;
@@ -411,10 +413,33 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
generateComponentFunctionsForDataClasses();
final List<PropertyDescriptor> properties = getDataProperties();
List<PropertyDescriptor> properties = getDataProperties();
if (!properties.isEmpty()) {
generateDataClassToStringIfNeeded(properties);
generateDataClassHashCodeIfNeeded(properties);
generateDataClassEqualsIfNeeded(properties);
}
}
private void generateDataClassToStringIfNeeded(List<PropertyDescriptor> properties) {
ClassDescriptor stringClass = JetStandardLibrary.getInstance().getString();
if (getDeclaredFunctionByRawSignature(descriptor, Name.identifier("toString"), stringClass) == null) {
generateDataClassToStringMethod(properties);
}
}
private void generateDataClassHashCodeIfNeeded(List<PropertyDescriptor> properties) {
ClassDescriptor intClass = JetStandardLibrary.getInstance().getInt();
if (getDeclaredFunctionByRawSignature(descriptor, Name.identifier("hashCode"), intClass) == null) {
generateDataClassHashCodeMethod(properties);
}
}
private void generateDataClassEqualsIfNeeded(List<PropertyDescriptor> properties) {
ClassDescriptor booleanClass = JetStandardLibrary.getInstance().getBoolean();
ClassDescriptor anyClass = JetStandardClasses.getAny();
FunctionDescriptor equalsFunction = getDeclaredFunctionByRawSignature(descriptor, Name.identifier("equals"), booleanClass, anyClass);
if (equalsFunction == null) {
generateDataClassEqualsMethod(properties);
}
}