Skip to content
Permalink
Browse files
GROOVY-10214, GROOVY-5453: order category methods by self-type distances
  • Loading branch information
eric-milles committed Apr 22, 2022
1 parent 22d1190 commit c880ef0218b3cc85efc9ae8bff2458f92bc89d32
Showing 2 changed files with 50 additions and 5 deletions.
@@ -2294,9 +2294,20 @@ private static CategoryMethod getCategoryMethodSetter(final Class<?> sender, fin
private static CategoryMethod findCategoryMethod(final String name, final Class<?> sender, final java.util.function.Predicate<CachedClass[]> paramFilter) {
List<CategoryMethod> categoryMethods = GroovyCategorySupport.getCategoryMethods(name);
if (categoryMethods != null) {
return categoryMethods.stream().filter(categoryMethod ->
categoryMethod.getDeclaringClass().isAssignableFrom(sender) && paramFilter.test(categoryMethod.getParameterTypes())
).sorted().findFirst().orElse(null);
List<CategoryMethod> choices = new ArrayList<>();
for (CategoryMethod categoryMethod : categoryMethods) {
if (categoryMethod.getOwnerClass().isAssignableFrom(sender)
&& paramFilter.test(categoryMethod.getParameterTypes())) {
choices.add(categoryMethod);
}
}
if (!choices.isEmpty()) {
if (choices.size() > 1) { // GROOVY-5453, GROOVY-10214: order by self-type distance
choices.sort(Comparator.comparingLong(m -> MetaClassHelper.calculateParameterDistance(
new Class[]{sender}, new ParameterTypes(new CachedClass[]{m.getOwnerClass()}))));
}
return choices.get(0);
}
}
return null;
}
@@ -22,7 +22,8 @@ import groovy.test.GroovyTestCase

final class CategoryTest extends GroovyTestCase {

void setUp() {
@Override
protected void setUp() {
def dummy = null
CategoryTestPropertyCategory.setSomething(dummy, 'hello')
CategoryTestHelperPropertyReplacer.setaProperty(dummy, 'anotherValue')
@@ -280,7 +281,7 @@ final class CategoryTest extends GroovyTestCase {
}

// GROOVY-5453
void testOverloadedGetterMethod() {
void testOverloadedGetterMethod1() {
assertScript '''
class Cat {
static getFoo(String s) {'String'}
@@ -293,6 +294,39 @@ final class CategoryTest extends GroovyTestCase {
'''
}

// GROOVY-10214
void testOverloadedGetterMethod2() {
assertScript '''
class Cat {
static String getFoo(Boolean self) {
'Boolean'
}
static String getFoo(Byte self) {
'Byte'
}
static String getFoo(Short self) {
'Short'
}
static String getFoo(Integer self) {
'Integer'
}
static String getFoo(Long self) {
'Long'
}
static String getFoo(Float self) {
'Float'
}
static String getFoo(Double self) {
'Double'
}
}
use (Cat) {
assert 123.foo == 'Integer'
assert 4.5d.foo == 'Double'
}
'''
}

// GROOVY-3867
void testPropertyMissing() {
def x = new X()

0 comments on commit c880ef0

Please sign in to comment.