Skip to content

Decrease field visibility

Introduction:

Decrease the visibility of a field from public to protected, protected to package or package to private.

Pre and Post Conditions

Pre Conditions:

  1. User must enter the field's name, and the source class's name for the refactoring in order to decrease the target field's visibility.

Post Conditions:

No specific Post Condition

__init__(self, common_token_stream=None, source_class=None, field_name=None) special

To implement ِDecrease Field Visibility refactoring based on its actors. Detects the required field and decreases/changes its visibility status.

Parameters:

Name Type Description Default
common_token_stream CommonTokenStream

A stream of tokens generated by parsing the main file using the ANTLR parser generator

None
source_class str

Name of the class in which the refactoring has to be done

None
field_name str

Name of the field whose visibility status has to be changed

None

Returns:

Type Description

No returns

Source code in refactorings\decrease_field_visibility.py
def __init__(self, common_token_stream: CommonTokenStream = None, source_class=None, field_name: str = None):
    """
    To implement ِDecrease Field Visibility refactoring based on its actors.
    Detects the required field and decreases/changes its visibility status.

    Args:
        common_token_stream (CommonTokenStream): A stream of tokens generated by parsing the main file using the ANTLR parser generator
        source_class (str): Name of the class in which the refactoring has to be done
        field_name (str): Name of the field whose visibility status has to be changed

    Returns:
        No returns

    """

    if field_name is None:
        self.field_name = ""
    else:
        self.field_name = field_name

    if source_class is None:
        self.source_class = ""
    else:
        self.source_class = source_class
    if common_token_stream is None:
        raise ValueError('common_token_stream is None')
    else:
        self.token_stream_rewriter = TokenStreamRewriter(common_token_stream)

        self.is_source_class = False
        self.detected_field = None
        self.detected_method = None
        self.TAB = "\t"
        self.NEW_LINE = "\n"
        self.code = ""
        self.tempdeclarationcode = ""