KT-836 Override toString() more correctly

This commit is contained in:
Sergey Ignatov
2011-12-15 15:49:51 +04:00
parent 4361dce2b0
commit 115a170f5b
3 changed files with 89 additions and 1 deletions
+34 -1
View File
@@ -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<HierarchicalMethodSignature> 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<HierarchicalMethodSignature> 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);
+30
View File
@@ -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";
}
}
+25
View File
@@ -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"
}
}