Skip to content

Commit

Permalink
Merge branch 'release/1.13'
Browse files Browse the repository at this point in the history
  • Loading branch information
jtnelson committed Oct 1, 2022
2 parents 1a760ba + 902164a commit 5e40c00
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.13.0
1.13.1
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

#### Version 1.13.1 (October 1, 2022)
* Fixed a bug that caused a `NoSuchMethodException` to be thrown when using the `Reflection` utility to `call` a non-overriden interface-defined default method.

#### Version 1.13.0 (September 5, 2022)
* Added the `ByteBuffers#share` method that returns a new ByteBuffer containing a shared subseqence of a source `ByteBuffer` while incrementing the `position` of the source the same number of bytes that are shared.
* Upgraded `logback` dependency to version `1.2.11`
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/cinchapi/common/reflect/Reflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -968,8 +970,10 @@ private static Method getMethod(@Nullable Object[] args,
Class<?>... paramTypes) {
List<Method> potential = Lists.newArrayListWithCapacity(1);
List<Method> deferred = Lists.newArrayListWithCapacity(1);
Deque<Class<?>> queue = new ArrayDeque<>();
queue.add(clazz);
try {
while (clazz != null) {
while ((clazz = queue.poll()) != null) {
for (Method method : Arrays.stream(clazz.getDeclaredMethods())
.filter(method -> method.getName().equals(name))
.collect(Collectors.toList())) {
Expand All @@ -989,7 +993,15 @@ else if(callable != TernaryTruth.FALSE
}
}
if(potential.isEmpty()) {
clazz = clazz.getSuperclass();
Class<?> superClass = clazz.getSuperclass();
if(superClass != null) {
queue.add(superClass);
}
for (Class<?> iface : clazz.getInterfaces()) {
// Account for default interface methods that are not
// explicitly overridden in the the #clazz.
queue.add(iface);
}
}
else {
break;
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/com/cinchapi/common/reflect/ReflectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,15 @@ public void testCallOverrideWithGenericParameter() {
Reflection.call(obj, "put", "Company", "Cinchapi");
Assert.assertTrue(true); // lack of Exception means we pass
}

@Test
public void testCallDefaultInterfaceMethod() {
ClassB obj = new ClassB();
String expected = "Jeff Nelson";
String actual = Reflection.call(obj, "foo", expected);
Assert.assertEquals("foo_"+expected, actual);
Assert.assertEquals("baz", Reflection.call(obj, "baz", expected));
}

private static class A {

Expand Down Expand Up @@ -585,5 +594,41 @@ public <T> void put(String key, T value) {

}
}

class ClassA implements InterfaceA, InterfaceB {


@Override
public String baz(String value) {
return value;
}

}

class ClassB extends ClassA {

@Override
public String baz(String value) {
return "baz";
}
}

interface InterfaceA {

public String baz(String value);

public default String foo(String value) {
return "foo_"+value;
}

}

interface InterfaceB {
public default String bar(String value) {
return "bar_"+value;
}

public String baz(String value);
}

}

0 comments on commit 5e40c00

Please sign in to comment.