-
Notifications
You must be signed in to change notification settings - Fork 298
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
Open
java-coding-prodigy
wants to merge
20
commits into
TNG:main
Choose a base branch
from
java-coding-prodigy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
380958a
Added Method#isOverridden
java-coding-prodigy e569b0e
Fixed the method and added a test
java-coding-prodigy 9c68442
Merge branch 'main' into main
java-coding-prodigy 9f58651
Added support for methods in grandparent class and used ArchUnit API …
java-coding-prodigy 1903590
Merge branch 'main' of github.com:TNG/ArchUnit
java-coding-prodigy 3e97e3f
Fixed issue with `isOverridden`
java-coding-prodigy 3ea2f36
Added testing for overriden method in grandparent class
java-coding-prodigy c901839
Merge branch 'main' of github.com:java-coding-prodigy/ArchUnit
java-coding-prodigy 9e74551
Undo accidental copyright change
java-coding-prodigy c9e63f6
Check for super methods in interfaces as well
java-coding-prodigy 02eae70
Import fix
java-coding-prodigy e67cb29
Merge branch 'main' into main
java-coding-prodigy d7c3481
Got this so far
java-coding-prodigy 2acc5d6
Got this so far
java-coding-prodigy 9c8a0a2
Merge branch 'main' into main
java-coding-prodigy 82c8bfa
Merge branch 'main' into main
java-coding-prodigy 1767d88
Merge branch 'main' into main
java-coding-prodigy b8b74e6
Merge branch 'TNG:main' into main
java-coding-prodigy c13a74a
Finally passed the test.
java-coding-prodigy fa2bebc
Merge branch 'main' into main
java-coding-prodigy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
archunit/src/test/java/com/tngtech/archunit/core/domain/JavaMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.tngtech.archunit.core.domain; | ||
|
||
|
||
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 | ||
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 | ||
} | ||
@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) { } | ||
|
||
} | ||
ClassFileImporter classFileImporter = new ClassFileImporter(); | ||
JavaClass childClass = classFileImporter.importClass(Child.class); | ||
JavaMethod method = childClass.getMethod("method", Integer.class); | ||
assertThat(method.isOverridden()).isTrue(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems that we indeed don't recognize all overriden generic methods:
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.
Hmmm, this might complicate the code now.
I'll see what I can do.