generate equals/hashCode: pass wildcards as type arguments when needed (IDEA-127939)

This commit is contained in:
Anna Kozlova
2015-01-13 13:34:10 +01:00
parent 04b8cf4aa5
commit 5373392483
6 changed files with 64 additions and 7 deletions
@@ -4,7 +4,22 @@
#if ($settings.generateFinalLocals)
final ##
#end
$classname $classInstanceName = ($classname)$paramName;
#set($wildcards = "#generateWildcards()")
$classname$wildcards $classInstanceName = ($classname$wildcards)$paramName;
#end
##
#macro(generateWildcards)
#if ($class.typeParams > 0)
<##
#set( $arr = [1..$class.typeParams] )
#foreach( $i in $arr )
#if ($i > 1)
, ##
#end
?##
#end
>##
#end
#end
##
#macro(addEqualsPrologue)
@@ -0,0 +1,23 @@
import java.util.Arrays;
class A<T extends String, K> {
Object[] a1;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final A<?, ?> a = (A<?, ?>) o;
// Probably incorrect - comparing Object[] arrays with Arrays.equals
if (!Arrays.equals(a1, a.a1)) return false;
return true;
}
@Override
public int hashCode() {
return Arrays.hashCode(a1);
}
}
@@ -0,0 +1,5 @@
class A<T extends String, K> {
Object[] a1;
<caret>
}
@@ -24,11 +24,11 @@ public class GenerateEquals15Test extends GenerateEqualsTestCase {
public void testDifferentTypesGetters() throws Exception {
doTest(Function.ID, Function.ID, new Function<PsiField[], PsiField[]>() {
@Override
public PsiField[] fun(PsiField[] fields) {
return new PsiField[0];
}
}, true, true);
@Override
public PsiField[] fun(PsiField[] fields) {
return new PsiField[0];
}
}, true, true);
}
public void testDifferentTypesAllNotNull() throws Exception {
@@ -47,6 +47,10 @@ public class GenerateEquals15Test extends GenerateEqualsTestCase {
doTest(Function.ID, Function.ID, Function.ID, true);
}
public void testClassWithTypeParams() throws Exception {
doTest(Function.ID, Function.ID, Function.ID, true);
}
public void testDifferentTypesSuperEqualsAndHashCodeApache3() throws Exception {
doTestWithTemplate(EqualsHashCodeTemplatesManager.EQUALS_HASH_CODE_BUILDER_APACHE_COMMONS_LANG_3);
}
@@ -36,8 +36,9 @@ public class ClassElement {
private boolean _enum;
private boolean exception;
private boolean _abstract;
private int myTypeParams;
/**
/**
* Does the class implement the given interface?
* <p/>
* The name should <b>not</b> be the qualified name.
@@ -169,4 +170,12 @@ public class ClassElement {
", abstract=" + _abstract +
"}";
}
public void setTypeParams(int typeParams) {
myTypeParams = typeParams;
}
public int getTypeParams() {
return myTypeParams;
}
}
@@ -57,6 +57,7 @@ public class ElementFactory {
ce.setDeprecated(clazz.isDeprecated());
ce.setException(PsiAdapter.isExceptionClass(clazz));
ce.setAbstract(clazz.hasModifierProperty(PsiModifier.ABSTRACT));
ce.setTypeParams(clazz.getTypeParameters().length);
return ce;
}