-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More easily detect overridden methods & calls to super #1040
base: main
Are you sure you want to change the base?
Changes from 12 commits
380958a
e569b0e
9c68442
9f58651
1903590
3e97e3f
3ea2f36
c901839
9e74551
c9e63f6
02eae70
e67cb29
d7c3481
2acc5d6
9c8a0a2
82c8bfa
1767d88
b8b74e6
c13a74a
fa2bebc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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; | ||
|
||
public class JavaMethodTest { | ||
@Test | ||
public void isOverriddenTest() { | ||
class Base { | ||
void method1() { | ||
} | ||
|
||
void method1(int x) { | ||
} | ||
} | ||
class Child extends Base { | ||
void method1() { | ||
} | ||
|
||
void method2() { | ||
} | ||
} | ||
class GrandChild extends Child { | ||
void method1() { | ||
|
||
} | ||
|
||
void method1(int x) { | ||
|
||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that we indeed don't recognize all overriden generic methods: @Test
public void overridden_generic_methods_are_supported() {
class Parent<T extends Number> {
void method(T t) { }
}
class Child extends Parent<Integer> {
@Override
void method(Integer t) { }
}
JavaClass childClass = new ClassFileImporter().importClass(Child.class);
JavaMethod method = childClass.getMethod("method", Integer.class);
assertThat(method.isOverridden()).isTrue(); // Expecting value to be true but was false
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, this might complicate the code now. |
||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also have a test for the use case of #1047 . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that is already in place, currently we check if the parameters and name of the method are equal, and that implies compatibility of return type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that we should document that this works (which it curently does, but we also want to avoid any future regression): @Test
public void more_specific_return_type_is_supported() {
class Parent {
CharSequence method() { return "base"; }
}
class Child extends Parent {
@Override
String method() { return "overridden"; }
}
JavaClass childClass = new ClassFileImporter().importClass(Child.class);
JavaMethod method = childClass.getMethod("method");
assertThat(method.isOverridden()).isTrue();
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we pleae add a small test to cover the feature that overriden methods from interfaces are detected?