1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.tools.FileObject; 29 import javax.tools.JavaFileManager; 30 import javax.tools.JavaFileObject; 31 import javax.tools.StandardLocation; 32 import org.netbeans.modules.java.source.util.Iterators; 33 34 38 public class ProxyFileManager implements JavaFileManager { 39 40 private static final Location ALL = new Location () { 41 public String getName() { return "ALL";} 43 public boolean isOutputLocation() { return false; } 44 }; 45 46 private final JavaFileManager bootPath; 47 private final JavaFileManager classPath; 48 private final JavaFileManager sourcePath; 49 private final JavaFileManager outputhPath; 50 51 private JavaFileObject lastInfered; 52 private String lastInferedResult; 53 54 55 public ProxyFileManager(JavaFileManager bootPath, JavaFileManager classPath, JavaFileManager sourcePath, JavaFileManager outputhPath) { 56 assert bootPath != null; 57 assert classPath != null; 58 this.bootPath = bootPath; 59 this.classPath = classPath; 60 this.sourcePath = sourcePath; 61 this.outputhPath = outputhPath; 62 } 63 64 private JavaFileManager[] getFileManager (Location location) { 65 if (location == StandardLocation.CLASS_PATH) { 66 return this.outputhPath == null ? 67 new JavaFileManager[] {this.classPath} : 68 new JavaFileManager[] {this.classPath, this.outputhPath}; 69 } 70 else if (location == StandardLocation.PLATFORM_CLASS_PATH) { 71 return new JavaFileManager[] {this.bootPath}; 72 } 73 else if (location == StandardLocation.SOURCE_PATH && this.sourcePath != null) { 74 return new JavaFileManager[] {this.sourcePath}; 75 } 76 else if (location == StandardLocation.CLASS_OUTPUT && this.outputhPath != null) { 77 return new JavaFileManager[] {this.outputhPath}; 78 } 79 else if (location == ALL) { 80 return this.outputhPath == null ? 81 new JavaFileManager[] { 82 this.sourcePath, 83 this.bootPath, 84 this.classPath 85 }: 86 new JavaFileManager[] { 87 this.sourcePath, 88 this.bootPath, 89 this.classPath, 90 this.outputhPath 91 }; 92 } 93 else { 94 return new JavaFileManager[0]; 95 } 96 } 97 98 private JavaFileManager[] getAllFileManagers () { 99 List <JavaFileManager> result = new ArrayList <JavaFileManager> (4); 100 result.add(this.bootPath); 101 result.add (this.classPath); 102 if (this.sourcePath!=null) { 103 result.add (this.sourcePath); 104 } 105 if (this.outputhPath!=null) { 106 result.add (this.outputhPath); 107 } 108 return result.toArray(new JavaFileManager[result.size()]); 109 } 110 111 public Iterable <JavaFileObject> list(Location l, String packageName, Set <JavaFileObject.Kind> kinds, boolean recurse) throws IOException { 112 List <Iterator <JavaFileObject>> iterators = new LinkedList <Iterator <JavaFileObject>>(); 113 JavaFileManager[] fms = getFileManager (l); 114 for (JavaFileManager fm : fms) { 115 iterators.add( fm.list(l, packageName, kinds, recurse).iterator() ); 116 } 117 return Iterators.toIterable(Iterators.chained( iterators )); 118 } 119 120 public FileObject getFileForInput(Location l, String packageName, String relativeName) throws IOException { 121 JavaFileManager[] fms = getFileManager(l); 122 for (JavaFileManager fm : fms) { 123 FileObject result = fm.getFileForInput(l, packageName, relativeName); 124 if (result != null) { 125 return result; 126 } 127 } 128 return null; 129 } 130 131 public FileObject getFileForOutput(Location l, String packageName, String relativeName, FileObject sibling) 132 throws IOException , UnsupportedOperationException , IllegalArgumentException { 133 JavaFileManager[] fms = getFileManager (l); 134 assert fms.length <=1; 135 if (fms.length == 0) { 136 return null; 137 } 138 else { 139 return fms[0].getFileForOutput(l, packageName, relativeName, sibling); 140 } 141 } 142 143 public ClassLoader getClassLoader (Location l) { 144 return null; 145 } 146 147 public void flush() throws IOException { 148 JavaFileManager[] fms = getAllFileManagers (); 149 for (JavaFileManager fm : fms) { 150 fm.flush(); 151 } 152 } 153 154 public void close() throws IOException { 155 JavaFileManager[] fms = getAllFileManagers (); 156 for (JavaFileManager fm : fms) { 157 fm.close(); 158 } 159 } 160 161 public int isSupportedOption(String string) { 162 return -1; 163 } 164 165 public boolean handleOption (String current, Iterator <String > remains) { 166 return false; 167 } 168 169 public boolean hasLocation(JavaFileManager.Location location) { 170 return location == StandardLocation.CLASS_PATH || 171 location == StandardLocation.PLATFORM_CLASS_PATH || 172 location == StandardLocation.SOURCE_PATH || 173 location == StandardLocation.CLASS_OUTPUT; 174 } 175 176 public JavaFileObject getJavaFileForInput (Location l, String className, JavaFileObject.Kind kind) throws IOException { 177 JavaFileManager[] fms = getFileManager (l); 178 for (JavaFileManager fm : fms) { 179 JavaFileObject result = fm.getJavaFileForInput(l,className,kind); 180 if (result != null) { 181 return result; 182 } 183 } 184 return null; 185 } 186 187 public JavaFileObject getJavaFileForOutput(Location l, String className, JavaFileObject.Kind kind, FileObject sibling) 188 throws IOException , UnsupportedOperationException , IllegalArgumentException { 189 JavaFileManager[] fms = getFileManager (l); 190 assert fms.length <=1; 191 if (fms.length == 0) { 192 return null; 193 } 194 else { 195 return fms[0].getJavaFileForOutput (l, className, kind, sibling); 196 } 197 } 198 199 200 public String inferBinaryName(JavaFileManager.Location location, JavaFileObject javaFileObject) { 201 assert javaFileObject != null; 202 if (javaFileObject == lastInfered) { 204 return lastInferedResult; 205 } 206 String result; 207 if (javaFileObject instanceof FileObjects.Base) { 209 final FileObjects.Base base = (FileObjects.Base) javaFileObject; 210 final StringBuilder sb = new StringBuilder (); 211 sb.append (base.getPackage()); 212 if (sb.length()>0) { 213 sb.append('.'); } 215 sb.append(base.getNameWithoutExtension()); 216 result = sb.toString(); 217 this.lastInfered = javaFileObject; 218 this.lastInferedResult = result; 219 return result; 220 } 221 JavaFileManager[] fms = getFileManager (location); 223 for (JavaFileManager fm : fms) { 224 result = fm.inferBinaryName (location, javaFileObject); 225 if (result != null && result.length() > 0) { 226 this.lastInfered = javaFileObject; 227 this.lastInferedResult = result; 228 return result; 229 } 230 } 231 return null; 232 } 233 234 public boolean isSameFile(FileObject fileObject, FileObject fileObject0) { 235 final JavaFileManager[] fms = getFileManager(ALL); 236 for (JavaFileManager fm : fms) { 237 if (fm.isSameFile(fileObject, fileObject0)) { 238 return true; 239 } 240 } 241 return fileObject.toUri().equals (fileObject0.toUri()); 242 } 243 244 } 245 | Popular Tags |