Skip to content

Commit

Permalink
Merge pull request #246 from rweisleder/gh-236
Browse files Browse the repository at this point in the history
Overload JavaClass.getConstructor() and JavaClass.getMethod()
  • Loading branch information
codecholeric authored Nov 3, 2019
2 parents c077e0b + 1e2401c commit 40625a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 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 @@ -178,6 +178,7 @@
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.apache.logging.log4j.Level;
import org.assertj.core.api.Condition;
import org.assertj.core.util.Objects;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -663,9 +664,14 @@ public void imports_complex_method_with_correct_parameters() throws Exception {
JavaClass clazz = classesIn("testexamples/complexmethodimport").get(ClassWithComplexMethod.class);

assertThat(clazz.getMethods()).as("Methods of %s", ClassWithComplexMethod.class.getSimpleName()).hasSize(1);
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));

Class<?>[] parameterTypes = {String.class, long.class, long.class, Serializable.class, Serializable.class};
Method expectedMethod = ClassWithComplexMethod.class.getDeclaredMethod("complex", parameterTypes);

assertThat(clazz.getMethod("complex", parameterTypes)).isEquivalentTo(expectedMethod);
assertThat(clazz.tryGetMethod("complex", parameterTypes).get()).isEquivalentTo(expectedMethod);
assertThat(clazz.getMethod("complex", Objects.namesOf(parameterTypes))).isEquivalentTo(expectedMethod);
assertThat(clazz.tryGetMethod("complex", Objects.namesOf(parameterTypes)).get()).isEquivalentTo(expectedMethod);
}

@Test
Expand Down Expand Up @@ -889,10 +895,16 @@ public void imports_simple_constructors_with_correct_parameters() throws Excepti

assertThat(clazz.getConstructors()).as("Constructors").hasSize(3);
assertThat(clazz.getConstructor()).isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor());
assertThat(clazz.getConstructor(Object.class))
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(Object.class));
assertThat(clazz.getConstructor(int.class, int.class))
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(int.class, int.class));

Class<?>[] parameterTypes = {Object.class};
Constructor<ClassWithSimpleConstructors> expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes);
assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor);
assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor);

parameterTypes = new Class[]{int.class, int.class};
expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes);
assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor);
assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor);
}

@Test
Expand Down

0 comments on commit 40625a3

Please sign in to comment.