def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if self.package_identifier is None and not self.in_some_package\ or self.package_identifier is not None and self.in_selected_package: if self.in_selected_class: if ctx.variableDeclarators().variableDeclarator(0)\ .variableDeclaratorId().getText() == self.field_identifier: grand_parent_ctx = ctx.parentCtx.parentCtx if len(grand_parent_ctx.modifier()) == 0: self.token_stream_rewriter.insertBeforeIndex( index=ctx.parentCtx.start.tokenIndex, text=' static ') else: is_static = False for modifier in grand_parent_ctx.modifier(): if modifier.getText() == "static": is_static = True break if not is_static: self.token_stream_rewriter.insertAfter( index=grand_parent_ctx.start.tokenIndex + len(grand_parent_ctx.modifier()), text=' static ') if self.package_identifier is None or self.package_identifier is not None and self.is_package_imported: if ctx.typeType().classOrInterfaceType() is not None: if ctx.typeType().classOrInterfaceType().getText( ) == self.class_identifier: self.declared_objects_names.append( ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().getText()) print("Object " + ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().getText()\ + " of type " + self.class_identifier + " found.")
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if self.enter_class == False: A = ctx.parentCtx.parentCtx B = ctx.variableDeclarators() if ctx.start.text == self.class_identifier: start = ctx.variableDeclarators().variableDeclarator( 0).ASSIGN().symbol.tokenIndex + 1 end = ctx.stop.tokenIndex self.ObjectIndex.append([start, end])
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if self.in_class: if ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().getText( ) == self.field_identifier: self.token_stream_rewriter.replaceIndex( index=ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().start.tokenIndex, text=self.field_new_name) print("field name changed !")
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.is_source_class: return None field_names = ctx.variableDeclarators().getText().split(",") print("Here") grand_parent_ctx = ctx.parentCtx.parentCtx if self.detected_field in field_names: modifier = grand_parent_ctx.modifier(0).getText() field_type = ctx.typeType().getText() self.code += f"{self.TAB}{modifier} {field_type} {self.detected_field};{self.NEW_LINE}" # delete field from source class field_names.remove(self.detected_field) if field_names: self.token_stream_rewriter.replaceRange( from_idx=grand_parent_ctx.start.tokenIndex, to_idx=grand_parent_ctx.stop.tokenIndex, text=f"{modifier} {field_type} {','.join(field_names)};") else: self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, from_idx=grand_parent_ctx.start.tokenIndex, to_idx=grand_parent_ctx.stop.tokenIndex) self.detected_field = None
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.is_source_class: return None grand_parent_ctx = ctx.parentCtx.parentCtx # field_identifier = ctx.variableDeclarators().getText().split(",") field_identifier = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() if self.field_name in field_identifier: if grand_parent_ctx.modifier() == []: self.token_stream_rewriter.replaceRange( from_idx=ctx.typeType().start.tokenIndex, to_idx=ctx.typeType().stop.tokenIndex, text='static ' + ctx.typeType().getText()) else: for i in range(0, len(grand_parent_ctx.modifier())): if grand_parent_ctx.modifier(i).getText() == "static": self.is_static = True break if not self.is_static: self.token_stream_rewriter.replaceRange( from_idx=grand_parent_ctx.modifier(0).start.tokenIndex, to_idx=grand_parent_ctx.modifier(0).stop.tokenIndex, text=grand_parent_ctx.modifier(0).getText() + ' static')
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if self.is_source_class or self.is_target_class: field_text = '' for child in ctx.children: if child.getText() == ';': field_text = field_text[:len(field_text) - 1] + ';' break field_text += child.getText() + ' ' name = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() if ctx.typeType().classOrInterfaceType() != None and ctx.typeType( ).classOrInterfaceType().getText() == self.source_class: self.field_that_has_source.append(name) return modifier_text = '' for modifier in ctx.parentCtx.parentCtx.modifier(): modifier_text += modifier.getText() + ' ' field_text = modifier_text + field_text if self.is_source_class: self.source_class_data['fields'].append( Field(name=name, text=field_text)) else: self.target_class_data['fields'].append( Field(name=name, text=field_text))
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.is_source_class: return None if not self.detected_field: return None field_names = ctx.variableDeclarators().getText() field_names = field_names.split(',') grand_parent_ctx = ctx.parentCtx.parentCtx if any([self.detected_field in i for i in field_names]): field_type = ctx.typeType().getText() if len(field_names) == 1: self.code += f"public {field_type} {field_names[0]};{self.NEW_LINE}" else: self.code += f"public {field_type} {self.detected_field};{self.NEW_LINE}" # delete field from source class for i in field_names: if self.detected_field in i: field_names.remove(i) if field_names: self.token_stream_rewriter.replaceRange( from_idx=grand_parent_ctx.start.tokenIndex, to_idx=grand_parent_ctx.stop.tokenIndex, text=f"public {field_type} {','.join(field_names)};\n") else: self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, from_idx=grand_parent_ctx.start.tokenIndex, to_idx=grand_parent_ctx.stop.tokenIndex) self.detected_field = None
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.enter_class: return field_id = ctx.variableDeclarators().variableDeclarator( i=0).variableDeclaratorId().IDENTIFIER().getText() self.field_dict[field_id] = []
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if self.is_source_class: # field_identifier = ctx.variableDeclarators().getText().split(",") field_identifier = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() print(field_identifier) if self.moved_fields[0] in field_identifier: ctx1 = ctx.parentCtx.parentCtx start_index = ctx1.start.tokenIndex stop_index = ctx1.stop.tokenIndex self.field_text = self.token_stream_rewriter.getText( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, start=start_index, stop=stop_index) self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, from_idx=ctx1.start.tokenIndex, to_idx=ctx1.stop.tokenIndex) print(self.field_text)
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.is_source_class: return None # field_names = ctx.variableDeclarators().getText().split(",") field_identifier = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() field_names = list() field_names.append(field_identifier) print("field_names=", field_names) print("Here") grand_parent_ctx = ctx.parentCtx.parentCtx if self.detected_field in field_names: if (not grand_parent_ctx.modifier()): # print("******************************************") modifier = "" else: modifier = grand_parent_ctx.modifier(0).getText() field_type = ctx.typeType().getText() self.code += f"{self.TAB}{modifier} {field_type} {self.detected_field};{self.NEW_LINE}" # delete field from source class # field_names.remove(self.detected_field) # if field_names: # self.token_stream_rewriter.replaceRange( # from_idx=grand_parent_ctx.start.tokenIndex, # to_idx=grand_parent_ctx.stop.tokenIndex, # text=f"{modifier} {field_type} {','.join(field_names)};" # ) # else: # self.token_stream_rewriter.delete( # program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, # from_idx=grand_parent_ctx.start.tokenIndex, # to_idx=grand_parent_ctx.stop.tokenIndex # ) self.detected_field = None
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): """ It gets the field name, if the field is one of the moved fields, we move it and delete it from the source program. """ if not self.is_source_class: return None field_identifier = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() field_names = list() field_names.append(field_identifier) # print("field_names=", field_names) grand_parent_ctx = ctx.parentCtx.parentCtx if self.detected_field in field_names: if (not grand_parent_ctx.modifier()): modifier = "" else: modifier = grand_parent_ctx.modifier(0).getText() field_type = ctx.typeType().getText() self.code += f"{self.TAB}{modifier} {field_type} {self.detected_field};{self.NEW_LINE}" # delete field from source class ==>new start_index = ctx.parentCtx.parentCtx.start.tokenIndex stop_index = ctx.parentCtx.parentCtx.stop.tokenIndex self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, from_idx=start_index, to_idx=stop_index) self.detected_field = None
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if ctx.typeType().getText() == self.source_class: self.aul.add_identifier( (ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText(), self.scope))
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.enter_class: return list_of_fields = ctx.variableDeclarators().getText().split(",") for field in list_of_fields: self.class_fields.append(field)
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): field_names = ctx.variableDeclarators().getText().split(",") for field in field_names: if field in self.fields_to_increase_visibility: for modifier in ctx.parentCtx.parentCtx.modifier(): if modifier.getText() == "private": self.token_stream_rewriter.replaceSingleToken( token=modifier.start, text="public")
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if ctx.typeType().getText() == self.source_class: flag = False for child in ctx.variableDeclarators().children: if child.getText() != ',': id = child.variableDeclaratorId().IDENTIFIER().getText() fields_used = self.aul.get_identifier_fields( (id, self.scope)) methods_used = self.aul.get_identifier_methods( (id, self.scope)) if len(self.intersection( fields_used, self.moved_fields)) > 0 or len( self.intersection(methods_used, self.moved_methods)) > 0: flag = True if flag == True: self.token_stream_rewriter.replaceRange( from_idx=ctx.typeType().start.tokenIndex, to_idx=ctx.typeType().stop.tokenIndex, text=f"{self.new_class}") for child in ctx.variableDeclarators().children: if child.getText() != ',': if type(child.children[-1]) == JavaParserLabeled.VariableInitializer1Context and \ type(child.children[-1].children[0]) == JavaParserLabeled.Expression4Context and \ child.children[-1].children[0].children[0].getText() == 'new' and \ len(child.children[-1].children[0].children) > 1 and \ type(child.children[-1].children[0].children[1]) == JavaParserLabeled.Creator1Context: if child.variableInitializer().expression( ).creator().createdName().getText( ) == self.source_class: self.token_stream_rewriter.replaceRange( from_idx=child. variableInitializer().expression().creator( ).createdName().start.tokenIndex, to_idx=child.variableInitializer( ).expression().creator().createdName( ).stop.tokenIndex, text=f"{self.new_class}")
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if self.inCreator == True: variableType = ctx.typeType().classOrInterfaceType().IDENTIFIER(0) if variableType.symbol.text in self.products_identifier: self.productVarTypeIndex.append(variableType.symbol.tokenIndex) self.productVarValueIndex.append([ variableType.symbol.text, ctx.variableDeclarators().variableDeclarator( 0).ASSIGN().symbol.tokenIndex, ctx.stop.tokenIndex ])
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.is_source_class: return None self.detected_field = ctx.variableDeclarators().getText().split(',') print("Here it is a field") grand_parent_ctx = ctx.parentCtx.parentCtx modifier = "" for i in range(0, len(grand_parent_ctx.modifier())): modifier += grand_parent_ctx.modifier(i).getText() modifier += " " field_type = ctx.typeType().getText() self.fieldcode += f"{self.TAB}{modifier} {field_type} {self.detected_field[0]};{self.NEW_LINE}"
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): last_scope = self.scope_handler.getScope()[-1] if last_scope == self.class_identifier: for variableDeclarator in ctx.variableDeclarators( ).variableDeclarator(): if variableDeclarator.variableDeclaratorId().getText( ) == self.field_identifier: interval = variableDeclarator.variableDeclaratorId( ).IDENTIFIER().getSourceInterval() self.token_stream_rewriter.replaceRange( interval[0], interval[1], self.new_field_identifier)
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.is_source_class or self.inner_class_count != 0: return None field_identifier = ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().getText() if self.field_name == field_identifier: grand_parent_ctx = ctx.parentCtx.parentCtx self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, from_idx=grand_parent_ctx.start.tokenIndex, to_idx=grand_parent_ctx.stop.tokenIndex ) self.detected_field = None
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): fieldIdentifier = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() grandParentCtx = ctx.parentCtx.parentCtx if self.Field and self.FieldIndex < len(self.Fields) and self.Fields[ self.FieldIndex].split('/')[1] == fieldIdentifier: if self.IsSourceClassForFields[self.FieldIndex]: self.CodeRewrite.delete(self.CodeRewrite.DEFAULT_PROGRAM_NAME, grandParentCtx.start.tokenIndex, grandParentCtx.stop.tokenIndex) self.FieldIndex += 1
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): ctx1 = ctx.parentCtx.parentCtx.parentCtx.parentCtx class_identifier = ctx1.IDENTIFIER().getText() if class_identifier in self.child: # field_identifier = ctx.variableDeclarators().getText().split(",") field_identifier = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() if self.field in field_identifier: ctx1 = ctx.parentCtx.parentCtx start_index = ctx1.start.tokenIndex stop_index = ctx1.stop.tokenIndex self.field_text = self.token_stream_rewriter.getText( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, start=start_index, stop=stop_index)
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='' )
def enterFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): # if self.is_source_class: ctx1 = ctx.parentCtx.parentCtx.parentCtx.parentCtx class_identifier = ctx1.IDENTIFIER().getText() if class_identifier in self.children_class: # class_identifier = ctx.variableDeclarators().variableDeclarator().variableDeclaratorId().IDENTIFIER.getText() # field_identifier = ctx.variableDeclarators().getText().split(",") field_identifier = ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().IDENTIFIER().getText() # print(class_identifier) # for item in self.moved_fields: if self.moved_fields[0] in field_identifier: ctx1 = ctx.parentCtx.parentCtx start_index = ctx1.start.tokenIndex stop_index = ctx1.stop.tokenIndex self.field_text = self.token_stream_rewriter.getText( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, start=start_index, stop=stop_index) # delete field from source class # self.token_stream_rewriter.replaceRange( # from_idx=ctx1.start.tokenIndex, # to_idx=ctx1.stop.tokenIndex, # text=f"{modifier} {field_type} {','.join(field_names)};" # ) # else: self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, from_idx=ctx1.start.tokenIndex, to_idx=ctx1.stop.tokenIndex)
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if not self.is_source_class: return None grand_parent_ctx = ctx.parentCtx.parentCtx # field_identifier = ctx.variableDeclarators().getText().split(",") field_identifier = ctx.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().getText() if self.field_name in field_identifier: if grand_parent_ctx.modifier() == []: self.token_stream_rewriter.replaceRange( from_idx=ctx.typeType().start.tokenIndex, to_idx=ctx.typeType().stop.tokenIndex, text='private ' + ctx.typeType().getText() ) elif grand_parent_ctx.modifier(0).getText() == 'public': self.token_stream_rewriter.replaceRange( from_idx=grand_parent_ctx.modifier(0).start.tokenIndex, to_idx=grand_parent_ctx.modifier(0).stop.tokenIndex, text='private') elif grand_parent_ctx.modifier(0).getText() != 'private': self.token_stream_rewriter.replaceRange( from_idx=grand_parent_ctx.modifier(0).start.tokenIndex, to_idx=grand_parent_ctx.modifier(0).stop.tokenIndex, text='private ' + grand_parent_ctx.modifier(0).getText()) # generate accessor and mutator methods # Accessor body new_code = '\n\t' new_code += 'public ' + ctx.typeType().getText() + ' get' + str.capitalize(self.field_name) new_code += '() { \n\t\t return this.' + self.field_name + ';' + '\n\t}' # Mutator body new_code += '\n\t' new_code += 'public void set' + str.capitalize(self.field_name) new_code += '(' + ctx.typeType().getText() + ' ' + self.field_name + ') { \n\t\t' new_code += 'this.' + self.field_name + ' = ' + self.field_name + ';' + '\n\t}\n' self.token_stream_rewriter.insertAfter(ctx.stop.tokenIndex, new_code)
def exitFieldDeclaration(self, ctx: JavaParserLabeled.FieldDeclarationContext): if self.in_source_class and self.in_selected_package: if ctx.variableDeclarators().variableDeclarator( 0).variableDeclaratorId().getText( ) == self.field_identifier: if not ctx.parentCtx.parentCtx.modifier(0): self.token_stream_rewriter.insertBeforeIndex( index=ctx.typeType().stop.tokenIndex, text='private ') elif ctx.parentCtx.parentCtx.modifier(0).getText() == 'public': self.token_stream_rewriter.replaceRange( from_idx=ctx.parentCtx.parentCtx.modifier( 0).start.tokenIndex, to_idx=ctx.parentCtx.parentCtx.modifier( 0).stop.tokenIndex, text='private') else: return for c in ctx.parentCtx.parentCtx.parentCtx.classBodyDeclaration( ): try: print('method name: ' + c.memberDeclaration(). methodDeclaration().IDENTIFIER().getText()) if c.memberDeclaration().methodDeclaration().IDENTIFIER() \ .getText() == 'get' + str.capitalize( self.field_identifier): self.getter_exist = True if c.memberDeclaration().methodDeclaration().IDENTIFIER() \ .getText() == 'set' + str.capitalize( self.field_identifier): self.setter_exist = True except: logger.error("not method !!!") logger.debug("setter find: " + str(self.setter_exist)) logger.debug("getter find: " + str(self.getter_exist)) # generate accessor and mutator methods # Accessor body new_code = '' if not self.getter_exist: new_code = '\n\t// new getter method\n\t' new_code += 'public ' + ctx.typeType().getText() + \ ' get' + str.capitalize(self.field_identifier) new_code += '() { \n\t\treturn this.' + self.field_identifier \ + ';' + '\n\t}\n' # Mutator body if not self.setter_exist: new_code += '\n\t// new setter method\n\t' new_code += 'public void set' + str.capitalize( self.field_identifier) new_code += '(' + ctx.typeType().getText() + ' ' \ + self.field_identifier + ') { \n\t\t' new_code += 'this.' + self.field_identifier + ' = ' \ + self.field_identifier + ';' + '\n\t}\n' self.token_stream_rewriter.insertAfter(ctx.stop.tokenIndex, new_code) hidden = self.token_stream.getHiddenTokensToRight( ctx.stop.tokenIndex)