Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClassInfo.method(String, List<Type>) #360

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading