From 380958a54bbf972225270d1e8c5ec5d464bea39a Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Fri, 6 Jan 2023 14:27:59 +0530 Subject: [PATCH 01/11] Added Method#isOverridden --- .../archunit/core/domain/JavaMethod.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index 58bcc603a8..3412a90591 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -15,13 +15,6 @@ */ package com.tngtech.archunit.core.domain; -import java.lang.reflect.Method; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.function.Function; -import java.util.function.Supplier; - import com.tngtech.archunit.PublicAPI; import com.tngtech.archunit.base.ArchUnitException.InconsistentClassPathException; import com.tngtech.archunit.base.MayResolveTypesViaReflection; @@ -29,6 +22,14 @@ import com.tngtech.archunit.base.Suppliers; import com.tngtech.archunit.core.importer.DomainBuilders; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Supplier; + import static com.google.common.collect.Sets.union; import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; import static com.tngtech.archunit.core.domain.Formatters.formatMethod; @@ -125,6 +126,12 @@ public String getDescription() { return "Method <" + getFullName() + ">"; } + @PublicAPI(usage = ACCESS) + public boolean isOverridden() { + Method method = methodSupplier.get(); + return Arrays.asList(method.getDeclaringClass().getSuperclass().getDeclaredMethods()).contains(method); + } + @ResolvesTypesViaReflection @MayResolveTypesViaReflection(reason = "Just part of a bigger resolution process") private class ReflectMethodSupplier implements Supplier { From e569b0e304e2b60db2eb3d333aac46b1aab66f39 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sat, 7 Jan 2023 16:31:25 +0530 Subject: [PATCH 02/11] Fixed the method and added a test --- .../archunit/core/domain/JavaMethod.java | 6 +-- .../archunit/core/domain/JavaMethodTest.java | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 archunit/src/test/java/com/tngtech/archunit/core/domain/JavaMethodTest.java diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index 3412a90591..1080ec5f62 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 TNG Technology Consulting GmbH + * Copyright 2014-2023 TNG Technology Consulting GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -128,8 +128,8 @@ public String getDescription() { @PublicAPI(usage = ACCESS) public boolean isOverridden() { - Method method = methodSupplier.get(); - return Arrays.asList(method.getDeclaringClass().getSuperclass().getDeclaredMethods()).contains(method); + Class[] reflectedParameters = reflect(getRawParameterTypes()); + return Arrays.stream(reflect().getDeclaringClass().getSuperclass().getDeclaredMethods()).anyMatch(superMethod -> superMethod.getName().equals(getName()) && Arrays.equals(superMethod.getParameterTypes(), reflectedParameters)); } @ResolvesTypesViaReflection 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 new file mode 100644 index 0000000000..e27eb79653 --- /dev/null +++ b/archunit/src/test/java/com/tngtech/archunit/core/domain/JavaMethodTest.java @@ -0,0 +1,39 @@ +package com.tngtech.archunit.core.domain; + + +import com.tngtech.archunit.core.importer.ClassFileImporter; +import org.junit.Test; + +import static com.tngtech.archunit.testutil.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class JavaMethodTest { + @Test + public void isOverriddenTest() { + class Base { + void method1() { + } + + void method1(int x) { + } + } + class Child extends Base { + void method1() { + } + + void method2() { + } + } + + JavaClass childClass = new ClassFileImporter().importClass(Child.class); + JavaClass baseClass = new ClassFileImporter().importClass(Base.class); + JavaMethod childMethod1 = childClass.getMethod("method1"); + JavaMethod baseMethod1 = baseClass.getMethod("method1"); + assertNotEquals(childMethod1, baseMethod1); + assertEquals(childMethod1.getName(), baseMethod1.getName()); + assertThat(childClass.getMethod("method1").isOverridden()).isTrue(); + assertThat(childClass.getMethod("method1", int.class).isOverridden()).isFalse(); + assertThat(childClass.getMethod("method2").isOverridden()).isFalse(); + } +} \ No newline at end of file From 9f586510309ca23444c50a500adc7cd6b98b59c9 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Jan 2023 13:54:21 +0530 Subject: [PATCH 03/11] Added support for methods in grandparent class and used ArchUnit API instead of reflection --- .../java/com/tngtech/archunit/core/domain/JavaMethod.java | 7 ++++--- .../com/tngtech/archunit/core/domain/JavaMethodTest.java | 3 --- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index 1080ec5f62..32138cf1c4 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -23,7 +23,6 @@ import com.tngtech.archunit.core.importer.DomainBuilders; import java.lang.reflect.Method; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.Set; @@ -128,8 +127,10 @@ public String getDescription() { @PublicAPI(usage = ACCESS) public boolean isOverridden() { - Class[] reflectedParameters = reflect(getRawParameterTypes()); - return Arrays.stream(reflect().getDeclaringClass().getSuperclass().getDeclaredMethods()).anyMatch(superMethod -> superMethod.getName().equals(getName()) && Arrays.equals(superMethod.getParameterTypes(), reflectedParameters)); + return getOwner().getAllRawSuperclasses().stream() + .map(JavaClass::getAllMethods) + .flatMap(Set::stream) + .anyMatch(superMethod -> superMethod.getName().equals(getName()) && superMethod.getParameters().equals(getParameters())); } @ResolvesTypesViaReflection 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 e27eb79653..07fe6ee061 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 @@ -30,10 +30,7 @@ void method2() { JavaClass baseClass = new ClassFileImporter().importClass(Base.class); JavaMethod childMethod1 = childClass.getMethod("method1"); JavaMethod baseMethod1 = baseClass.getMethod("method1"); - assertNotEquals(childMethod1, baseMethod1); - assertEquals(childMethod1.getName(), baseMethod1.getName()); assertThat(childClass.getMethod("method1").isOverridden()).isTrue(); - assertThat(childClass.getMethod("method1", int.class).isOverridden()).isFalse(); assertThat(childClass.getMethod("method2").isOverridden()).isFalse(); } } \ No newline at end of file From 3e97e3f931717ec1cae139e2dd558e61d75c6d3c Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Jan 2023 14:11:41 +0530 Subject: [PATCH 04/11] Fixed issue with `isOverridden` I was checking if the parameters are equal by using `getParameters().equals()` which did not work as `JavaParameter#equals` checks for equality of the owner(in this case, super method) as well. `JavaType#equals` only checks for equality of the actual parameter type regardless of source, and hence works. --- .../main/java/com/tngtech/archunit/core/domain/JavaMethod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index 32138cf1c4..3dd330b61a 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -130,7 +130,7 @@ public boolean isOverridden() { return getOwner().getAllRawSuperclasses().stream() .map(JavaClass::getAllMethods) .flatMap(Set::stream) - .anyMatch(superMethod -> superMethod.getName().equals(getName()) && superMethod.getParameters().equals(getParameters())); + .anyMatch(superMethod -> superMethod.getName().equals(getName()) && superMethod.getParameterTypes().equals(getParameterTypes())); } @ResolvesTypesViaReflection From 3ea2f3664baa6a6039d259cfba0d9ab28fcf8db9 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Jan 2023 14:16:21 +0530 Subject: [PATCH 05/11] Added testing for overriden method in grandparent class --- .../archunit/core/domain/JavaMethodTest.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 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 07fe6ee061..af1bbeb0ff 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 @@ -25,12 +25,36 @@ void method1() { void method2() { } } + class GrandChild extends Child { + void method1() { + + } + + void method1(int x) { + + } - JavaClass childClass = new ClassFileImporter().importClass(Child.class); - JavaClass baseClass = new ClassFileImporter().importClass(Base.class); - JavaMethod childMethod1 = childClass.getMethod("method1"); - JavaMethod baseMethod1 = baseClass.getMethod("method1"); + void method2() { + + } + + void method3() { + + } + + } + ClassFileImporter importer = new ClassFileImporter(); + JavaClass baseClass = importer.importClass(Base.class); + JavaClass childClass = importer.importClass(Child.class); + JavaClass grandChildClass = importer.importClass(GrandChild.class); + assertThat(baseClass.getMethod("method1").isOverridden()).isFalse(); + assertThat(baseClass.getMethod("method1", int.class).isOverridden()).isFalse(); assertThat(childClass.getMethod("method1").isOverridden()).isTrue(); assertThat(childClass.getMethod("method2").isOverridden()).isFalse(); + assertThat(grandChildClass.getMethod("method1").isOverridden()).isTrue(); + assertThat(grandChildClass.getMethod("method1", int.class).isOverridden()).isTrue(); + assertThat(grandChildClass.getMethod("method2").isOverridden()).isTrue(); + assertThat(grandChildClass.getMethod("method3").isOverridden()).isFalse(); + //TODO add testing for methods with generic parameters } } \ No newline at end of file From 9e74551dd852f4788c7509841e11be2596dcaa30 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Jan 2023 20:09:59 +0530 Subject: [PATCH 06/11] Undo accidental copyright change --- .../main/java/com/tngtech/archunit/core/domain/JavaMethod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index 3dd330b61a..70ca831450 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2023 TNG Technology Consulting GmbH + * Copyright 2014-2022 TNG Technology Consulting GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From c9e63f63199e6559fedb739564ca68ce03a1d13d Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Jan 2023 20:11:49 +0530 Subject: [PATCH 07/11] Check for super methods in interfaces as well --- .../main/java/com/tngtech/archunit/core/domain/JavaMethod.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index 70ca831450..9c73b43ccf 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -28,6 +28,7 @@ import java.util.Set; import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Stream; import static com.google.common.collect.Sets.union; import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; @@ -127,7 +128,7 @@ public String getDescription() { @PublicAPI(usage = ACCESS) public boolean isOverridden() { - return getOwner().getAllRawSuperclasses().stream() + return Stream.concat(getOwner().getAllRawSuperclasses().stream(), getOwner().getAllRawInterfaces().stream()) .map(JavaClass::getAllMethods) .flatMap(Set::stream) .anyMatch(superMethod -> superMethod.getName().equals(getName()) && superMethod.getParameterTypes().equals(getParameterTypes())); From 02eae706a81421382964224d347af099f8b1579d Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sun, 8 Jan 2023 20:13:24 +0530 Subject: [PATCH 08/11] Import fix --- .../tngtech/archunit/core/domain/JavaMethod.java | 14 +++++++------- .../archunit/core/domain/JavaMethodTest.java | 2 -- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index 9c73b43ccf..1120067dc9 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -15,13 +15,6 @@ */ package com.tngtech.archunit.core.domain; -import com.tngtech.archunit.PublicAPI; -import com.tngtech.archunit.base.ArchUnitException.InconsistentClassPathException; -import com.tngtech.archunit.base.MayResolveTypesViaReflection; -import com.tngtech.archunit.base.ResolvesTypesViaReflection; -import com.tngtech.archunit.base.Suppliers; -import com.tngtech.archunit.core.importer.DomainBuilders; - import java.lang.reflect.Method; import java.util.List; import java.util.Optional; @@ -30,6 +23,13 @@ import java.util.function.Supplier; import java.util.stream.Stream; +import com.tngtech.archunit.PublicAPI; +import com.tngtech.archunit.base.ArchUnitException.InconsistentClassPathException; +import com.tngtech.archunit.base.MayResolveTypesViaReflection; +import com.tngtech.archunit.base.ResolvesTypesViaReflection; +import com.tngtech.archunit.base.Suppliers; +import com.tngtech.archunit.core.importer.DomainBuilders; + import static com.google.common.collect.Sets.union; import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; import static com.tngtech.archunit.core.domain.Formatters.formatMethod; 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 af1bbeb0ff..1c8c9bcd6b 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 @@ -5,8 +5,6 @@ import org.junit.Test; import static com.tngtech.archunit.testutil.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; public class JavaMethodTest { @Test From d7c3481f5c1e5ebc585e245803bf4adbd20f4fa8 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Wed, 17 May 2023 19:42:03 +0530 Subject: [PATCH 09/11] Got this so far --- .../archunit/core/domain/JavaMethod.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index fa3cc82a0f..cb0f01d2c9 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -131,7 +131,26 @@ public boolean isOverridden() { return Stream.concat(getOwner().getAllRawSuperclasses().stream(), getOwner().getAllRawInterfaces().stream()) .map(JavaClass::getAllMethods) .flatMap(Set::stream) - .anyMatch(superMethod -> superMethod.getName().equals(getName()) && superMethod.getParameterTypes().equals(getParameterTypes())); + .anyMatch(superMethod -> { + if(!superMethod.getName().equals(getName())){ + return false; + } + List> superClassTypeParameters = superMethod.getOwner().getTypeParameters(); + List parameterTypes = getParameters(); + List superMethodParameterTypes = superMethod.getParameters(); + if(parameterTypes.size() != superMethodParameterTypes.size()){ + return false; + } + for(int i = 0; i < parameterTypes.size(); i++){ + JavaParameter parameter = parameterTypes.get(i); + JavaParameter superParameter = superMethodParameterTypes.get(i); + if(!parameter.equals(superParameter)){ + return false; + } + //TODO Somehow find type arguements of extension to the super class(cannot be done with raw supperclass) + } + return true; + }); } @ResolvesTypesViaReflection From 2acc5d6d4844c860c10e22e0b39b2e258ed7b062 Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Wed, 17 May 2023 19:43:41 +0530 Subject: [PATCH 10/11] Got this so far --- .../archunit/core/domain/JavaMethodTest.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 1c8c9bcd6b..53d1832d37 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 @@ -4,10 +4,12 @@ import com.tngtech.archunit.core.importer.ClassFileImporter; import org.junit.Test; +import java.util.stream.Collectors; + import static com.tngtech.archunit.testutil.Assertions.assertThat; public class JavaMethodTest { - @Test + //@Test public void isOverriddenTest() { class Base { void method1() { @@ -55,4 +57,23 @@ void method3() { assertThat(grandChildClass.getMethod("method3").isOverridden()).isFalse(); //TODO add testing for methods with generic parameters } + @Test + public void overridden_generic_methods_are_supported() { + class Parent { + void method(T t) { } + } + class Child extends Parent { + @Override + void method(Integer t) { } + + } + ClassFileImporter classFileImporter = new ClassFileImporter(); + JavaClass childClass = classFileImporter.importClass(Child.class); + JavaMethod method = childClass.getMethod("method", Integer.class); + JavaClass parentClass = classFileImporter.importClass(Parent.class); + System.out.println(parentClass.getMethod("method", Number.class).getParameterTypes()); + System.out.println(parentClass.getTypeParameters().stream().map(JavaTypeVariable::getBounds).collect(Collectors.toList())); + System.out.println(childClass.getRawSuperclass()); + assertThat(method.isOverridden()).isTrue(); // Expecting value to be true but was false + } } \ No newline at end of file From c13a74a1ab029cd90bcbb437eb7ba3ea29589c5c Mon Sep 17 00:00:00 2001 From: java-coding-prodigy Date: Sat, 11 Nov 2023 21:02:47 +0530 Subject: [PATCH 11/11] Finally passed the test. --- .../archunit/core/domain/JavaMethod.java | 64 +++++++++++-------- .../archunit/core/domain/JavaMethodTest.java | 10 +-- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java index cb0f01d2c9..49583d1846 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/JavaMethod.java @@ -16,12 +16,9 @@ package com.tngtech.archunit.core.domain; import java.lang.reflect.Method; -import java.util.List; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.Function; import java.util.function.Supplier; -import java.util.stream.Stream; import com.tngtech.archunit.PublicAPI; import com.tngtech.archunit.base.ArchUnitException.InconsistentClassPathException; @@ -126,31 +123,44 @@ public String getDescription() { return "Method <" + getFullName() + ">"; } - @PublicAPI(usage = ACCESS) - public boolean isOverridden() { - return Stream.concat(getOwner().getAllRawSuperclasses().stream(), getOwner().getAllRawInterfaces().stream()) - .map(JavaClass::getAllMethods) - .flatMap(Set::stream) - .anyMatch(superMethod -> { - if(!superMethod.getName().equals(getName())){ - return false; - } - List> superClassTypeParameters = superMethod.getOwner().getTypeParameters(); - List parameterTypes = getParameters(); - List superMethodParameterTypes = superMethod.getParameters(); - if(parameterTypes.size() != superMethodParameterTypes.size()){ + @PublicAPI(usage = ACCESS) public boolean isOverridden() { + JavaClass superClass; + for (Optional superClassOpt = + getOwner().getSuperclass(); superClassOpt.isPresent(); + superClassOpt = superClass.getSuperclass()) { + JavaParameterizedType superClassType = (JavaParameterizedType) superClassOpt.get(); + superClass = superClassType.toErasure(); + List> superClassTypeParameters = + superClass.getTypeParameters(); + Map*/, JavaType> typeParametersToOverridenTypes = + new HashMap<>(); + for (int i = 0; i < superClassTypeParameters.size(); i++) { + typeParametersToOverridenTypes.put(superClassTypeParameters.get(i), + superClassType.getActualTypeArguments().get(i)); + } + + for (JavaMethod superMethod : superClass.getAllMethods()) { + if (!superMethod.getName().equals(getName())) { + return false; + } + + List parameterTypes = getParameters(); + List superMethodParameterTypes = superMethod.getParameters(); + if (parameterTypes.size() != superMethodParameterTypes.size()) { + return false; + } + for (int i = 0; i < parameterTypes.size(); i++) { + JavaParameter parameter = parameterTypes.get(i); + JavaParameter superParameter = superMethodParameterTypes.get(i); + if (!parameter.equals(superParameter) && !typeParametersToOverridenTypes.get( + superParameter.getType()).equals(parameter.getType())) { return false; } - for(int i = 0; i < parameterTypes.size(); i++){ - JavaParameter parameter = parameterTypes.get(i); - JavaParameter superParameter = superMethodParameterTypes.get(i); - if(!parameter.equals(superParameter)){ - return false; - } - //TODO Somehow find type arguements of extension to the super class(cannot be done with raw supperclass) - } - return true; - }); + } + return true; + } + } + return false; } @ResolvesTypesViaReflection 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 53d1832d37..5bc8e6ad8c 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 @@ -9,7 +9,7 @@ import static com.tngtech.archunit.testutil.Assertions.assertThat; public class JavaMethodTest { - //@Test + @Test public void isOverriddenTest() { class Base { void method1() { @@ -70,10 +70,6 @@ void method(Integer t) { } ClassFileImporter classFileImporter = new ClassFileImporter(); JavaClass childClass = classFileImporter.importClass(Child.class); JavaMethod method = childClass.getMethod("method", Integer.class); - JavaClass parentClass = classFileImporter.importClass(Parent.class); - System.out.println(parentClass.getMethod("method", Number.class).getParameterTypes()); - System.out.println(parentClass.getTypeParameters().stream().map(JavaTypeVariable::getBounds).collect(Collectors.toList())); - System.out.println(childClass.getRawSuperclass()); - assertThat(method.isOverridden()).isTrue(); // Expecting value to be true but was false + assertThat(method.isOverridden()).isTrue(); } -} \ No newline at end of file +}