def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.is_source_class: return None method_identifier = ctx.IDENTIFIER().getText() if self.detected_method == method_identifier: start_index = ctx.start.tokenIndex stop_index = ctx.stop.tokenIndex method_text = self.token_stream_rewriter.getText( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, start=start_index, stop=stop_index) self.code += self.NEW_LINE + ("public " + method_text + self.NEW_LINE) # delegate method body in source class if self.method_map.get(method_identifier): self.parameters.append("this") self.token_stream_rewriter.replaceRange( from_idx=ctx.methodBody().start.tokenIndex, to_idx=stop_index, text="{" + f"\nreturn this.{self.object_name}.{self.detected_method}(" + ",".join(self.parameters) + ");\n" + "}") self.parameters = [] self.detected_method = None
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.is_source_class: return None method_identifier = ctx.IDENTIFIER().getText() if method_identifier in self.moved_methods: self.detected_method = method_identifier
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.is_target_class: if ctx.IDENTIFIER().getText() in self.target_methods: grand_parent_ctx = ctx.parentCtx.parentCtx if grand_parent_ctx.modifier(): if len(grand_parent_ctx.modifier()) == 2: self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, from_idx=grand_parent_ctx.modifier( 1).start.tokenIndex - 1, to_idx=grand_parent_ctx.modifier( 1).stop.tokenIndex) else: if grand_parent_ctx.modifier(0).getText() == 'static': self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, from_idx=grand_parent_ctx.modifier( 0).start.tokenIndex - 1, to_idx=grand_parent_ctx.modifier( 0).stop.tokenIndex) else: return None
def enterMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if len(self.enter_method_body_stack) > 0: return if hasattr(ctx.parentCtx.parentCtx, 'modifier'): modifiers = ctx.parentCtx.parentCtx.modifier() else: modifiers = ctx.parentCtx.parentCtx.parentCtx.modifier() do_extract = True for modifier in modifiers: if modifier.classOrInterfaceModifier() is not None: # print('modifier.classOrInterfaceModifier().getText()', modifier.classOrInterfaceModifier().getText()) # Todo: Requires better handling if "private" in modifier.classOrInterfaceModifier().getText() or \ "static" in modifier.classOrInterfaceModifier().getText() or \ '?' in modifier.classOrInterfaceModifier().getText(): do_extract = False break # Todo: Requires better handling if '?' in ctx.getChild(0).getText(): do_extract = False if do_extract: method = {'name': ctx.IDENTIFIER().getText(), 'return_type': ctx.typeTypeOrVoid().getText(), 'formal_parameters': []} if ctx.formalParameters().formalParameterList() is not None: if hasattr(ctx.formalParameters().formalParameterList(), 'formalParameter'): for f in ctx.formalParameters().formalParameterList().formalParameter(): _type = f.typeType().getText() identifier = f.variableDeclaratorId().getText() method['formal_parameters'].append([_type, identifier]) self.interface_info['methods'].append(method)
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.source_method == ctx.IDENTIFIER().getText(): self.token_stream_rewriter.replaceRange( from_idx=ctx.parentCtx.parentCtx.start.tokenIndex, to_idx=ctx.parentCtx.parentCtx.stop.tokenIndex, text="")
def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): """ It gets the method name, if the method is one of the moved methods, we move it to the subclass and delete it from the source program. """ if not self.is_source_class: return None method_identifier = ctx.IDENTIFIER().getText() if self.detected_method == method_identifier: start_index = ctx.parentCtx.parentCtx.start.tokenIndex stop_index = ctx.stop.tokenIndex method_text = self.token_stream_rewriter.getText( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, start=start_index, stop=stop_index) self.code += (self.NEW_LINE + self.TAB + method_text + self.NEW_LINE) # delete method from source class self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, from_idx=start_index, to_idx=stop_index) self.detected_method = None
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): self.methods_name.append(ctx.IDENTIFIER().getText()) # checks if this is the method containing target lines if ctx.start.line <= self.first_line and ctx.stop.line >= self.last_line: print("Found method containing target lines.") self.is_in_target_method = True self.is_result_valid = True # checks if method is static for modifier in ctx.parentCtx.parentCtx.modifier(): if modifier.getText() == 'static': self.is_target_method_static = True print("Target Method is static.") break # checks if method throws any exception if ctx.qualifiedNameList(): self.exception_thrown_in_target_method = ctx.qualifiedNameList( ).getText() print("Target Method throws exception.") # TODO : check extracted lines for exception occurrence instead , # as they may not throw exception even though their parent method does # save method's last line number self.method_stop_line = ctx.stop.line
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.in_class: name = ctx.IDENTIFIER() if name.getText() == self.method_name: node = name.getSymbol() self.token_stream_rewriter.replaceIndex( node.tokenIndex, self.new_method_name) self.changed = True
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.enter_class: return m = [] m_name = ctx.IDENTIFIER().getText() self.method_no = self.method_no + 1 m.append(m_name) m.append(self.method_no) self.method_name.append(m)
def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): grandParentCtx = ctx.parentCtx.parentCtx methodIdentifier = ctx.IDENTIFIER().getText() if self.Method and self.MethodIndex < len( self.Methods) and self.Methods[self.MethodIndex].split( '/')[1] == methodIdentifier: if self.IsSourceClassForMethods[self.MethodIndex]: self.CodeRewrite.delete(self.CodeRewrite.DEFAULT_PROGRAM_NAME, grandParentCtx.start.tokenIndex, grandParentCtx.stop.tokenIndex) self.MethodIndex += 1
def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.is_source_class or self.inner_class_count != 0: return None grand_parent_ctx = ctx.parentCtx.parentCtx method_identifier = ctx.IDENTIFIER().getText() if self.method_name == method_identifier: 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_method = None
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): grand_parent_ctx = ctx.parentCtx.parentCtx self.method_selected = False if ctx.IDENTIFIER().getText() == self.method_identifier: self.enter_method = True self.method_selected = True self.old_method_Declaration = self.token_stream_rewriter.getText( "", grand_parent_ctx.modifier(0).start.tokenIndex, ctx.formalParameters().stop.tokenIndex) self.old_method_Declaration = self.old_method_Declaration.replace( self.method_identifier, "doOperation")
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): self.fields = self.method_map.get(ctx.IDENTIFIER().getText()) if self.fields: if ctx.formalParameters().getText() == "()": text = f"{self.source_class} ref" else: text = f", {self.source_class} ref" self.token_stream_rewriter.insertBeforeToken( token=ctx.formalParameters().stop, text=text, program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME)
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): """ It sets the detected field to the method if it is one of the moved methods. """ if not self.is_source_class: return None method_identifier = ctx.IDENTIFIER().getText() if method_identifier in self.moved_methods: self.detected_method = method_identifier
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): methodIdentifier = ctx.IDENTIFIER().getText() if self.Variable: for i in range(len(self.Variables)): if self.IsSourceClassForVariables[i] and self.Variables[ i].split('/')[1] == methodIdentifier: self.IsSourceMethodForVariables[i] = True if self.Parameter: for i in range(len(self.Parameters)): if self.IsSourceClassForParameters[i] and self.Parameters[ i].split('/')[1] == methodIdentifier: self.IsSourceMethodForParameters[i] = True pass
def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.is_source_class: return None grand_parent_ctx = ctx.parentCtx.parentCtx method_identifier = ctx.IDENTIFIER().getText() if self.method_name in method_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 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 enterMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.is_target_class: if ctx.IDENTIFIER().getText() in self.target_methods: if 'this.' in ctx.getText(): raise ValueError("this method can not refactor") grand_parent_ctx = ctx.parentCtx.parentCtx if grand_parent_ctx.modifier(): if len(grand_parent_ctx.modifier()) == 2: return None else: self.token_stream_rewriter.insertAfter( index=grand_parent_ctx.modifier(0).stop.tokenIndex, program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, text=" static" ) else: self.token_stream_rewriter.insertBeforeIndex( index=ctx.start.tokenIndex, text="static " )
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): """check if this is the intended method if so capture signature and remove boolean argument Args: ctx (JavaParserLabeled.MethodDeclarationContext): """ self.is_source_method = ( ctx.IDENTIFIER().getText() == self.source_method) if self.is_source_method: nextParam = None for idx, formalParameter in enumerate(ctx.formalParameters( ).formalParameterList().formalParameter()): if formalParameter.variableDeclaratorId().IDENTIFIER().getText( ) == self.argument_name: self.argument_token = formalParameter nextParam = ctx.formalParameters().formalParameterList().formalParameter()[idx + 1] \ if idx != len(ctx.formalParameters().formalParameterList().formalParameter()) - 1 else None break if nextParam: self.token_stream_rewriter.replaceRange( self.argument_token.start.tokenIndex, nextParam.start.tokenIndex - 1, '') else: self.token_stream_rewriter.replaceRange( self.argument_token.start.tokenIndex, self.argument_token.stop.tokenIndex, '') self.signature = self.token_stream_rewriter.getText( self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, ctx.start.tokenIndex, ctx.methodBody().start.tokenIndex) if self.token_stream_rewriter_changed == False: self.token_stream_rewriter = TokenStreamRewriter( self.common_token_stream) self.token_stream_rewriter_changed = True
def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.is_source_class: return None method_identifier = ctx.IDENTIFIER().getText() print("Here it is a method") 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 += " " if self.detected_method == method_identifier: start_index = ctx.start.tokenIndex stop_index = ctx.stop.tokenIndex method_text = self.token_stream_rewriter.getText( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, start=start_index, stop=stop_index) self.methodcode += (self.NEW_LINE + self.TAB + modifier + method_text + self.NEW_LINE)
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.is_children_class: method_identifier = ctx.IDENTIFIER().getText() if self.moved_methods == method_identifier: methodDefctx = ctx.parentCtx.parentCtx start_index = methodDefctx.start.tokenIndex stop_index = methodDefctx.stop.tokenIndex self.method_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=methodDefctx.start.tokenIndex, to_idx=methodDefctx.stop.tokenIndex) else: return None
def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.is_source_class: return None grand_parent_ctx = ctx.parentCtx.parentCtx method_identifier = ctx.IDENTIFIER().getText() if self.method_name in method_identifier: if grand_parent_ctx.modifier() is None or len( grand_parent_ctx.modifier()) == 0: self.token_stream_rewriter.replaceRange( from_idx=ctx.typeTypeOrVoid().start.tokenIndex, to_idx=ctx.typeTypeOrVoid().stop.tokenIndex, text='static ' + ctx.typeTypeOrVoid().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 enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): self.Methods.append(str(ctx.IDENTIFIER())) self.MethodParameters.append(str(ctx.formalParameters()))
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): self.current_method = ctx.IDENTIFIER().getText() if self.current_method == self.target_method and self.current_class == self.target_class: self.target_method_ctx = ctx
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): methods.append(f"{ctx.IDENTIFIER()} ") returntype.append(ctx.typeTypeOrVoid().getText()) if ctx.IDENTIFIER().getText() == self.method_name: found_func.append(ctx.IDENTIFIER())
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if not self.is_source_class: return None self.detected_method = ctx.IDENTIFIER().getText()
def exitMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.is_source_class and ctx.IDENTIFIER().getText( ) == self.method_name: self.detected_method = True
def enterMethodDeclaration(self, ctx: JavaParserLabeled.MethodDeclarationContext): # Extract Methods Name & Methods body of SuperClass if self.InSuperClass: new_class_name = ctx.IDENTIFIER().getText() new_method_body = ctx.methodBody().getText() self.Visitors[new_class_name] = [new_method_body]
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.is_member and ctx.IDENTIFIER().getText() == self.method_name: self.do_delete = True
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if ctx.IDENTIFIER().getText() == self.source_method: self.detected_method = True
def enterMethodDeclaration( self, ctx: JavaParserLabeled.MethodDeclarationContext): if self.is_source_class or self.is_target_class: if ctx.formalParameters().formalParameterList(): method_parameters = [ ctx.formalParameters().formalParameterList().children[i] for i in range( len(ctx.formalParameters().formalParameterList(). children)) if i % 2 == 0 ] else: method_parameters = [] method_text = '' for modifier in ctx.parentCtx.parentCtx.modifier(): method_text += modifier.getText() + ' ' type_text = ctx.typeTypeOrVoid().getText() if type_text == self.source_class: type_text = self.target_class if self.is_target_class: self.token_stream_rewriter.replace( program_name=self.token_stream_rewriter. DEFAULT_PROGRAM_NAME, from_idx=ctx.typeTypeOrVoid().start.tokenIndex, to_idx=ctx.typeTypeOrVoid().stop.tokenIndex, text=type_text) method_text += type_text + ' ' + ctx.IDENTIFIER().getText() method_text += ' ( ' for parameter in method_parameters: method_text += parameter.typeType().getText() + ' ' method_text += parameter.variableDeclaratorId().getText( ) + ', ' if method_parameters: method_text = method_text[:len(method_text) - 2] method_text += ')\n\t{' method_text += self.token_stream_rewriter.getText( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, start=ctx.methodBody().start.tokenIndex + 1, stop=ctx.methodBody().stop.tokenIndex - 1) method_text += '}\n' if self.is_source_class: self.source_class_data['methods'].append( ConstructorOrMethod( name=ctx.IDENTIFIER().getText(), parameters=[ Parameter(parameter_type=p.typeType().getText(), name=p.variableDeclaratorId().IDENTIFIER( ).getText()) for p in method_parameters ], text=method_text)) else: self.target_class_data['methods'].append( ConstructorOrMethod( name=ctx.IDENTIFIER().getText(), parameters=[ Parameter(parameter_type=p.typeType().getText(), name=p.variableDeclaratorId().IDENTIFIER( ).getText()) for p in method_parameters ], text=method_text))