コード例 #1
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)
コード例 #2
0
 def test_implements_in_comments(self):
     filepath = self.current_directory / "KeyProviderCryptoExtension.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [])
コード例 #3
0
 def test_implements_multi(self):
     filepath = self.current_directory / "OsSecureRandom.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [42])
コード例 #4
0
 def test_implements_with_parantheses_multi(self):
     filepath = self.current_directory / "FJIterateTest.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [601])
コード例 #5
0
 def test_implements_with_parantheses_before(self):
     filepath = self.current_directory / "FJListProcedureRunner.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [])
コード例 #6
0
 def test_implements_with_nested_parantheses(self):
     filepath = self.current_directory / "Configuration.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [225])
コード例 #7
0
 def test_implements_multi_classes(self):
     filepath = self.current_directory / "FillContent.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [29])
コード例 #8
0
 def test_implements_in_string(self):
     filepath = self.current_directory / "AuditEventModelProcessor.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [])
コード例 #9
0
 def test_two_classes(self):
     filepath = self.current_directory / "AnimatableTransform.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [12])
コード例 #10
0
 def test_one_class_with_types(self):
     filepath = self.current_directory / "AnimatableSplitDimensionPathValue.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [])
コード例 #11
0
 def test_implements_many(self):
     filepath = self.current_directory / "SequenceFile.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = ImplementsMultiFinder()
     lines = pattern.value(ast)
     self.assertEqual(lines, [837])
コード例 #12
0
class TestImplementsMulti(TestCase):
    dir_path = Path(os.path.realpath(__file__)).parent
    multi_finder = ImplementsMultiFinder()

    def test_one_class_with_types(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'AnimatableSplitDimensionPathValue.java'))
        self.assertEqual(lines, [])

    def test_two_classes(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'AnimatableTransform.java'))
        self.assertEqual(lines, [12])

    def test_implements_in_string(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'AuditEventModelProcessor.java'))
        self.assertEqual(lines, [])

    def test_implements_with_parantheses(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'BaseKeyframeAnimation.java'))
        self.assertEqual(lines, [])

    def test_implements_with_nested_parantheses(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'Configuration.java'))
        self.assertEqual(lines, [225])

    def test_implements_multi_classes(self):
        lines = self.multi_finder.value(Path(self.dir_path,
                                             'FillContent.java'))
        self.assertEqual(lines, [29])

    def test_implements_with_parantheses_multi(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'FJIterateTest.java'))
        self.assertEqual(lines, [601])

    def test_implements_with_parantheses_before(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'FJListProcedureRunner.java'))
        self.assertEqual(lines, [])

    def test_implements_in_comments(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'KeyProviderCryptoExtension.java'))
        self.assertEqual(lines, [])

    def test_implements_multi(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'OsSecureRandom.java'))
        self.assertEqual(lines, [42])

    def test_implements_three(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'RectangleContent.java'))
        self.assertEqual(lines, [22])

    def test_implements_many(self):
        lines = self.multi_finder.value(
            Path(self.dir_path, 'SequenceFile.java'))
        self.assertEqual(lines, [837])