Skip to content

Commit

Permalink
Array as argument refactored.
Browse files Browse the repository at this point in the history
It is now check constructors too.
According test is added.
  • Loading branch information
aravij committed Jul 17, 2020
1 parent 630df7b commit a44cf30
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
25 changes: 13 additions & 12 deletions aibolit/patterns/array_as_argument/array_as_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import javalang
from typing import List

from aibolit.ast_framework import AST, ASTNodeType
from aibolit.utils.ast_builder import build_ast


class ArrayAsArgument:
'''
Finds all methods declarations, which accept arrays as parameters.
'''

def __init__(self):
pass

def value(self, filename):
tree = build_ast(filename).filter(javalang.tree.MethodDeclaration)
ret = []
for _, node in tree:
if len(node.parameters) > 0:
if any(len(parameter.type.dimensions) > 0 for parameter in node.parameters):
ret += [node.position.line]
return ret
def value(self, filename: str) -> List[int]:
lines: List[int] = []
ast = AST.build_from_javalang(build_ast(filename))
for method_declaration in ast.get_proxy_nodes(ASTNodeType.METHOD_DECLARATION,
ASTNodeType.CONSTRUCTOR_DECLARATION):
if any(len(parameter.type.dimensions) > 0 for parameter in method_declaration.parameters):
lines.append(method_declaration.line)
return lines
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ConstructorWithArrayAsArgument {
ConstructorWithArrayAsArgument(int[] argument) { // here
}
}
8 changes: 8 additions & 0 deletions test/patterns/array_as_argument/test_array_as_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ def test_GenericArrayAsArgument(self):
self.pattern.value(file),
'Should match method with generic array as argument'
)

def test_ConstructorWithArrayAsArgument(self):
file = Path(self.dir_path, 'ConstructorWithArrayAsArgument.java')
self.assertEqual(
[2],
self.pattern.value(file),
'Should match constructor with array as argument'
)

0 comments on commit a44cf30

Please sign in to comment.