コード例 #1
0
 def test_anonymous(self):
     filepath = str(Path(self.current_directory, "Anonymous.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [19, 25])
コード例 #2
0
 def test_chained_exceptions3(self):
     filepath = str(
         Path(self.current_directory,
              "DatabaseNavigatorSourceContainer.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [])
コード例 #3
0
 def test_try_inside_try(self):
     filepath = str(Path(self.current_directory, "TryInsideTry.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [10, 17])
コード例 #4
0
 def test_tricky_fake(self):
     filepath = str(Path(self.current_directory, "TrickyFake.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [])
コード例 #5
0
 def test_sequential_catch_try(self):
     filepath = str(Path(self.current_directory, "SequentialCatchTry.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [6, 8, 13, 15])
コード例 #6
0
 def test_instance_different_methods(self):
     filepath = str(Path(self.current_directory, "MultipleCatch.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [6])
コード例 #7
0
 def test_both_catches(self):
     filepath = str(Path(self.current_directory, "BothCatches.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [6, 8])
コード例 #8
0
 def test_throw_with_cast(self):
     filepath = str(Path(self.current_directory, "CommonRdbmsReader.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [])
コード例 #9
0
 def test_try_without_catch(self):
     filepath = str(Path(self.current_directory, "ConcurrentDiskUtil.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [])
コード例 #10
0
 def test_chained_exceptions2(self):
     filepath = str(
         Path(self.current_directory, "ConfigImportWizardPageDbvis.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [])
コード例 #11
0
 def test_catch_with_if(self):
     filepath = str(Path(self.current_directory, "CatchWithIf.java"))
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = EmptyRethrow()
     self.assertEqual(pattern.value(ast), [14])
コード例 #12
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)
コード例 #13
0
class TestEmptyRethrow(TestCase):
    cur_file_dir = Path(os.path.realpath(__file__)).parent
    testClass = EmptyRethrow()

    def test_empty(self):
        file = str(Path(self.cur_file_dir, 'Empty.java'))
        self.assertEqual(self.testClass.value(file), [])

    def test_anonymous(self):
        file = str(Path(self.cur_file_dir, 'Anonymous.java'))
        self.assertEqual(self.testClass.value(file), [19, 25])

    def test_both_catches(self):
        file = str(Path(self.cur_file_dir, 'BothCatches.java'))
        self.assertEqual(self.testClass.value(file), [6, 8])

    def test_instance_different_methods(self):
        file = str(Path(self.cur_file_dir, 'MultipleCatch.java'))
        self.assertEqual(self.testClass.value(file), [6])

    def test_sequential_catch(self):
        file = str(Path(self.cur_file_dir, 'SequentialCatch.java'))
        self.assertEqual(self.testClass.value(file), [6, 8])

    def test_sequential_catch_try(self):
        file = str(Path(self.cur_file_dir, 'SequentialCatchTry.java'))
        self.assertEqual(self.testClass.value(file), [6, 8, 13, 15])

    def test_simple(self):
        file = str(Path(self.cur_file_dir, 'Simple.java'))
        self.assertEqual(self.testClass.value(file), [6])

    def test_tricky_fake(self):
        file = str(Path(self.cur_file_dir, 'TrickyFake.java'))
        self.assertEqual(self.testClass.value(file), [])

    def test_try_inside_catch(self):
        file = str(Path(self.cur_file_dir, 'TryInsideCatch.java'))
        self.assertEqual(self.testClass.value(file), [10])

    def test_try_inside_finally(self):
        file = str(Path(self.cur_file_dir, 'TryInsideFinally.java'))
        self.assertEqual(self.testClass.value(file), [6, 12])

    def test_try_inside_try(self):
        file = str(Path(self.cur_file_dir, 'TryInsideTry.java'))
        self.assertEqual(self.testClass.value(file), [10, 17])

    def test_catch_with_functions(self):
        file = str(Path(self.cur_file_dir, 'CatchWithFunctions.java'))
        self.assertEqual(self.testClass.value(file), [13])

    def test_catch_with_if(self):
        file = str(Path(self.cur_file_dir, 'CatchWithIf.java'))
        self.assertEqual(self.testClass.value(file), [14])

    def test_chained_exceptions(self):
        file = str(Path(self.cur_file_dir, 'DataBaseNavigator.java'))
        file2 = str(Path(self.cur_file_dir, 'ConfigImportWizardPageDbvis.java'))
        file3 = str(Path(self.cur_file_dir, 'DatabaseNavigatorSourceContainer.java'))
        self.assertEqual(self.testClass.value(file), [])
        self.assertEqual(self.testClass.value(file2), [])
        self.assertEqual(self.testClass.value(file3), [])

    def test_try_without_catch(self):
        file = str(Path(self.cur_file_dir, 'ConcurrentDiskUtil.java'))
        self.assertEqual(self.testClass.value(file), [])

    def test_throw_with_cast(self):
        file = str(Path(self.cur_file_dir, 'CommonRdbmsReader.java'))
        self.assertEqual(self.testClass.value(file), [])