KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > CompilationInfo


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source;
21
22 import com.sun.source.tree.CompilationUnitTree;
23 import com.sun.source.util.Trees;
24 import com.sun.tools.javac.api.JavacTaskImpl;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.lang.model.util.Elements;
29 import javax.lang.model.util.Types;
30 import javax.swing.text.Document JavaDoc;
31 import javax.tools.DiagnosticListener;
32 import javax.tools.Diagnostic;
33 import javax.tools.JavaFileObject;
34 import org.netbeans.api.lexer.TokenHierarchy;
35 import org.netbeans.modules.java.source.parsing.SourceFileObject;
36 import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation;
37 import org.openide.ErrorManager;
38 import org.openide.cookies.EditorCookie;
39 import org.openide.filesystems.FileObject;
40 import org.openide.loaders.DataObject;
41
42
43 /** Asorted information about the JavaSource.
44  *
45  * @author Petr Hrebejk, Tomas Zezula
46  */

47 public class CompilationInfo {
48     
49     private JavaSource.Phase phase = JavaSource.Phase.MODIFIED;
50     private CompilationUnitTree compilationUnit;
51     private List JavaDoc<Diagnostic> errors;
52     
53     private JavacTaskImpl javacTask;
54     private FileObject fo;
55     final JavaFileObject jfo;
56     final JavaSource javaSource;
57     boolean needsRestart;
58     
59     private ElementUtilities elementUtilities;
60     private TreeUtilities treeUtilities;
61     private CommentUtilities commentUtilities;
62     
63     CompilationInfo () {
64         this.javaSource = null;
65         this.jfo = null;
66         this.javacTask = null;
67         this.errors = null;
68     }
69     
70     CompilationInfo ( final JavaSource javaSource, final FileObject fo, final JavaFileFilterImplementation filter, final JavacTaskImpl javacTask) throws IOException JavaDoc {
71         assert javaSource != null;
72         this.javaSource = javaSource;
73         this.fo = fo;
74         this.jfo = fo != null ? javaSource.jfoProvider.createJavaFileObject(fo, filter) : null;
75         this.javacTask = javacTask;
76         this.errors = new ArrayList JavaDoc<Diagnostic>();
77     }
78              
79     // API of the class --------------------------------------------------------
80

81     /**
82      * Returns the current phase of the {@link JavaSource}.
83      * @return {@link JavaSource.Phase} the state which was reached by the {@link JavaSource}.
84      */

85     public JavaSource.Phase getPhase() {
86         return this.phase;
87     }
88        
89     /**
90      * Returns the javac tree representing the source file.
91      * @return {@link CompilationUnitTree} the compilation unit cantaining the top level classes contained in the,
92      * java source file. It may return null when the phase is less than {@link JavaSource.Phase#PARSED}
93      */

94     public CompilationUnitTree getCompilationUnit() {
95         if (this.jfo == null) {
96             throw new IllegalStateException JavaDoc ();
97         }
98         return this.compilationUnit;
99     }
100     
101     /**
102      * Returns the content of the file represented by the {@link JavaSource}.
103      * @return String the java source
104      */

105     public String JavaDoc getText() {
106         if (this.jfo == null) {
107             throw new IllegalStateException JavaDoc ();
108         }
109         try {
110             return this.jfo.getCharContent(false).toString();
111         } catch (IOException JavaDoc ioe) {
112             //Should never happen
113
ErrorManager.getDefault().notify(ioe);
114             return null;
115         }
116     }
117     
118     public TokenHierarchy getTokenHierarchy() {
119         if (this.jfo == null) {
120             throw new IllegalStateException JavaDoc ();
121         }
122         try {
123             return ((SourceFileObject) this.jfo).getTokenHierarchy();
124         } catch (IOException JavaDoc ioe) {
125             //Should never happen
126
ErrorManager.getDefault().notify(ioe);
127             return null;
128         }
129     }
130     
131     /**
132      * Returns the errors in the file represented by the {@link JavaSource}.
133      * @return an list of {@link Diagnostic}
134      */

135     public List JavaDoc<Diagnostic> getDiagnostics() {
136         if (this.jfo == null) {
137             throw new IllegalStateException JavaDoc ();
138         }
139         ArrayList JavaDoc<Diagnostic> localErrors = new ArrayList JavaDoc<Diagnostic>(errors.size());
140         for(Diagnostic m : errors) {
141             if (this.jfo == m.getSource())
142                 localErrors.add(m);
143         }
144         return localErrors;
145     }
146     
147     public Trees getTrees() {
148         return Trees.instance(getJavacTask());
149     }
150     
151     public Types getTypes() {
152         return getJavacTask().getTypes();
153     }
154     
155     public Elements getElements() {
156     return getJavacTask().getElements();
157     }
158         
159     public JavaSource getJavaSource() {
160         return javaSource;
161     }
162     
163     public ClasspathInfo getClasspathInfo() {
164     return javaSource.getClasspathInfo();
165     }
166     
167     public FileObject getFileObject() {
168         return fo;
169     }
170     
171     public Document JavaDoc getDocument() throws IOException JavaDoc {
172         if (this.fo == null) {
173             return null;
174         }
175         DataObject od = DataObject.find(fo);
176         EditorCookie ec = (EditorCookie) od.getCookie(EditorCookie.class);
177         if (ec != null) {
178             return ec.getDocument();
179         } else {
180             return null;
181         }
182     }
183     
184     public synchronized TreeUtilities getTreeUtilities() {
185         if (treeUtilities == null) {
186             treeUtilities = new TreeUtilities(this);
187         }
188         return treeUtilities;
189     }
190     
191     public synchronized ElementUtilities getElementUtilities() {
192         if (elementUtilities == null) {
193             elementUtilities = new ElementUtilities(this);
194
195         }
196         return elementUtilities;
197     }
198     
199     public synchronized CommentUtilities getCommentUtilities() {
200         if (commentUtilities == null) {
201             commentUtilities = new CommentUtilities(this);
202         }
203         return commentUtilities;
204     }
205     
206     // Package private methods -------------------------------------------------
207

208     void setPhase(final JavaSource.Phase phase) {
209         assert phase != null;
210         this.phase = phase;
211     }
212     
213     void setCompilationUnit(final CompilationUnitTree compilationUnit) {
214         assert compilationUnit != null;
215         this.compilationUnit = compilationUnit;
216     }
217     
218     synchronized JavacTaskImpl getJavacTask() {
219         if (javacTask == null) {
220             javacTask = javaSource.createJavacTask(new DiagnosticListenerImpl(errors));
221         }
222     return javacTask;
223     }
224     
225     // Innerclasses ------------------------------------------------------------
226

227     private static class DiagnosticListenerImpl implements DiagnosticListener {
228         
229         private final List JavaDoc<Diagnostic> errors;
230         
231         public DiagnosticListenerImpl(final List JavaDoc<Diagnostic> errors) {
232             this.errors = errors;
233         }
234         
235         public void report(Diagnostic message) {
236             errors.add(message);
237         }
238     }
239 }
240
Popular Tags