-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
201 additions
and
101 deletions.
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,15 +1,40 @@ | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2020 Aibolit | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from typing import List | ||
|
||
from aibolit.utils.ast_builder import build_ast | ||
from aibolit.ast_framework import AST, ASTNodeType | ||
from aibolit.utils.ast_builder import build_ast | ||
|
||
|
||
class AssertInCode: | ||
''' | ||
Finds use of all asserts. | ||
''' | ||
|
||
def value(self, filename: str) -> List[int]: | ||
tree = AST.build_from_javalang(build_ast(filename)) | ||
ast = AST.build_from_javalang(build_ast(filename)) | ||
lines: List[int] = [] | ||
nodes = tree.get_nodes(ASTNodeType.ASSERT_STATEMENT) | ||
for node in nodes: | ||
lines.append(tree.get_attr(node, 'line')) | ||
for assert_node in ast.get_proxy_nodes(ASTNodeType.ASSERT_STATEMENT): | ||
lines.append(assert_node.line) | ||
|
||
return lines |
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
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 |
---|---|---|
@@ -1,25 +1,52 @@ | ||
from aibolit.ast_framework import ASTNodeType | ||
from aibolit.ast_framework.java_package import JavaPackage | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2020 Aibolit | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from typing import List | ||
|
||
from aibolit.ast_framework import AST, ASTNode, ASTNodeType | ||
from aibolit.utils.ast_builder import build_ast | ||
|
||
|
||
class CountIfReturn: | ||
''' | ||
Returns lines with if statement which has also return statement | ||
and other conditions with else. | ||
Finds "if" statements with else branches and return statement in then branch. | ||
''' | ||
def __init__(self): | ||
pass | ||
|
||
def value(self, filename: str) -> List[int]: | ||
detected_lines = [] | ||
ast = JavaPackage(filename).java_classes | ||
for class_name in ast: | ||
java_class = ast[class_name] | ||
for index, if_node in enumerate(java_class.get_nodes(ASTNodeType.IF_STATEMENT)): | ||
all_childs = [i for i in java_class.tree.succ[if_node]] | ||
if len(all_childs) == 3: | ||
for i in java_class.tree.succ[all_childs[1]]: | ||
if java_class.get_type(i) == ASTNodeType.RETURN_STATEMENT: | ||
detected_lines += [java_class.get_attr(if_node, 'line')] | ||
return detected_lines | ||
ast = AST.build_from_javalang(build_ast(filename)) | ||
lines: List[int] = [] | ||
for if_statement in ast.get_proxy_nodes(ASTNodeType.IF_STATEMENT): | ||
if if_statement.else_statement is not None and \ | ||
self._is_then_branch_return(if_statement): | ||
lines.append(if_statement.line) | ||
|
||
return lines | ||
|
||
def _is_then_branch_return(self, if_statement: ASTNode) -> bool: | ||
if if_statement.then_statement.node_type == ASTNodeType.RETURN_STATEMENT: | ||
return True | ||
|
||
if if_statement.then_statement.node_type == ASTNodeType.BLOCK_STATEMENT: | ||
return any(statement.node_type == ASTNodeType.RETURN_STATEMENT | ||
for statement in if_statement.then_statement.statements) | ||
|
||
return False |
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 |
---|---|---|
@@ -1,29 +1,45 @@ | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2020 Aibolit | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from typing import List | ||
|
||
from aibolit.utils.ast_builder import build_ast | ||
from aibolit.ast_framework import AST, ASTNodeType | ||
from aibolit.utils.ast_builder import build_ast | ||
|
||
|
||
class InstanceOf: | ||
def __init__(self): | ||
pass | ||
""" | ||
Finds instance_of operator and .isInstance() method call. | ||
""" | ||
|
||
def value(self, filename: str): | ||
""" | ||
Traverse over AST tree finds instance_of and .isInstance(). | ||
:param filename: | ||
:return: | ||
List of code lines | ||
""" | ||
tree = AST.build_from_javalang(build_ast(filename)) | ||
ast = AST.build_from_javalang(build_ast(filename)) | ||
lines: List[int] = [] | ||
for node in tree.get_nodes(ASTNodeType.BINARY_OPERATION): | ||
if tree.get_binary_operation_name(node) == 'instanceof': | ||
lines.append(tree.get_line_number_from_children(node)) | ||
for binary_operator in ast.get_proxy_nodes(ASTNodeType.BINARY_OPERATION): | ||
if binary_operator.operator == 'instanceof': | ||
lines.append(binary_operator.line) | ||
|
||
for node in tree.get_nodes(ASTNodeType.METHOD_INVOCATION): | ||
method_name = tree.get_method_invocation_params(node).method_name | ||
if method_name == 'isInstance': | ||
lines.append(tree.get_attr(node, 'line')) | ||
for method_invocation in ast.get_proxy_nodes(ASTNodeType.METHOD_INVOCATION): | ||
if method_invocation.member == 'isInstance': | ||
lines.append(method_invocation.line) | ||
|
||
return lines |
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 |
---|---|---|
@@ -1,34 +1,42 @@ | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2020 Aibolit | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from typing import List | ||
|
||
from aibolit.utils.ast_builder import build_ast | ||
from aibolit.ast_framework import AST, ASTNodeType | ||
from typing import List, Set | ||
|
||
|
||
class MultipleWhile: | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def get_top_level_while_qty(self, tree: AST, node: int) -> int: | ||
list_while_nodes: List[int] = [] | ||
set_child_while_nodes: Set[int] = set() | ||
for child in tree.all_children_with_type(node, ASTNodeType.WHILE_STATEMENT): | ||
list_while_nodes.append(child) | ||
set_internal_while = set(tree.list_all_children_with_type(child, ASTNodeType.WHILE_STATEMENT)) | ||
set_child_while_nodes |= set_internal_while | ||
return len(list_while_nodes) - len(set_child_while_nodes) | ||
''' | ||
Finds methods, with more than one "while" cycle, exceluding nested ones. | ||
''' | ||
|
||
def value(self, filename: str) -> List[int]: | ||
""" | ||
Travers over AST tree and finds function with sequential while statement | ||
:param filename: | ||
:return: | ||
List of LineNumber of methods which have sequential while statements | ||
""" | ||
|
||
tree = AST.build_from_javalang(build_ast(filename)) | ||
ast = AST.build_from_javalang(build_ast(filename)) | ||
lines: List[int] = [] | ||
for node in tree.get_nodes(ASTNodeType.METHOD_DECLARATION): | ||
if self.get_top_level_while_qty(tree, node) > 1: | ||
lines.append(tree.get_attr(node, 'line')) | ||
for method_declaration in ast.get_subtrees(ASTNodeType.METHOD_DECLARATION): | ||
top_level_while_loops = method_declaration.get_subtrees(ASTNodeType.WHILE_STATEMENT) | ||
if len(list(top_level_while_loops)) > 1: | ||
lines.append(method_declaration.get_root().line) | ||
|
||
return lines |
Oops, something went wrong.