KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > DefaultEnvironment


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
53 import java.io.PrintWriter JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import javax.swing.text.BadLocationException JavaDoc;
57 import javax.tools.JavaFileObject;
58 import org.netbeans.modules.java.source.engine.TreeMakerInt;
59
60 /**
61  * DefaultCommandEnvironment:
62  */

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 JavaDoc 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 JavaDoc<CompilationUnitTree>();
78         makeRoot(units);
79     }
80     public DefaultEnvironment(JavacTask task, Iterable JavaDoc<? extends CompilationUnitTree> trees) {
81         this(task, new DefaultApplicationContext(), "1.5");
82         makeRoot(trees);
83     }
84     public DefaultEnvironment(JavacTask task, Iterable JavaDoc<? extends CompilationUnitTree> trees,
85                               ApplicationContext appContext) {
86         this(task, appContext, "1.5");
87         makeRoot(trees);
88     }
89     public DefaultEnvironment(JavacTask javacTask, ApplicationContext appContext, String JavaDoc 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); // needed for Log initialization
94
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 JavaDoc<? extends CompilationUnitTree> trees) {
100         try {
101             List<CompilationUnitTree> units = new ArrayList JavaDoc<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 JavaDoc getOption(String JavaDoc key) {
158         return Options.instance(getContext()).get(key);
159     }
160     public void setOption(String JavaDoc key, String JavaDoc 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 JavaDoc toSource(CompilationUnitTree unit) throws IOException JavaDoc {
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 JavaDoc e) {
180             throw new IOException JavaDoc(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 JavaDoc result, String JavaDoc title) {
204         appContext.setResult(result, title);
205     }
206     
207     public void setStatusMessage(String JavaDoc message) {
208         appContext.setStatusMessage(message);
209     }
210     
211     public void setErrorMessage(String JavaDoc message, String JavaDoc title) {
212         appContext.setErrorMessage(message, title);
213     }
214     
215     public PrintWriter JavaDoc getOutputWriter(String JavaDoc title) {
216         return appContext.getOutputWriter(title);
217     }
218
219     public SourceRewriter getSourceRewriter(JavaFileObject sourceFile) throws IOException JavaDoc {
220         return appContext.getSourceRewriter(sourceFile);
221     }
222
223     /**
224      * com.sun.tools.javac.util.Log requires a subclass just to set its
225      * output writer.
226      */

227     private static class RedirectedLog extends Log {
228         static void setLogWriter(Context context, PrintWriter JavaDoc out) {
229             // There may be a Log service already registered in the context
230
// in the case when Javadoc is used.
231
if (context.get(logKey) == null) {
232                 new RedirectedLog(context, out);
233             }
234         }
235         protected RedirectedLog(Context context, PrintWriter JavaDoc out) {
236             super(context, out);
237         }
238         public void report(JCDiagnostic diagnostic) {
239             // suppress warnings and notes
240
if (diagnostic.getType() == JCDiagnostic.DiagnosticType.ERROR)
241                 super.report(diagnostic);
242         }
243     }
244 }
245
Popular Tags