Skip to content

Commit 40625a3

Browse files
authored
Merge pull request #246 from rweisleder/gh-236
Overload JavaClass.getConstructor() and JavaClass.getMethod()
2 parents c077e0b + 1e2401c commit 40625a3

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java

+30
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,36 @@ private <T extends JavaCodeUnit> Optional<T> tryFindMatchingCodeUnit(Set<T> code
501501
return Optional.absent();
502502
}
503503

504+
@PublicAPI(usage = ACCESS)
505+
public JavaMethod getMethod(String name) {
506+
return findMatchingCodeUnit(methods, name, Collections.<String>emptyList());
507+
}
508+
504509
@PublicAPI(usage = ACCESS)
505510
public JavaMethod getMethod(String name, Class<?>... parameters) {
506511
return findMatchingCodeUnit(methods, name, namesOf(parameters));
507512
}
508513

514+
@PublicAPI(usage = ACCESS)
515+
public JavaMethod getMethod(String name, String... parameters) {
516+
return findMatchingCodeUnit(methods, name, ImmutableList.copyOf(parameters));
517+
}
518+
519+
@PublicAPI(usage = ACCESS)
520+
public Optional<JavaMethod> tryGetMethod(String name) {
521+
return tryFindMatchingCodeUnit(methods, name, Collections.<String>emptyList());
522+
}
523+
509524
@PublicAPI(usage = ACCESS)
510525
public Optional<JavaMethod> tryGetMethod(String name, Class<?>... parameters) {
511526
return tryFindMatchingCodeUnit(methods, name, namesOf(parameters));
512527
}
513528

529+
@PublicAPI(usage = ACCESS)
530+
public Optional<JavaMethod> tryGetMethod(String name, String... parameters) {
531+
return tryFindMatchingCodeUnit(methods, name, ImmutableList.copyOf(parameters));
532+
}
533+
514534
@PublicAPI(usage = ACCESS)
515535
public Set<JavaMethod> getMethods() {
516536
return methods;
@@ -522,11 +542,21 @@ public Set<JavaMethod> getAllMethods() {
522542
return allMethods.get();
523543
}
524544

545+
@PublicAPI(usage = ACCESS)
546+
public JavaConstructor getConstructor() {
547+
return findMatchingCodeUnit(constructors, CONSTRUCTOR_NAME, Collections.<String>emptyList());
548+
}
549+
525550
@PublicAPI(usage = ACCESS)
526551
public JavaConstructor getConstructor(Class<?>... parameters) {
527552
return findMatchingCodeUnit(constructors, CONSTRUCTOR_NAME, namesOf(parameters));
528553
}
529554

555+
@PublicAPI(usage = ACCESS)
556+
public JavaConstructor getConstructor(String... parameters) {
557+
return findMatchingCodeUnit(constructors, CONSTRUCTOR_NAME, ImmutableList.copyOf(parameters));
558+
}
559+
530560
@PublicAPI(usage = ACCESS)
531561
public Set<JavaConstructor> getConstructors() {
532562
return constructors;

archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
import com.tngtech.java.junit.dataprovider.UseDataProvider;
179179
import org.apache.logging.log4j.Level;
180180
import org.assertj.core.api.Condition;
181+
import org.assertj.core.util.Objects;
181182
import org.junit.After;
182183
import org.junit.Rule;
183184
import org.junit.Test;
@@ -663,9 +664,14 @@ public void imports_complex_method_with_correct_parameters() throws Exception {
663664
JavaClass clazz = classesIn("testexamples/complexmethodimport").get(ClassWithComplexMethod.class);
664665

665666
assertThat(clazz.getMethods()).as("Methods of %s", ClassWithComplexMethod.class.getSimpleName()).hasSize(1);
666-
assertThat(clazz.getMethod("complex", String.class, long.class, long.class, Serializable.class, Serializable.class))
667-
.isEquivalentTo(ClassWithComplexMethod.class.getDeclaredMethod(
668-
"complex", String.class, long.class, long.class, Serializable.class, Serializable.class));
667+
668+
Class<?>[] parameterTypes = {String.class, long.class, long.class, Serializable.class, Serializable.class};
669+
Method expectedMethod = ClassWithComplexMethod.class.getDeclaredMethod("complex", parameterTypes);
670+
671+
assertThat(clazz.getMethod("complex", parameterTypes)).isEquivalentTo(expectedMethod);
672+
assertThat(clazz.tryGetMethod("complex", parameterTypes).get()).isEquivalentTo(expectedMethod);
673+
assertThat(clazz.getMethod("complex", Objects.namesOf(parameterTypes))).isEquivalentTo(expectedMethod);
674+
assertThat(clazz.tryGetMethod("complex", Objects.namesOf(parameterTypes)).get()).isEquivalentTo(expectedMethod);
669675
}
670676

671677
@Test
@@ -889,10 +895,16 @@ public void imports_simple_constructors_with_correct_parameters() throws Excepti
889895

890896
assertThat(clazz.getConstructors()).as("Constructors").hasSize(3);
891897
assertThat(clazz.getConstructor()).isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor());
892-
assertThat(clazz.getConstructor(Object.class))
893-
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(Object.class));
894-
assertThat(clazz.getConstructor(int.class, int.class))
895-
.isEquivalentTo(ClassWithSimpleConstructors.class.getDeclaredConstructor(int.class, int.class));
898+
899+
Class<?>[] parameterTypes = {Object.class};
900+
Constructor<ClassWithSimpleConstructors> expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes);
901+
assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor);
902+
assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor);
903+
904+
parameterTypes = new Class[]{int.class, int.class};
905+
expectedConstructor = ClassWithSimpleConstructors.class.getDeclaredConstructor(parameterTypes);
906+
assertThat(clazz.getConstructor(parameterTypes)).isEquivalentTo(expectedConstructor);
907+
assertThat(clazz.getConstructor(Objects.namesOf(parameterTypes))).isEquivalentTo(expectedConstructor);
896908
}
897909

898910
@Test

0 commit comments

Comments
 (0)