Exemplo n.º 1
0
def class_RequestMapping_produces_consumes(lines):
    # fix class-level annotation
    try:
        req_mapping_anno = perfec2.get_class_annotation(perfec2.this_clazz(), 'RequestMapping')
        path = perfec2.util.get_annotation_property(req_mapping_anno)
        new_anno_props = {
            'value': path,
            'produces': '"application/json"',
            'consumes': '"application/json"',
        }
        perfec2.remove_node_annotation(perfec2.this_clazz(), 'RequestMapping', lines)
        perfec2.add_node_annotation(perfec2.this_clazz(), 'RequestMapping', lines, anno_props=new_anno_props, oneline=True)
    except Exception as e:
        print(traceback.format_exc())
def refactor_classfile(lines: List[str]):
    print(f'processing class {perfec2.this_clazz().name}')

    # add Logger field
    # todo return added field
    perfec2.add_field(perfec2.this_clazz(), 'Logger', 'log', 'private', lines,
                      '')  #fixme unused last argument
    perfec2.add_field_annotation(
        perfec2.util.field_with_name(perfec2.this_clazz(), 'log'), 'Autowired',
        lines)

    # remove all @Slf4j
    perfec2.remove_all_anno('Slf4j', lines)

    # add imports:
    perfec2.add_import(
        'org.springframework.beans.factory.annotation.Autowired', lines)
    return perfec2.add_import('org.slf4j.Logger', lines)
def refactor_testfile(lines: List[str]) -> List[str]:
    print(f'Refactoring {perfec2.this_clazz().name}')

    # add Logger with @Mock
    perfec2.add_field(perfec2.this_clazz(), 'Logger', 'log', 'private', lines,
                      'Mock')
    log_field = perfec2.util.get_field(perfec2.this_clazz(), 'log')
    perfec2.field_spacing(log_field, lines)

    # check if @InjectMocks required
    if not perfec2.util.fields_with_anno(perfec2.this_clazz(), 'InjectMocks'):
        # add @InjectMocks to testee
        perfec2.add_field_annotation(perfec2.find_testee(perfec2.this_clazz()),
                                     'InjectMocks', lines)
        perfec2.field_spacing(perfec2.find_testee(perfec2.this_clazz()), lines)

        # also add @Before init mocks
        lfield = perfec2.last_field_line(perfec2.this_clazz())
        perfec2.add_method('public', 'void', 'init', [],
                           ['MockitoAnnotations.initMocks(this);\n'],
                           lfield + 1, lines, 'Before')

        perfec2.add_import('org.mockito.InjectMocks', lines)
        perfec2.add_import('org.mockito.MockitoAnnotations', lines)
        perfec2.add_import('org.junit.Before', lines)
        perfec2.add_import(
            'org.springframework.beans.factory.annotation.Autowired', lines)

    # add mock import
    perfec2.add_import('org.slf4j.Logger', lines)
    return perfec2.add_import('org.mockito.Mock', lines)
Exemplo n.º 4
0
def refactor_controller(lines: List[str]) -> List[str]:
    """
    within this method we have access to the lines of the file being processed
    and the compilation unit itself
    :param lines:   provided by perfec2.process_file()
    :return:
    """
    clazz = perfec2.this_clazz()
    print(f'annotating {clazz.name}')
    use_RestController_only(lines)
    use_RequestMapping_only(lines)
    do_swagger_annotations(lines)
    class_RequestMapping_produces_consumes(lines)

    return lines
Exemplo n.º 5
0
def do_swagger_annotations(lines):
    perfec2.annotate_methods_having_annotation(perfec2.this_clazz(), lines, 'RequestMapping', 'ApiOperation', props_for_method)
    perfec2.add_import('io.swagger.annotations.ApiOperation', lines)
Exemplo n.º 6
0
def use_RestController_only(lines: [str]) -> [str]:
    if perfec2.get_class_annotation(perfec2.this_clazz(), 'Controller'):
        perfec2.remove_node_annotation(perfec2.this_clazz(), 'Controller', lines)
        perfec2.add_node_annotation(perfec2.this_clazz(), 'RestController', lines)
Exemplo n.º 7
0
def use_RequestMapping_only(lines: [str]) -> [str]:
    perfec2.replace_annotation_on_members(perfec2.this_clazz(), 'methods', lines,
                                          'PostMapping', 'RequestMapping', request_mapping_props, oneline=True)