1 19 20 package org.netbeans.modules.java.source.save; 21 22 import org.netbeans.modules.java.source.engine.EngineEnvironment; 23 import org.netbeans.modules.java.source.engine.EngineEnvironment; 24 import com.sun.source.tree.CompilationUnitTree; 25 import org.netbeans.modules.java.source.engine.EngineEnvironment; 26 import org.netbeans.api.java.source.query.QueryEnvironment; 27 import org.netbeans.api.java.source.query.Query; 28 import org.netbeans.modules.java.source.pretty.VeryPretty; 29 import org.netbeans.modules.java.source.pretty.ImportAnalysis; 30 31 import com.sun.tools.javac.tree.*; 32 import com.sun.tools.javac.tree.JCTree.*; 33 import com.sun.tools.javac.util.*; 34 35 import java.io.*; 36 37 public class Reformat extends Query<Void ,Object > { 38 public String destinationDirectory; 39 boolean displayedError = false; 40 Context context; 41 PrintWriter log; 42 int nWritten; 43 private EngineEnvironment ee; 44 45 @Override 46 public void attach(QueryEnvironment env) { 47 super.attach(env); 48 ee = (EngineEnvironment)env; 49 context = ee.getContext(); 50 Options options = Options.instance(context); 51 String saveDir = options.get("-save_directory"); 52 if (saveDir != null) 53 destinationDirectory = saveDir; 54 } 55 56 @Override 57 public void release() { 58 super.release(); 59 ee = null; 60 context = null; 61 log = null; 62 } 63 64 public void apply() { 65 nWritten = 0; 66 log = env.getOutputWriter("Formatting Files"); 67 super.apply(); 68 log.println("Wrote "+nWritten+" files"); 69 log.close(); 70 log = null; 71 } 72 73 @Override 74 public Void visitCompilationUnit(CompilationUnitTree node, Object p) { 75 JCCompilationUnit topLevel = (JCCompilationUnit)node; 76 String sourceName = Commit.getSourceFileName(topLevel); 77 File dfn = new File(destinationDirectory, sourceName); 78 log.println("Formatting "+dfn); 79 PrintWriter out; 80 try { 81 out = new PrintWriter(new BufferedWriter(new FileWriter(dfn))); 82 } catch (IOException ioe) { 83 if (!dfn.getParentFile().mkdirs()) { 84 if (!displayedError) 85 error("Couldn\'t create directory:\n" + dfn.getParent() + 86 "\n" + ioe.getMessage()); 87 displayedError = true; 88 return null; 89 } 90 try { 91 out = new PrintWriter(new BufferedWriter(new FileWriter(dfn))); 92 } 93 catch (IOException ioe2) { 94 if (!displayedError) 95 error("Couldn\'t create file:\n" + dfn + "\n" + 96 ioe2.getMessage()); 97 displayedError = true; 98 return null; 99 } 100 } 101 102 try { 103 VeryPretty pretty = new VeryPretty(context); 104 ImportAnalysis imports = new ImportAnalysis(pretty.options.starThreshold, 105 pretty.options.useThreshold, 106 topLevel, ee.getSymbolTable(), 107 ee.getJavacTypes()); 108 pretty.setImports(imports); 109 if (imports.containingClass != null) 110 pretty.enclClassName = imports.containingClass.name; 111 pretty.printImports(); 112 for (List<JCTree> defs = topLevel.defs; defs.tail != null; defs = defs.tail) { 113 defs.head.accept(pretty); 114 if (defs.tail != null) 115 pretty.print('\n'); 116 } 117 pretty.writeTo(out); 118 out.close(); 119 nWritten++; 120 } catch(Throwable err) { 121 String msg = "Error writing " + sourceName + ": " + err; 122 try { 123 log.println(msg); 124 msg = null; 125 } catch(Throwable t) {} 126 if(msg != null) error(msg); 127 displayedError = true; 128 } 129 return null; 130 } 131 } 132 | Popular Tags |