Skip to content

Commit 4b0b224

Browse files
MikeEdgarLadicek
authored andcommitted
Support lookup of method based on an example method
Signed-off-by: Michael Edgar <[email protected]>
1 parent 13b07f6 commit 4b0b224

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

core/src/main/java/org/jboss/jandex/ClassInfo.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,35 @@ final byte[] methodPositionArray() {
832832
return methodPositions;
833833
}
834834

835+
final MethodInfo method(MethodInternal key) {
836+
int i = Arrays.binarySearch(methods, key, MethodInternal.NAME_AND_PARAMETER_COMPONENT_COMPARATOR);
837+
return i >= 0 ? new MethodInfo(this, methods[i]) : null;
838+
}
839+
840+
/**
841+
* Retrieves a method that has the same signature as the given {@code other} method. The signature includes
842+
* method name and parameter types; it does <em>not</em> include return type, type parameters or anything else.
843+
* The parameter types are compared based on the underlying raw types. For example, an unbounded type parameter
844+
* {@code T} is considered equal to {@code java.lang.Object}, since the raw form of a type variable is
845+
* its upper bound.
846+
* <p>
847+
* Eligible methods include constructors and static initializers, which have the special names
848+
* of {@code <init>} and {@code <clinit>}, respectively. Eligible methods do <em>not</em> include
849+
* inherited methods. These must be discovered by traversing the class hierarchy.
850+
* <p>
851+
* This method may be useful for retrieving overridden or overriding methods, but note that the definition
852+
* of method override is more complex; notably, it includes method visibility. For example, a {@code private}
853+
* method in a superclass is not overridden by a method with the same signature in a subclass.
854+
*
855+
* @param other method instance having the same signature to retrieve from this ClassInfo
856+
* @return the located method or {@code null} if not found
857+
*
858+
* @since 3.5.2
859+
*/
860+
public final MethodInfo method(MethodInfo other) {
861+
return method(other.methodInternal());
862+
}
863+
835864
/**
836865
* Retrieves a method based on its signature, which includes a method name and a parameter type list.
837866
* The parameter type list is compared based on the underlying raw types. As an example,
@@ -849,8 +878,7 @@ final byte[] methodPositionArray() {
849878
public final MethodInfo method(String name, Type... parameters) {
850879
MethodInternal key = new MethodInternal(Utils.toUTF8(name), MethodInternal.EMPTY_PARAMETER_NAMES, parameters, null,
851880
(short) 0);
852-
int i = Arrays.binarySearch(methods, key, MethodInternal.NAME_AND_PARAMETER_COMPONENT_COMPARATOR);
853-
return i >= 0 ? new MethodInfo(this, methods[i]) : null;
881+
return method(key);
854882
}
855883

856884
/**
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.jboss.jandex.test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNotSame;
6+
7+
import java.util.Arrays;
8+
9+
import org.jboss.jandex.ClassInfo;
10+
import org.jboss.jandex.Index;
11+
import org.jboss.jandex.MethodInfo;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class MethodRetrievalByMethodTest {
15+
static class Grandparent {
16+
public void doSomething(String p1, int p2) {
17+
}
18+
19+
public void doSomething(Object p) {
20+
}
21+
22+
private Number hello() {
23+
return 1;
24+
}
25+
}
26+
27+
static class Parent extends Grandparent {
28+
@Override
29+
public void doSomething(String p1, int p2) {
30+
}
31+
32+
public void doSomething(int p1, String p2) {
33+
}
34+
35+
private Integer hello() {
36+
return 2;
37+
}
38+
}
39+
40+
static class Child extends Parent {
41+
@Override
42+
public void doSomething(String p1, int p2) {
43+
}
44+
45+
private Long hello() {
46+
return 3L;
47+
}
48+
}
49+
50+
@Test
51+
void test() throws Exception {
52+
Index index = Index.of(Grandparent.class, Parent.class, Child.class);
53+
54+
ClassInfo childClazz = index.getClassByName(Child.class);
55+
56+
MethodInfo doSomething = childClazz.firstMethod("doSomething");
57+
MethodInfo hello = childClazz.firstMethod("hello");
58+
59+
for (Class<?> ancestor : Arrays.asList(Parent.class, Grandparent.class)) {
60+
ClassInfo ancestorClazz = index.getClassByName(ancestor);
61+
62+
MethodInfo ancestorDoSomething = ancestorClazz.method(doSomething);
63+
assertNotNull(ancestorDoSomething);
64+
assertNotSame(doSomething, ancestorDoSomething);
65+
assertEquals("doSomething", ancestorDoSomething.name());
66+
assertEquals(String.class.getName(), ancestorDoSomething.parameterType(0).name().toString());
67+
assertEquals("int", ancestorDoSomething.parameterType(1).name().toString());
68+
69+
MethodInfo ancestorHello = ancestorClazz.method(hello);
70+
assertNotNull(ancestorHello);
71+
assertNotSame(hello, ancestorHello);
72+
assertEquals("hello", ancestorHello.name());
73+
assertEquals(0, ancestorHello.parametersCount());
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)