13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.tools.javac.file;
26
27 import java.io.IOException;
28 import java.io.UncheckedIOException;
29 import java.lang.ref.SoftReference;
30 import java.net.URI;
31 import java.nio.file.DirectoryStream;
32 import java.nio.file.FileSystem;
33 import java.nio.file.FileSystems;
34 import java.nio.file.FileSystemNotFoundException;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.ProviderNotFoundException;
38 import java.nio.file.spi.FileSystemProvider;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.LinkedHashMap;
42 import java.util.LinkedHashSet;
43 import java.util.Map;
44 import java.util.MissingResourceException;
45 import java.util.ResourceBundle;
46 import java.util.Set;
47
48 import javax.tools.FileObject;
49
50 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
51 import com.sun.tools.javac.nio.PathFileObject;
52 import com.sun.tools.javac.util.Context;
53
54 /**
55 * A package-oriented index into the jrt: filesystem.
56 */
57 public class JRTIndex {
58 /** Get a shared instance of the cache. */
59 private static JRTIndex sharedInstance;
60 public synchronized static JRTIndex getSharedInstance() {
61 if (sharedInstance == null) {
62 try {
63 sharedInstance = new JRTIndex();
64 } catch (IOException e) {
65 throw new UncheckedIOException(e);
66 }
67 }
68 return sharedInstance;
69 }
70
71 /** Get a context-specific instance of a cache. */
72 public static JRTIndex instance(Context context) {
213 for (Path entry: stream) {
214 String name = entry.getFileName().toString();
215 if (Files.isRegularFile(entry)) {
216 // TODO: consider issue of files with same name in different modules
217 files.put(name, entry);
218 } else if (Files.isDirectory(entry)) {
219 subdirs.add(new RelativeDirectory(rd, name));
220 }
221 }
222 }
223 }
224 e = new Entry(Collections.unmodifiableMap(files),
225 Collections.unmodifiableSet(subdirs),
226 getCtInfo(rd));
227 entries.put(rd, new SoftReference<>(e));
228 }
229 return e;
230 }
231
232 public boolean isInJRT(FileObject fo) {
233 if (fo instanceof PathFileObject) {
234 Path path = ((PathFileObject) fo).getPath();
235 return (path.getFileSystem() == jrtfs);
236 } else {
237 return false;
238 }
239 }
240
241 private CtSym getCtInfo(RelativeDirectory dir) {
242 if (dir.path.isEmpty())
243 return CtSym.EMPTY;
244 // It's a side-effect of the default build rules that ct.properties
245 // ends up as a resource bundle.
246 if (ctBundle == null) {
247 final String bundleName = "com.sun.tools.javac.resources.ct";
248 ctBundle = ResourceBundle.getBundle(bundleName);
249 }
250 try {
251 String attrs = ctBundle.getString(dir.path.replace('/', '.') + '*');
252 boolean hidden = false;
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.tools.javac.file;
26
27 import java.io.IOException;
28 import java.io.UncheckedIOException;
29 import java.lang.ref.SoftReference;
30 import java.net.URI;
31 import java.nio.file.DirectoryStream;
32 import java.nio.file.FileSystem;
33 import java.nio.file.FileSystemNotFoundException;
34 import java.nio.file.FileSystems;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.ProviderNotFoundException;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.LinkedHashMap;
41 import java.util.LinkedHashSet;
42 import java.util.Map;
43 import java.util.MissingResourceException;
44 import java.util.ResourceBundle;
45 import java.util.Set;
46
47 import javax.tools.FileObject;
48
49 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
50 import com.sun.tools.javac.nio.PathFileObject;
51 import com.sun.tools.javac.util.Context;
52 import com.sun.tools.sjavac.comp.JavaFileObjectWithLocation;
53
54 /**
55 * A package-oriented index into the jrt: filesystem.
56 */
57 public class JRTIndex {
58 /** Get a shared instance of the cache. */
59 private static JRTIndex sharedInstance;
60 public synchronized static JRTIndex getSharedInstance() {
61 if (sharedInstance == null) {
62 try {
63 sharedInstance = new JRTIndex();
64 } catch (IOException e) {
65 throw new UncheckedIOException(e);
66 }
67 }
68 return sharedInstance;
69 }
70
71 /** Get a context-specific instance of a cache. */
72 public static JRTIndex instance(Context context) {
213 for (Path entry: stream) {
214 String name = entry.getFileName().toString();
215 if (Files.isRegularFile(entry)) {
216 // TODO: consider issue of files with same name in different modules
217 files.put(name, entry);
218 } else if (Files.isDirectory(entry)) {
219 subdirs.add(new RelativeDirectory(rd, name));
220 }
221 }
222 }
223 }
224 e = new Entry(Collections.unmodifiableMap(files),
225 Collections.unmodifiableSet(subdirs),
226 getCtInfo(rd));
227 entries.put(rd, new SoftReference<>(e));
228 }
229 return e;
230 }
231
232 public boolean isInJRT(FileObject fo) {
233 if (fo instanceof JavaFileObjectWithLocation) {
234 fo = ((JavaFileObjectWithLocation<?>) fo).getDelegate();
235 }
236 if (fo instanceof PathFileObject) {
237 Path path = ((PathFileObject) fo).getPath();
238 return (path.getFileSystem() == jrtfs);
239 } else {
240 return false;
241 }
242 }
243
244 private CtSym getCtInfo(RelativeDirectory dir) {
245 if (dir.path.isEmpty())
246 return CtSym.EMPTY;
247 // It's a side-effect of the default build rules that ct.properties
248 // ends up as a resource bundle.
249 if (ctBundle == null) {
250 final String bundleName = "com.sun.tools.javac.resources.ct";
251 ctBundle = ResourceBundle.getBundle(bundleName);
252 }
253 try {
254 String attrs = ctBundle.getString(dir.path.replace('/', '.') + '*');
255 boolean hidden = false;
|