Exemple #1
0
 def test_nested_chain_complicated_structure(self):
     """
     Several nested structures are checked: nested method chaining
     with nested anonymous classes
     """
     filepath = self.current_directory / "HolyMolyNestedChain.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [60, 67, 77])
Exemple #2
0
 def test_nested_chain_with_this(self):
     filepath = self.current_directory / "NestedChainWIthThis.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [14, 15])
Exemple #3
0
 def test_chain_without_object_creating(self):
     filepath = self.current_directory / "WithoutObjectCreating.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [14])
Exemple #4
0
 def test_several_chains(self):
     filepath = self.current_directory / "MethodChainSeveral.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [13, 34, 48])
Exemple #5
0
 def test_chain_in_anonymous_class_empty(self):
     filepath = self.current_directory / "MethodChainAnonymousClassEmpty.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [])
Exemple #6
0
 def test_chain_in_nested_class(self):
     filepath = self.current_directory / "MethodChainNestedClass.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [19])
Exemple #7
0
 def test_method_chain_in_different_methods(self):
     filepath = self.current_directory / "MethodChainInDifferentMethods.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [22, 34])
Exemple #8
0
 def test_chain_with_new_object(self):
     filepath = self.current_directory / "MethodChainNewObjectMethods.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [23, 34])
Exemple #9
0
 def test_empty_method_chain(self):
     filepath = self.current_directory / "EmptyMethodChain.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [21])
Exemple #10
0
 def test_many_chains(self):
     filepath = self.current_directory / "MachineLearningGetResultsIT.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertGreater(len(lines), 300)
Exemple #11
0
 def test_smallest_chain(self):
     filepath = self.current_directory / "SmallestChain.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [31, 83, 84])
Exemple #12
0
 def test_nested_chain_with_simple_method_invocation(self):
     filepath = self.current_directory / "NestedChainWithSimpleMethodInvocation.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = MethodChainFind()
     lines = pattern.value(ast)
     self.assertEqual(lines, [15, 16])
Exemple #13
0
class TestMethodChain(TestCase):
    dir_path = Path(os.path.realpath(__file__)).parent
    method_chain_finder = MethodChainFind()

    def test_method_chain(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MethodChain.java'))
        self.assertEqual(lines, [21])

    def test_empty_method_chain(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'EmptyMethodChain.java'))
        self.assertEqual(lines, [21])

    def test_chain_with_new_object(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MethodChainNewObjectMethods.java'))
        self.assertEqual(lines, [23, 34])

    def test_method_chain_in_different_methods(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MethodChainInDifferentMethods.java'))
        self.assertEqual(lines, [22, 34])

    def test_chain_in_nested_class(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MethodChainNestedClass.java'))
        self.assertEqual(lines, [19])

    def test_chain_in_anonymous_class(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MethodChainAnonymousClass.java'))
        self.assertEqual(lines, [29])

    def test_chain_in_anonymous_class_empty(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MethodChainAnonymousClassEmpty.java'))
        self.assertEqual(lines, [])

    def test_several_chains(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MethodChainSeveral.java'))
        self.assertEqual(lines, [13, 34, 48])

    def test_chain_without_object_creating(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'WithoutObjectCreating.java'))
        self.assertEqual(lines, [14])

    def test_nested_chain_with_this(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'NestedChainWIthThis.java'))
        self.assertEqual(lines, [14, 15])

    def test_nested_chain_with_simple_method_invocation(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'NestedChainWithSimpleMethodInvocation.java'))
        self.assertEqual(lines, [15, 16])

    def test_nested_chain_complicated_structure(self):
        """
        Several nested structures are checked: nested method chaining
        with nested anonymous classes
        """
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'HolyMolyNestedChain.java'))
        self.assertEqual(lines, [60, 67, 77])

    def test_smallest_chain(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'SmallestChain.java'))
        self.assertEqual(lines, [31, 83, 84])

    def test_fake_chain(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'FakeChain.java'))
        self.assertEqual(lines, [])

    def test_many_chains(self):
        lines = self.method_chain_finder.value(
            Path(self.dir_path, 'MachineLearningGetResultsIT.java'))
        self.assertGreater(len(lines), 300)
Exemple #14
0
def main():
    exit_status = -1
    patterns_list = [
        'var_middle_number', 'this_find_number', 'string_concat_number',
        'instance_of_number', 'method_chain_number', 'var_decl_diff_number_11',
        'var_decl_diff_number_7', 'var_decl_diff_number_5',
        'super_method_call_number', 'force_type_cast_number', 'asserts_number',
        'setter_number', 'empty_rethrow_number',
        'prohibited_class_names_number', 'return_in_if_number',
        'impl_multi_number', 'many_prim_ctors_number', 'multiple_try_number',
        'non_final_field_number', 'null_check_number', 'part_sync_number',
        'red_catch_number', 'return_null_number'
    ]
    try:
        parser = argparse.ArgumentParser(
            description=
            'Find the pattern which has the largest impact on readability')
        parser.add_argument('--filename', help='path for Java file')
        parser.add_argument(
            '--version',
            action='version',
            version='%(prog)s {version}'.format(version=__version__))

        args = parser.parse_args(args=None if sys.argv[1:] else ['--help'])

        if args:
            java_file = str(Path(os.getcwd(), args.filename))
            halstead_volume = find_halstead(java_file)
            var_numbers = VarMiddle().value(java_file)
            entropy = Entropy().value(java_file)
            left_space_variance, right_space_variance, max_left_space_diff, max_right_space_diff \
                = IndentationCounter().value(java_file)
            concat_str_number = StringConcatFinder().value(java_file)
            instance_of_lines = InstanceOf().value(java_file)
            method_chain_lines = MethodChainFind().value(java_file)
            var_decl_diff_lines_5 = VarDeclarationDistance(
                lines_th=5).value(java_file)
            var_decl_diff_lines_7 = VarDeclarationDistance(
                lines_th=7).value(java_file)
            var_decl_diff_lines_11 = VarDeclarationDistance(
                lines_th=11).value(java_file)
            super_m_lines = SuperMethod().value(java_file)
            force_type_cast_number = ForceTypeCastingFinder().value(java_file)
            this_lines = ThisFinder().value(java_file)
            asserts_lines = AssertInCode().value(java_file)
            setter_lines = ClassicSetter().value(java_file)
            empty_rethrow_lines = EmptyRethrow().value(java_file)
            prohibited_class_names = ErClass().value(java_file)
            if_return_lines = CountIfReturn().value(java_file)
            impl_multi_lines = ImplementsMultiFinder().value(java_file)
            many_prim_ctors_lines = ManyPrimaryCtors().value(java_file)
            multiple_try_lines = MultipleTry().value(java_file)
            non_final_field_lines = NonFinalAttribute().value(java_file)
            null_check_lines = NullCheck().value(java_file)
            part_sync_lines = PartialSync().value(java_file)
            red_catch_lines = RedundantCatch().value(java_file)
            return_null_lines = ReturnNull().value(java_file)
            ncss_lightweight = NCSSMetric().value(java_file)

            code_lines_dict = {
                'var_middle_number': var_numbers,
                'string_concat_number': concat_str_number,
                'instance_of_number': instance_of_lines,
                'method_chain_number': method_chain_lines,
                'var_decl_diff_number_5': var_decl_diff_lines_5,
                'var_decl_diff_number_7': var_decl_diff_lines_7,
                'var_decl_diff_number_11': var_decl_diff_lines_11,
                'super_method_call_number': super_m_lines,
                'force_type_cast_number': force_type_cast_number,
                'this_find_number': this_lines,
                'asserts_number': asserts_lines,
                'setter_number': setter_lines,
                'empty_rethrow_number': empty_rethrow_lines,
                'prohibited_class_names_number': prohibited_class_names,
                'return_in_if_number': if_return_lines,
                'impl_multi_number': impl_multi_lines,
                'many_prim_ctors_number': many_prim_ctors_lines,
                'multiple_try_number': multiple_try_lines,
                'non_final_field_number': non_final_field_lines,
                'null_check_number': null_check_lines,
                'part_sync_number': part_sync_lines,
                'red_catch_number': red_catch_lines,
                'return_null_number': return_null_lines,
            }
            input_params = {
                'var_middle_number': len(var_numbers),
                'string_concat_number': len(concat_str_number),
                'instance_of_number': len(instance_of_lines),
                'method_chain_number': len(method_chain_lines),
                'var_decl_diff_number_5': len(var_decl_diff_lines_5),
                'var_decl_diff_number_7': len(var_decl_diff_lines_7),
                'var_decl_diff_number_11': len(var_decl_diff_lines_11),
                'super_method_call_number': len(super_m_lines),
                'force_type_cast_number': len(force_type_cast_number),
                'this_find_number': len(this_lines),
                'asserts_number': len(asserts_lines),
                'setter_number': len(setter_lines),
                'empty_rethrow_number': len(empty_rethrow_lines),
                'prohibited_class_names_number': len(prohibited_class_names),
                'return_in_if_number': len(if_return_lines),
                'impl_multi_number': len(impl_multi_lines),
                'many_prim_ctors_number': len(many_prim_ctors_lines),
                'multiple_try_number': len(multiple_try_lines),
                'non_final_field_number': len(non_final_field_lines),
                'null_check_number': len(null_check_lines),
                'part_sync_number': len(part_sync_lines),
                'red_catch_number': len(red_catch_lines),
                'return_null_number': len(return_null_lines),
                'entropy': entropy,
                'halstead volume': halstead_volume,
                'left_spaces_var': left_space_variance,
                'right_spaces_var': right_space_variance,
                'max_left_diff_spaces': max_left_space_diff,
                'max_right_diff_spaces': max_right_space_diff,
                'ncss_lightweight': ncss_lightweight,
            }

            sorted_result = predict(input_params)
            found_pattern = False
            code_lines = None
            value = None
            for iter, (key, val) in enumerate(sorted_result.items()):
                if key in patterns_list:
                    if not found_pattern:
                        pattern = key
                        code_lines = code_lines_dict.get(key)
                        # We show only positive gradient, we won't add patterns
                        if code_lines and val > 1.00000e-20:
                            found_pattern = True
                            value = val

            if not code_lines:
                print('Your code is perfect in aibolit\'s opinion')
            else:
                output_str = \
                    'The largest contribution for {file} is {val} for \"{pattern}\" pattern'.format(
                        file=java_file,
                        pattern=pattern,
                        val=value)
                print(output_str)
                for line in code_lines:
                    if line:
                        print('Line {}. Low readability due to: {}'.format(
                            line, pattern))
            exit_status = 0
    except KeyboardInterrupt:
        exit_status = -1
    sys.exit(exit_status)