Skip to content

Commit

Permalink
Overload JavaClass.getConstructor() and JavaClass.getMethod()
Browse files Browse the repository at this point in the history
This commit adds the methods
  JavaClass.getConstructor(String... parameters)
  JavaClass.getMethod(String name, String... parameters)
  JavaClass.tryGetMethod(String name, String... parameters)
to retrieve constructors/methods even if a parameter type is not present
on the classpath.

The methods
  JavaClass.getConstructor()
  JavaClass.getMethod(String name)
  JavaClass.tryGetMethod(String name)
to retrieve constructors/methods without parameters where added to avoid
ambiguous method calls.

Resolves #236

Signed-off-by: Roland Weisleder <roland.weisleder@googlemail.com>
  • Loading branch information
rweisleder authored and codecholeric committed Nov 3, 2019
1 parent c077e0b commit 192c0a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,36 @@ private <T extends JavaCodeUnit> Optional<T> tryFindMatchingCodeUnit(Set<T> code
return Optional.absent();
}

@PublicAPI(usage = ACCESS)
public JavaMethod getMethod(String name) {
return findMatchingCodeUnit(methods, name, Collections.<String>emptyList());
}

@PublicAPI(usage = ACCESS)
public JavaMethod getMethod(String name, Class<?>... parameters) {
return findMatchingCodeUnit(methods, name, namesOf(parameters));
}

@PublicAPI(usage = ACCESS)
public JavaMethod getMethod(String name, String... parameters) {
return findMatchingCodeUnit(methods, name, ImmutableList.copyOf(parameters));
}

@PublicAPI(usage = ACCESS)
public Optional<JavaMethod> tryGetMethod(String name) {
return tryFindMatchingCodeUnit(methods, name, Collections.<String>emptyList());
}

@PublicAPI(usage = ACCESS)
public Optional<JavaMethod> tryGetMethod(String name, Class<?>... parameters) {
return tryFindMatchingCodeUnit(methods, name, namesOf(parameters));
}

@PublicAPI(usage = ACCESS)
public Optional<JavaMethod> tryGetMethod(String name, String... parameters) {
return tryFindMatchingCodeUnit(methods, name, ImmutableList.copyOf(parameters));
}

@PublicAPI(usage = ACCESS)
public Set<JavaMethod> getMethods() {
return methods;
Expand All @@ -522,11 +542,21 @@ public Set<JavaMethod> getAllMethods() {
return allMethods.get();
}

@PublicAPI(usage = ACCESS)
public JavaConstructor getConstructor() {
return findMatchingCodeUnit(constructors, CONSTRUCTOR_NAME, Collections.<String>emptyList());
}

@PublicAPI(usage = ACCESS)
public JavaConstructor getConstructor(Class<?>... parameters) {
return findMatchingCodeUnit(constructors, CONSTRUCTOR_NAME, namesOf(parameters));
}

@PublicAPI(usage = ACCESS)
public JavaConstructor getConstructor(String... parameters) {
return findMatchingCodeUnit(constructors, CONSTRUCTOR_NAME, ImmutableList.copyOf(parameters));
}

@PublicAPI(usage = ACCESS)
public Set<JavaConstructor> getConstructors() {
return constructors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,15 @@ public void imports_complex_method_with_correct_parameters() throws Exception {
assertThat(clazz.getMethod("complex", String.class, long.class, long.class, Serializable.class, Serializable.class))
.isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod(
"complex", String.class, long.class, long.class, Serializable.class, Serializable.class));
assertThat(clazz.tryGetMethod("complex", String.class, long.class, long.class, Serializable.class, Serializable.class).get())
.isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod(
"complex", String.class, long.class, long.class, Serializable.class, Serializable.class));
assertThat(clazz.getMethod("complex", "java.lang.String", "long", "long", "java.io.Serializable", "java.io.Serializable"))
.isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod(
"complex", String.class, long.class, long.class, Serializable.class, Serializable.class));
assertThat(clazz.tryGetMethod("complex", "java.lang.String", "long", "long", "java.io.Serializable", "java.io.Serializable").get())
.isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod(
"complex", String.class, long.class, long.class, Serializable.class, Serializable.class));
}

@Test
Expand Down Expand Up @@ -891,8 +900,12 @@ public void imports_simple_constructors_with_correct_parameters() throws Excepti
assertThat(clazz.getConstructor()).isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor());
assertThat(clazz.getConstructor(Object.class))
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(Object.class));
assertThat(clazz.getConstructor("java.lang.Object"))
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(Object.class));
assertThat(clazz.getConstructor(int.class, int.class))
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(int.class, int.class));
assertThat(clazz.getConstructor("int", "int"))
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(int.class, int.class));
}

@Test
Expand Down

0 comments on commit 192c0a3

Please sign in to comment.