From bc6db9351023ad8b098a668308869598522b76b3 Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Sat, 20 Apr 2024 19:24:50 +0200 Subject: [PATCH] fixup! add possibility to check for overriding methods to domain and lang I think there is still some problems with generics and bridge methods. --- .../archunit/core/domain/JavaMethodTest.java | 69 +++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaMethodTest.java b/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaMethodTest.java index 8eea409ca..a58247deb 100644 --- a/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaMethodTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaMethodTest.java @@ -1,5 +1,7 @@ package com.tngtech.archunit.core.domain; +import java.io.File; + import org.junit.Test; import static com.tngtech.archunit.core.domain.TestUtils.importMethod; @@ -9,27 +11,52 @@ public class JavaMethodTest { @Test public void isOverriding_returns_true_for_implemented_interface_method() { - assertThat(importMethod(SomeClass.class, "doSomething").isOverriding()).isTrue(); + assertThat(importMethod(SomeClass.class, "doSomething").isOverriding()) + .as("method is detected as overriding").isTrue(); } @Test public void isOverriding_returns_false_for_interface_method() { - assertThat(importMethod(SomeInterface.class, "doSomething").isOverriding()).isFalse(); + assertThat(importMethod(SomeInterface.class, "doSomething").isOverriding()) + .as("method is detected as overriding").isFalse(); } @Test public void isOverriding_returns_true_for_overriding_method_of_java_lang_Object() { - assertThat(importMethod(SomeClass.class, "toString").isOverriding()).isTrue(); + assertThat(importMethod(SomeClass.class, "toString").isOverriding()) + .as("method is detected as overriding").isTrue(); } @Test public void isOverriding_returns_false_for_non_overriding_method() { - assertThat(importMethod(SomeClass.class, "doSomethingElse").isOverriding()).isFalse(); + assertThat(importMethod(SomeClass.class, "doSomethingElse").isOverriding()) + .as("method is detected as overriding").isFalse(); } @Test public void isOverriding_returns_false_for_non_overriding_overloaded_method() { - assertThat(importMethod(SomeClass.class, "doSomething", Object.class).isOverriding()).isFalse(); + assertThat(importMethod(SomeClass.class, "doSomething", Object.class).isOverriding()) + .as("method is detected as overriding").isFalse(); + } + + @Test + public void isOverriding_returns_true_for_concrete_implementation_of_generic_interface_method() { + assertThat(importMethod(SomeClassImplementingGenericsConcrete.class, "genericParam", Object.class).isOverriding()) + .as("method is detected as overriding").isTrue(); + assertThat(importMethod(SomeClassImplementingGenericsConcrete.class, "genericParam", String.class).isOverriding()) + .as("method is detected as overriding").isTrue(); + assertThat(importMethod(SomeClassImplementingGenericsConcrete.class, "genericReturnType").isOverriding()) + .as("method is detected as overriding").isTrue(); + } + + @Test + public void isOverriding_returns_true_for_generic_implementation_of_generic_interface_method() { + assertThat(importMethod(SomeClassImplementingGenericsGeneric.class, "genericParam", Object.class).isOverriding()) + .as("method is detected as overriding").isTrue(); + assertThat(importMethod(SomeClassImplementingGenericsConcrete.class, "genericParam", String.class).isOverriding()) + .as("method is detected as overriding").isTrue(); + assertThat(importMethod(SomeClassImplementingGenericsGeneric.class, "genericReturnType").isOverriding()) + .as("method is detected as overriding").isTrue(); } @SuppressWarnings("unused") @@ -56,4 +83,36 @@ public String toString() { return super.toString(); } } + + @SuppressWarnings("unused") + private interface SomeGenericInterface { + + void genericParam(T param); + + U genericReturnType(); + } + + private static class SomeClassImplementingGenericsConcrete implements SomeGenericInterface { + + @Override + public void genericParam(String param) { + } + + @Override + public File genericReturnType() { + return null; + } + } + + private static class SomeClassImplementingGenericsGeneric implements SomeGenericInterface { + + @Override + public void genericParam(X param) { + } + + @Override + public Y genericReturnType() { + return null; + } + } }