From 192c0a3b4566c515a9794f09b74a9bc89737b429 Mon Sep 17 00:00:00 2001 From: Roland Weisleder Date: Mon, 7 Oct 2019 18:00:29 +0200 Subject: [PATCH] Overload JavaClass.getConstructor() and JavaClass.getMethod() 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 --- .../archunit/core/domain/JavaClass.java | 30 +++++++++++++++++++ .../core/importer/ClassFileImporterTest.java | 13 ++++++++ 2 files changed, 43 insertions(+) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java index bad96fb931..810ecb33b1 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java @@ -501,16 +501,36 @@ private Optional tryFindMatchingCodeUnit(Set code return Optional.absent(); } + @PublicAPI(usage = ACCESS) + public JavaMethod getMethod(String name) { + return findMatchingCodeUnit(methods, name, Collections.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 tryGetMethod(String name) { + return tryFindMatchingCodeUnit(methods, name, Collections.emptyList()); + } + @PublicAPI(usage = ACCESS) public Optional tryGetMethod(String name, Class... parameters) { return tryFindMatchingCodeUnit(methods, name, namesOf(parameters)); } + @PublicAPI(usage = ACCESS) + public Optional tryGetMethod(String name, String... parameters) { + return tryFindMatchingCodeUnit(methods, name, ImmutableList.copyOf(parameters)); + } + @PublicAPI(usage = ACCESS) public Set getMethods() { return methods; @@ -522,11 +542,21 @@ public Set getAllMethods() { return allMethods.get(); } + @PublicAPI(usage = ACCESS) + public JavaConstructor getConstructor() { + return findMatchingCodeUnit(constructors, CONSTRUCTOR_NAME, Collections.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 getConstructors() { return constructors; diff --git a/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java b/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java index 3b3bb4a1ba..2c507718f4 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java @@ -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 @@ -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