1 19 20 package org.netbeans.modules.java.source.builder; 21 22 import org.netbeans.api.java.source.query.CommentHandler; 23 import org.netbeans.api.java.source.ElementUtilities; 24 import org.netbeans.api.java.source.transform.UndoList; 25 import org.netbeans.api.java.source.transform.UndoList; 26 import org.netbeans.modules.java.source.save.Commit; 27 import org.netbeans.modules.java.source.engine.ApplicationContext; 28 import org.netbeans.modules.java.source.engine.DefaultApplicationContext; 29 import org.netbeans.modules.java.source.engine.ElementMaker; 30 import org.netbeans.modules.java.source.engine.EngineEnvironment; 31 import org.netbeans.modules.java.source.engine.ReattributionException; 32 import org.netbeans.modules.java.source.engine.RootTree; 33 import org.netbeans.modules.java.source.engine.StringSourceRewriter; 34 import org.netbeans.modules.java.source.engine.SourceRewriter; 35 36 import com.sun.source.tree.CompilationUnitTree; 37 import com.sun.source.util.JavacTask; 38 39 import com.sun.tools.javac.api.JavacTaskImpl; 40 import com.sun.tools.javac.api.JavacTrees; 41 import com.sun.tools.javac.code.Symtab; 42 import com.sun.tools.javac.jvm.ClassReader; 43 import com.sun.tools.javac.parser.Keywords; 44 import com.sun.tools.javac.model.JavacElements; 45 import com.sun.tools.javac.util.*; 46 47 import javax.lang.model.util.Elements; 48 import javax.lang.model.util.Types; 49 import javax.tools.JavaFileManager; 50 import javax.tools.JavaFileObject; 51 52 import java.io.IOException ; 53 import java.io.PrintWriter ; 54 import java.util.List ; 55 import java.util.ArrayList ; 56 import javax.swing.text.BadLocationException ; 57 import javax.tools.JavaFileObject; 58 import org.netbeans.modules.java.source.engine.TreeMakerInt; 59 60 63 public class DefaultEnvironment implements EngineEnvironment { 64 protected JavacTaskImpl task; 65 protected Context context; 66 protected ApplicationContext appContext; 67 ElementUtilities elementUtils; 68 final private long whenCreated; 69 70 public DefaultEnvironment() { 71 this(null, new DefaultApplicationContext(), "1.5"); 72 } 73 public DefaultEnvironment(JavacTask task, CompilationUnitTree tree, String source, 74 ApplicationContext appContext) throws ReattributionException { 75 this(task, appContext, source); 76 List<CompilationUnitTree> units = (tree != null) ? 77 java.util.Arrays.asList(tree) : new ArrayList <CompilationUnitTree>(); 78 makeRoot(units); 79 } 80 public DefaultEnvironment(JavacTask task, Iterable <? extends CompilationUnitTree> trees) { 81 this(task, new DefaultApplicationContext(), "1.5"); 82 makeRoot(trees); 83 } 84 public DefaultEnvironment(JavacTask task, Iterable <? extends CompilationUnitTree> trees, 85 ApplicationContext appContext) { 86 this(task, appContext, "1.5"); 87 makeRoot(trees); 88 } 89 public DefaultEnvironment(JavacTask javacTask, ApplicationContext appContext, String source) { 90 this.task = ((JavacTaskImpl)javacTask); 91 this.context = task != null ? task.getContext() : new Context(); 92 this.appContext = appContext; 93 Options.instance(getContext()).put("-source", source); whenCreated = System.currentTimeMillis(); 95 RedirectedLog.setLogWriter(context, appContext.getOutputWriter(null)); 96 if (context.get(JavaFileManager.class) == null) 97 DefaultFileManager.preRegister(context); 98 } 99 private void makeRoot(Iterable <? extends CompilationUnitTree> trees) { 100 try { 101 List<CompilationUnitTree> units = new ArrayList <CompilationUnitTree>(); 102 if (trees != null) 103 for (CompilationUnitTree t : trees) 104 units.add(t); 105 getModel().setRoot(new RootTree(units)); 106 } catch (ReattributionException ex) { 107 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 108 ex.getMessage(), ex); 109 } 110 } 111 public JavacTaskImpl getTask() { 112 return task; 113 } 114 public void setTask(JavacTaskImpl task) { 115 this.task = task; 116 this.elementUtils = new ElementUtilities(task); 117 } 118 public Context getContext() { 119 return context; 120 } 121 public void setContext(Context context) { 122 this.context = context; 123 } 124 public ASTService getModel() { 125 return ASTService.instance(getContext()); 126 } 127 public com.sun.source.util.Trees getTrees() { 128 return JavacTrees.instance(context); 129 } 130 public CommentHandler getCommentHandler() { 131 return CommentHandlerService.instance(getContext()); 132 } 133 public Keywords getKeywords() { 134 return Keywords.instance(getContext()); 135 } 136 public Name.Table getNameTable() { 137 return Name.Table.instance(getContext()); 138 } 139 public Symtab getSymbolTable() { 140 return Symtab.instance(getContext()); 141 } 142 public Types getTypes() { 143 return TypesService.instance(getContext()); 144 } 145 public com.sun.tools.javac.code.Types getJavacTypes() { 146 return com.sun.tools.javac.code.Types.instance(getContext()); 147 } 148 public TreeMakerInt getTreeMaker() { 149 return TreeFactory.instance(getContext()); 150 } 151 public ElementMaker getElementMaker() { 152 return ElementFactory.instance(getContext()); 153 } 154 public UndoList getUndoList() { 155 return UndoListService.instance(getContext()); 156 } 157 public String getOption(String key) { 158 return Options.instance(getContext()).get(key); 159 } 160 public void setOption(String key, String value) { 161 Options.instance(getContext()).put(key, value); 162 } 163 public RootTree getRootNode() { 164 return (RootTree)getModel().getRoot(); 165 } 166 public ApplicationContext getApplicationContext() { 167 return appContext; 168 } 169 public JavaFileManager getJavaFileManager() { 170 return getContext().get(JavaFileManager.class); 171 } 172 public String toSource(CompilationUnitTree unit) throws IOException { 173 StringSourceRewriter out = new StringSourceRewriter(); 174 Commit commit = new Commit(); 175 commit.init(); 176 commit.attach(this); 177 try { 178 commit.commit(unit, out); 179 } catch (BadLocationException e) { 180 throw new IOException (e.toString()); 181 } finally { 182 commit.release(); 183 commit.destroy(); 184 } 185 out.close(true); 186 return out.toString(); 187 } 188 public ClassReader getClassReader() { 189 return ClassReader.instance(getContext()); 190 } 191 public long whenCreated() { 192 return whenCreated; 193 } 194 public Elements getElements() { 195 return JavacElements.instance(getContext()); 196 } 197 public ElementUtilities getElementUtilities() { 198 if (elementUtils == null) 199 elementUtils = new ElementUtilities(task); 200 return elementUtils; 201 } 202 203 public void setResult(Object result, String title) { 204 appContext.setResult(result, title); 205 } 206 207 public void setStatusMessage(String message) { 208 appContext.setStatusMessage(message); 209 } 210 211 public void setErrorMessage(String message, String title) { 212 appContext.setErrorMessage(message, title); 213 } 214 215 public PrintWriter getOutputWriter(String title) { 216 return appContext.getOutputWriter(title); 217 } 218 219 public SourceRewriter getSourceRewriter(JavaFileObject sourceFile) throws IOException { 220 return appContext.getSourceRewriter(sourceFile); 221 } 222 223 227 private static class RedirectedLog extends Log { 228 static void setLogWriter(Context context, PrintWriter out) { 229 if (context.get(logKey) == null) { 232 new RedirectedLog(context, out); 233 } 234 } 235 protected RedirectedLog(Context context, PrintWriter out) { 236 super(context, out); 237 } 238 public void report(JCDiagnostic diagnostic) { 239 if (diagnostic.getType() == JCDiagnostic.DiagnosticType.ERROR) 241 super.report(diagnostic); 242 } 243 } 244 } 245 | Popular Tags |