Java 使用eclipse插件将修改后的AST保存在新文件中

Java 使用eclipse插件将修改后的AST保存在新文件中,java,eclipse,eclipse-plugin,abstract-syntax-tree,eclipse-jdt,Java,Eclipse,Eclipse Plugin,Abstract Syntax Tree,Eclipse Jdt,我有一个eclipse插件代码来操作项目/工作区中的类(smcho.Hello)。 我可以创建一个CompliationUnit并对其进行一些修改,但我需要将结果保存在不同的文件中,以检查两个版本之间的差异 这是我获取编译单元的代码 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IProject project = root.getProject("Hello"); project.open(null); IJav

我有一个eclipse插件代码来操作项目/工作区中的类(smcho.Hello)。 我可以创建一个CompliationUnit并对其进行一些修改,但我需要将结果保存在不同的文件中,以检查两个版本之间的差异

这是我获取编译单元的代码

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Hello");
project.open(null);
IJavaProject javaProject = JavaCore.create(project);
IType lwType = javaProject.findType("smcho.Hello");
org.eclipse.jdt.core.ICompilationUnit lwCompilationUnit = lwType.getCompilationUnit();
final ASTParser parser = ASTParser.newParser(AST.JLS3); 
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(lwCompilationUnit);
parser.setResolveBindings(true); // we need bindings later on
CompilationUnit unit = (CompilationUnit) parser.createAST(null /* IProgressMonitor */); 
// modify the unit AST node

如何将修改后的装置保存到新文件中?

您可以使用
ASTRewriter
来执行此操作

// get the ast rewriter
final ASTRewrite rewriter = ASTRewrite.create(ast);
// get the current document source
final Document document = new Document(unit.getSource());
// compute the edits you have made to the compilation unit
final TextEdit edits = rewriter.rewriteAST();
// apply the edits to the document
edits.apply(document);
// get the new source
String newSource = document.get();
// now write this source to some other file.
检查下面的链接。这提供了有关如何将AST更改写入文件的详细信息

更新: 以下是我写入文件的方式:

File file = new File(destFile);
FileUtils.writeStringToFile(File file, String newSource) 

您可以使用
ASTRewriter
来执行此操作

// get the ast rewriter
final ASTRewrite rewriter = ASTRewrite.create(ast);
// get the current document source
final Document document = new Document(unit.getSource());
// compute the edits you have made to the compilation unit
final TextEdit edits = rewriter.rewriteAST();
// apply the edits to the document
edits.apply(document);
// get the new source
String newSource = document.get();
// now write this source to some other file.
检查下面的链接。这提供了有关如何将AST更改写入文件的详细信息

更新: 以下是我写入文件的方式:

File file = new File(destFile);
FileUtils.writeStringToFile(File file, String newSource) 

这是我可以用来将重写的ast保存到另一个文件中的代码。我想知道是否有更简单的方法

Document document = new Document(lwCompilationUnit.getSource());
rewrite.rewriteAST().apply(document);
String source = document.get();
String destFile = "...";
Helper.toFile(source, destFile);

public static void toFile(String source, String outputPath)
{
   try{
          // Create file 
          FileWriter fstream = new FileWriter(outputPath);
          BufferedWriter out = new BufferedWriter(fstream);
          out.write(source);
          //Close the output stream
          out.close();
    }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
    }
}

这是我可以用来将重写的ast保存到另一个文件中的代码。我想知道是否有更简单的方法

Document document = new Document(lwCompilationUnit.getSource());
rewrite.rewriteAST().apply(document);
String source = document.get();
String destFile = "...";
Helper.toFile(source, destFile);

public static void toFile(String source, String outputPath)
{
   try{
          // Create file 
          FileWriter fstream = new FileWriter(outputPath);
          BufferedWriter out = new BufferedWriter(fstream);
          out.write(source);
          //Close the output stream
          out.close();
    }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
    }
}