def enterImportDeclaration( self, ctx: JavaParserLabeled.ImportDeclarationContext): if ctx.getText() == "import" + self.package_identifier + "." + self.class_identifier + ";" \ or ctx.getText() == "import" + self.package_identifier + ".*" + ";" \ or ctx.getText() == "import" + self.package_identifier + ";": self.is_package_imported = True print("package " + self.package_identifier + " imported")
def enterImportDeclaration(self, ctx: JavaParserLabeled.ImportDeclarationContext): if ctx.qualifiedName().IDENTIFIER()[-1].getText() == self.package_identifier: if self.package_new_name not in self.packages_name: self.token_stream_rewriter.replaceIndex( index=ctx.qualifiedName().start.tokenIndex + (2 * len(ctx.qualifiedName().IDENTIFIER()) - 2), text=self.package_new_name) print("package name in import changed")
def exitImportDeclaration(self, ctx: JavaParserLabeled.ImportDeclarationContext): text_to_replace = "import " + ctx.qualifiedName().getText() + ';' if ctx.STATIC() is not None: text_to_replace = text_to_replace.replace("import", "import static") self.code += text_to_replace + self.NEW_LINE
def enterImportDeclaration(self, ctx: JavaParserLabeled.ImportDeclarationContext): if ctx.getText() == "import" + self.package_identifier + "." + self.class_identifier + ";" \ or ctx.getText() == "import" + self.package_identifier + ".*" + ";" \ or ctx.getText() == "import" + self.package_identifier + ";": self.is_package_imported = True print("package " + self.package_identifier + " imported") if ctx.getText() == "import" + self.package_identifier + "." + self.class_identifier + ";": self.token_stream_rewriter.replaceIndex( index=ctx.qualifiedName().start.tokenIndex + 2 * len(ctx.qualifiedName().IDENTIFIER()) - 2, text=self.class_new_name) print("class name in package changed")
def exitImportDeclaration(self, ctx: JavaParserLabeled.ImportDeclarationContext): if ctx.qualifiedName().getText( ) != self.source_package + '.' + self.class_identifier: return start_index = ctx.start.tokenIndex stop_index = ctx.stop.tokenIndex text_to_replace = "import " + self.target_package + '.' + self.class_identifier + ';' if ctx.STATIC() is not None: text_to_replace = text_to_replace.replace("import", "import static") # replace the import source package with target package self.token_stream_rewriter.replace( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, from_idx=start_index, to_idx=stop_index, text=text_to_replace)
def enterImportDeclaration( self, ctx: JavaParserLabeled.ImportDeclarationContext): # import source_package.Sample; if self.target_package in ctx.getText(): self.imported = True if self.class_name in ctx.getText(): if self.target_package == self.current_package: replace_text = "" else: replace_text = f"import {self.target_package}.{self.class_name};\n" self.rewriter.replaceRangeTokens( from_token=ctx.start, to_token=ctx.stop, text=replace_text, program_name=self.rewriter.DEFAULT_PROGRAM_NAME) elif f"{self.source_package}.{self.class_name}" in ctx.getText(): self.rewriter.delete( program_name=self.rewriter.DEFAULT_PROGRAM_NAME, from_idx=ctx.start.tokenIndex, to_idx=ctx.stop.tokenIndex)
def exitImportDeclaration(self, ctx: JavaParserLabeled.ImportDeclarationContext): # extract the imported package from context imported_package = "" mul = None if ctx.qualifiedName() is not None: imported_package += ctx.qualifiedName().getText() if ctx.MUL() is not None: mul = ctx.MUL().getText() imported_package += '.' + ctx.MUL().getText() # return if the current import statement is not relevant to source package if self.source_package not in imported_package or (self.class_identifier not in imported_package and '*' not in imported_package): return start_index = ctx.start.tokenIndex stop_index = ctx.stop.tokenIndex target_exact_package = f"{self.target_package}.{self.class_identifier}" target_exact_import = f"import {target_exact_package};" if ctx.STATIC() is not None: target_exact_import = target_exact_import.replace("import", "import ") # handle import scenarios in files dependent on the moved class if target_exact_package in self.exact_imports: if mul is None: self.token_stream_rewriter.delete( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, from_idx=start_index, to_idx=stop_index + 1 ) else: if mul is not None: self.token_stream_rewriter.insertAfter( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, index=stop_index, text=self.NEW_LINE + target_exact_import ) else: print(ctx.getText()) self.token_stream_rewriter.replace( program_name=self.token_stream_rewriter.DEFAULT_PROGRAM_NAME, from_idx=start_index, to_idx=stop_index, text=target_exact_import ) self.exact_imports.append(target_exact_package)
def enterImportDeclaration( self, ctx: JavaParserLabeled.ImportDeclarationContext): if ctx.qualifiedName().getText() == self.package_name + '.' + self.source_class_name \ or ctx.qualifiedName().getText() == self.package_name: self.has_access_to_class = True
def enterImportDeclaration( self, ctx: JavaParserLabeled.ImportDeclarationContext): if f"{self.target_package}.{self.child_class}" in ctx.getText(): self.detected_package = True self.import_end = ctx.stop