generate equals/hashCode: ensure one @Override is inserted; extensions could also provide their annotations on equals/hashCode override;

tests: language level is 1.5 though jdk is mockJdk14 without @Override as expected -> fqns in test data
This commit is contained in:
Anna Kozlova
2015-01-13 16:10:39 +01:00
parent 20f7213e03
commit 0074de3fa9
5 changed files with 18 additions and 16 deletions

View File

@@ -145,9 +145,6 @@ public class GenerateEqualsHelper implements Runnable {
private PsiMethod createEquals() throws IncorrectOperationException {
@NonNls StringBuilder buffer = new StringBuilder();
CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(myProject);
if (shouldAddOverrideAnnotation(myClass)) {
buffer.append("@Override\n");
}
ArrayList<PsiField> equalsFields = new ArrayList<PsiField>();
ContainerUtil.addAll(equalsFields, myEqualsFields);
Collections.sort(equalsFields, EqualsFieldsComparator.INSTANCE);
@@ -165,7 +162,8 @@ public class GenerateEqualsHelper implements Runnable {
nameSuggestions = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, objectType).names;
final String objectBaseName = nameSuggestions.length > 0 ? nameSuggestions[0] : "object";
contextMap.put("baseParamName", objectBaseName);
contextMap.put("superHasEquals", superMethodExists(getEqualsSignature(myProject, myClass.getResolveScope())));
final MethodSignature equalsSignature = getEqualsSignature(myProject, myClass.getResolveScope());
contextMap.put("superHasEquals", superMethodExists(equalsSignature));
contextMap.put("checkParameterWithInstanceof", myCheckParameterWithInstanceof);
final String methodText = GenerationUtil
@@ -185,6 +183,10 @@ public class GenerateEqualsHelper implements Runnable {
PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, styleSettings.GENERATE_FINAL_PARAMETERS);
PsiMethod method = (PsiMethod)myCodeStyleManager.reformat(result);
final PsiMethod superEquals = MethodSignatureUtil.findMethodBySignature(myClass, equalsSignature, true);
if (superEquals != null) {
OverrideImplementUtil.annotateOnOverrideImplement(method, myClass, superEquals);
}
method = (PsiMethod)myJavaCodeStyleManager.shortenClassReferences(method);
return method;
}
@@ -200,10 +202,6 @@ public class GenerateEqualsHelper implements Runnable {
private PsiMethod createHashCode() throws IncorrectOperationException {
@NonNls StringBuilder buffer = new StringBuilder();
if (shouldAddOverrideAnnotation(myClass)) {
buffer.append("@Override\n");
}
final HashMap<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put("superHasHashCode", mySuperHasHashCode);
@@ -218,6 +216,10 @@ public class GenerateEqualsHelper implements Runnable {
catch (IncorrectOperationException e) {
return null;
}
final PsiMethod superHashCode = MethodSignatureUtil.findMethodBySignature(myClass, getHashCodeSignature(), true);
if (superHashCode != null) {
OverrideImplementUtil.annotateOnOverrideImplement(hashCode, myClass, superHashCode);
}
hashCode = (PsiMethod)myJavaCodeStyleManager.shortenClassReferences(hashCode);
return (PsiMethod)myCodeStyleManager.reformat(hashCode);
}

View File

@@ -1,7 +1,7 @@
class Arrays {
int[] i;
@Override
@java.lang.Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
@@ -13,7 +13,7 @@ class Arrays {
return true;
}
@Override
@java.lang.Override
public int hashCode() {
return i != null ? i.hashCode() : 0;
}

View File

@@ -1,7 +1,7 @@
class Test {
int i;
@Override
@java.lang.Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
@@ -13,7 +13,7 @@ class Test {
return true;
}
@Override
@java.lang.Override
public int hashCode() {
return i;
}

View File

@@ -1,7 +1,7 @@
class Integer {
int i;
@Override
@java.lang.Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
@@ -13,7 +13,7 @@ class Integer {
return true;
}
@Override
@java.lang.Override
public int hashCode() {
return i;
}

View File

@@ -3,7 +3,7 @@ class Test {
class Integer {
int i;
@Override
@java.lang.Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
@@ -15,7 +15,7 @@ class Test {
return true;
}
@Override
@java.lang.Override
public int hashCode() {
return i;
}