Make field non-final
Introduction
Remove the "final" property from a field, so that it can be changed after initialization.
Pre and post-conditions
Pre-conditions:
-
User must enter the field's name and the name of the source class in order to make it non-final
-
Check if the field exists, then make it non-final
Post-conditions:
No specific Post Condition
MakeFieldNonFinalRefactoringListener (JavaParserLabeledListener)
The Main listener which parses the file based on the provided information using ANTLR parser generator and tokenization methods.
Detects the desired field and removes the "final" keyword from its properties.
Source code in codart\refactorings\make_field_non_final.py
class MakeFieldNonFinalRefactoringListener(JavaParserLabeledListener):
"""
The Main listener which parses the file based on the provided information using ANTLR parser generator and tokenization methods.
Detects the desired field and removes the "final" keyword from its properties.
"""
def __init__(self, common_token_stream: CommonTokenStream = None, source_class=None, field_name: str = None):
"""
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 final status has to be changed
Returns:
object (MakeFieldNonFinalRefactoringListener): An instance of MakeFieldNonFinalRefactoringListener
"""
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.is_final = False
def enterClassDeclaration(self, ctx: JavaParserLabeled.ClassDeclarationContext):
class_identifier = ctx.IDENTIFIER().getText()
if class_identifier == self.source_class:
self.is_source_class = True
else:
self.is_source_class = False
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext):
if not self.is_source_class:
return None
grand_parent_ctx = ctx.parentCtx.parentCtx
field_identifier = ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().getText()
print("field_identifier :", field_identifier)
if self.field_name in field_identifier:
if not (grand_parent_ctx.modifier() == []):
for i in range(0, len(grand_parent_ctx.modifier())):
if grand_parent_ctx.modifier(i).getText() == "final":
self.is_final = True
break
print("-----------------------", self.is_final)
if self.is_final:
self.token_stream_rewriter.replaceRange(
from_idx=grand_parent_ctx.modifier(i).start.tokenIndex,
to_idx=grand_parent_ctx.modifier(i).stop.tokenIndex,
text=''
)
__init__(self, common_token_stream=None, source_class=None, field_name=None)
special
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 final status has to be changed |
None |
Returns:
Type | Description |
---|---|
object (MakeFieldNonFinalRefactoringListener) |
An instance of MakeFieldNonFinalRefactoringListener |
Source code in codart\refactorings\make_field_non_final.py
def __init__(self, common_token_stream: CommonTokenStream = None, source_class=None, field_name: str = None):
"""
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 final status has to be changed
Returns:
object (MakeFieldNonFinalRefactoringListener): An instance of MakeFieldNonFinalRefactoringListener
"""
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.is_final = False