1 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 ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import javax.lang.model.util.Elements; 29 import javax.lang.model.util.Types; 30 import javax.swing.text.Document ; 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 47 public class CompilationInfo { 48 49 private JavaSource.Phase phase = JavaSource.Phase.MODIFIED; 50 private CompilationUnitTree compilationUnit; 51 private List <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 { 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 <Diagnostic>(); 77 } 78 79 81 85 public JavaSource.Phase getPhase() { 86 return this.phase; 87 } 88 89 94 public CompilationUnitTree getCompilationUnit() { 95 if (this.jfo == null) { 96 throw new IllegalStateException (); 97 } 98 return this.compilationUnit; 99 } 100 101 105 public String getText() { 106 if (this.jfo == null) { 107 throw new IllegalStateException (); 108 } 109 try { 110 return this.jfo.getCharContent(false).toString(); 111 } catch (IOException ioe) { 112 ErrorManager.getDefault().notify(ioe); 114 return null; 115 } 116 } 117 118 public TokenHierarchy getTokenHierarchy() { 119 if (this.jfo == null) { 120 throw new IllegalStateException (); 121 } 122 try { 123 return ((SourceFileObject) this.jfo).getTokenHierarchy(); 124 } catch (IOException ioe) { 125 ErrorManager.getDefault().notify(ioe); 127 return null; 128 } 129 } 130 131 135 public List <Diagnostic> getDiagnostics() { 136 if (this.jfo == null) { 137 throw new IllegalStateException (); 138 } 139 ArrayList <Diagnostic> localErrors = new ArrayList <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 getDocument() throws IOException { 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 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 227 private static class DiagnosticListenerImpl implements DiagnosticListener { 228 229 private final List <Diagnostic> errors; 230 231 public DiagnosticListenerImpl(final List <Diagnostic> errors) { 232 this.errors = errors; 233 } 234 235 public void report(Diagnostic message) { 236 errors.add(message); 237 } 238 } 239 } 240 | Popular Tags |