Skip to content

Commit

Permalink
Add ClassInfo.method(String, List<Type>)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Apr 24, 2024
1 parent 9a35bd9 commit 177315c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 20 additions & 2 deletions core/src/main/java/org/jboss/jandex/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,8 @@ final byte[] methodPositionArray() {
* inherited methods. These must be discovered by traversing the class hierarchy.
*
* @param name the name of the method to find
* @param parameters the type parameters of the method
* @return the located method or null if not found
* @param parameters the parameter types of the method
* @return the located method or {@code null} if not found
*/
public final MethodInfo method(String name, Type... parameters) {
MethodInternal key = new MethodInternal(Utils.toUTF8(name), MethodInternal.EMPTY_PARAMETER_NAMES, parameters, null,
Expand All @@ -755,6 +755,24 @@ public final MethodInfo method(String name, Type... parameters) {
return i >= 0 ? new MethodInfo(this, methods[i]) : null;
}

/**
* Retrieves a method based on its signature, which includes a method name and a parameter type list.
* The parameter type list is compared based on the underlying raw types. As an example,
* a generic type parameter {@code T} is considered equal to {@code java.lang.Object}, since the raw form
* of a type variable is its upper bound.
* <p>
* Eligible methods include constructors and static initializer blocks which have the special
* names of {@code <init>} and {@code <clinit>}, respectively. This does not, however, include
* inherited methods. These must be discovered by traversing the class hierarchy.
*
* @param name the name of the method to find
* @param parameters the parameter types of the method
* @return the located method or {@code null} if not found
*/
public final MethodInfo method(String name, List<Type> parameters) {
return method(name, parameters.toArray(Type.EMPTY_ARRAY));
}

/**
* Retrieves the "first" occurrence of a method by the given name. Note that the order of methods
* is not defined, and may change in the future. Therefore, this method should not be used when
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/java/org/jboss/jandex/test/BasicTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -60,6 +61,7 @@
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexReader;
Expand Down Expand Up @@ -635,6 +637,10 @@ private void verifyDummy(Index index, boolean v2features) {
assertEquals("y", method.parameterName(1));
assertEquals("foo", method.parameterName(2));

MethodInfo method2 = clazz.method("doSomething", Arrays.asList(PrimitiveType.INT, PrimitiveType.LONG,
ClassType.create("java.lang.String")));
assertEquals(method, method2);

ClassInfo nested = index.getClassByName(DotName.createSimple(DummyClass.Nested.class.getName()));
assertNotNull(nested);
MethodInfo nestedConstructor1 = nested.method("<init>", PrimitiveType.INT);
Expand Down

0 comments on commit 177315c

Please sign in to comment.