From 115a170f5b4674b505f3321c60e8d517a73b3917 Mon Sep 17 00:00:00 2001 From: Sergey Ignatov Date: Thu, 15 Dec 2011 15:49:51 +0400 Subject: [PATCH] KT-836 Override toString() more correctly --- src/org/jetbrains/jet/j2k/Converter.java | 35 +++++++++++++++++++++++- testData/ast/issues/file/kt-836.jav | 30 ++++++++++++++++++++ testData/ast/issues/file/kt-836.kt | 25 +++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 testData/ast/issues/file/kt-836.jav create mode 100644 testData/ast/issues/file/kt-836.kt diff --git a/src/org/jetbrains/jet/j2k/Converter.java b/src/org/jetbrains/jet/j2k/Converter.java index a448b78d4a1..17159937e0c 100644 --- a/src/org/jetbrains/jet/j2k/Converter.java +++ b/src/org/jetbrains/jet/j2k/Converter.java @@ -364,6 +364,28 @@ public class Converter { } private static boolean isOverrideAnyMethodExceptMethodsFromObject(@NotNull PsiMethod method) { + boolean counter = normalCase(method); + if (counter) + return true; + if (isInheritFromObject(method)) + return caseForObject(method); + return false; + } + + private static boolean caseForObject(@NotNull PsiMethod method) { + PsiClass containing = method.getContainingClass(); + if (containing != null) { + for (PsiClassType s : containing.getSuperTypes()) { + String canonicalText = s.getCanonicalText(); + if (!canonicalText.equals("java.lang.Object") && !getClassIdentifiers().contains(canonicalText)) { + return true; + } + } + } + return false; + } + + private static boolean normalCase(@NotNull PsiMethod method) { int counter = 0; for (HierarchicalMethodSignature s : method.getHierarchicalMethodSignature().getSuperSignatures()) { PsiClass containingClass = s.getMethod().getContainingClass(); @@ -374,6 +396,17 @@ public class Converter { return counter > 0; } + private static boolean isInheritFromObject(@NotNull PsiMethod method) { + List superSignatures = method.getHierarchicalMethodSignature().getSuperSignatures(); + for (HierarchicalMethodSignature s : superSignatures) { + PsiClass containingClass = s.getMethod().getContainingClass(); + String qualifiedName = containingClass != null ? containingClass.getQualifiedName() : ""; + if (qualifiedName != null && qualifiedName.equals("java.lang.Object")) + return true; + } + return false; + } + private static boolean isOverrideObjectDirect(@NotNull final PsiMethod method) { List superSignatures = method.getHierarchicalMethodSignature().getSuperSignatures(); if (superSignatures.size() == 1) { @@ -652,7 +685,7 @@ public class Converter { // } @NotNull - public static SureCallChainExpression createSureCallOnlyForChain(PsiExpression expression, PsiType type) { + public static SureCallChainExpression createSureCallOnlyForChain(@Nullable PsiExpression expression, @NotNull PsiType type) { String conversion = (expression != null && (expression instanceof PsiReferenceExpression || expression instanceof PsiMethodCallExpression)) ? createConversionForExpression(expression, type) : ""; return new SureCallChainExpression(expressionToExpression(expression), conversion); diff --git a/testData/ast/issues/file/kt-836.jav b/testData/ast/issues/file/kt-836.jav new file mode 100644 index 00000000000..99d0b460e3a --- /dev/null +++ b/testData/ast/issues/file/kt-836.jav @@ -0,0 +1,30 @@ +package com.voltvoodoo.saplo4j.model; + +import java.io.Serializable; + +public class Language implements Serializable { + protected String code; + + public Language(String code) { + this.code = code; + } + + public String toString() { + return this.code; + } +} + + +class Base { + void test() {} + String toString() { + return "BASE"; + } +} + +class Child extends Base { + void test() {} + String toString() { + return "Child"; + } +} diff --git a/testData/ast/issues/file/kt-836.kt b/testData/ast/issues/file/kt-836.kt new file mode 100644 index 00000000000..3a73a7a4d52 --- /dev/null +++ b/testData/ast/issues/file/kt-836.kt @@ -0,0 +1,25 @@ +namespace com.voltvoodoo.saplo4j.model +import java.io.Serializable +public open class Language(code : String?) : Serializable { +{ +this.$code = code +} +protected var code : String? = null +override public fun toString() : String? { +return this.code +} +} +open class Base() { +open fun test() : Unit { +} +open fun toString() : String? { +return "BASE" +} +} +open class Child() : Base() { +override fun test() : Unit { +} +override fun toString() : String? { +return "Child" +} +} \ No newline at end of file