-
Notifications
You must be signed in to change notification settings - Fork 51
#150: Implement class inheritance pattern #1227
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
Merged
yegor256
merged 3 commits into
cqfn:master
from
marinka-aa:fix-issue-150-class-inheritance
Jun 25, 2026
Merged
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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,2 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2019-2026 Aibolit | ||
| # SPDX-License-Identifier: MIT |
This file contains hidden or 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,19 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2019-2026 Aibolit | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from typing import List | ||
|
|
||
| from aibolit.ast_framework import AST, ASTNodeType | ||
|
|
||
|
|
||
| class ClassInheritance: | ||
| """ | ||
| Find classes that use implementation inheritance via the `extends` keyword. | ||
| """ | ||
|
|
||
| def value(self, ast: AST) -> List[int]: | ||
| lines: List[int] = [] | ||
| for class_declaration in ast.proxy_nodes(ASTNodeType.CLASS_DECLARATION): | ||
| if class_declaration.extends is not None: | ||
| lines.append(class_declaration.line) | ||
| return lines |
This file contains hidden or 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,12 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2019-2026 Aibolit | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package patterns.class_inheritance; | ||
|
|
||
| class Outer { | ||
| static class Base { | ||
| } | ||
|
|
||
| class Inner extends Base { | ||
| } | ||
| } | ||
This file contains hidden or 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,8 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2019-2026 Aibolit | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package patterns.class_inheritance; | ||
|
|
||
| interface Base {} | ||
| class MyList implements Base { | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or 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,8 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2019-2026 Aibolit | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package patterns.class_inheritance; | ||
|
|
||
| class Base {} | ||
| class MyList extends Base { | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or 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,7 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2019-2026 Aibolit | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package patterns.class_inheritance; | ||
|
|
||
| class GenericBox<T extends Number> { | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or 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,41 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2019-2026 Aibolit | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from pathlib import Path | ||
| from unittest import TestCase | ||
|
|
||
| from aibolit.ast_framework import AST | ||
| from aibolit.patterns.class_inheritance.class_inheritance import ClassInheritance | ||
| from aibolit.utils.ast_builder import build_ast | ||
|
|
||
|
|
||
| class ClassInheritanceTestCase(TestCase): | ||
| current_directory = Path(__file__).absolute().parent | ||
|
|
||
| def test_find_class_inheritance(self): | ||
| filepath = self.current_directory / 'SimpleInheritance.java' | ||
| ast = AST.build_from_javalang(build_ast(filepath)) | ||
| pattern = ClassInheritance() | ||
| lines = pattern.value(ast) | ||
| self.assertEqual(lines, [7]) | ||
|
|
||
| def test_ignore_implements_only(self): | ||
| filepath = self.current_directory / 'OnlyImplements.java' | ||
| ast = AST.build_from_javalang(build_ast(filepath)) | ||
| pattern = ClassInheritance() | ||
| lines = pattern.value(ast) | ||
| self.assertEqual(lines, []) | ||
|
|
||
| def test_ignore_type_parameter_extends(self): | ||
| filepath = self.current_directory / 'TypeParameterExtends.java' | ||
| ast = AST.build_from_javalang(build_ast(filepath)) | ||
| pattern = ClassInheritance() | ||
| lines = pattern.value(ast) | ||
| self.assertEqual(lines, []) | ||
|
|
||
| def test_find_nested_class_inheritance(self): | ||
| filepath = self.current_directory / 'NestedInheritance.java' | ||
| ast = AST.build_from_javalang(build_ast(filepath)) | ||
| pattern = ClassInheritance() | ||
| lines = pattern.value(ast) | ||
| self.assertEqual(lines, [10]) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.