author | jjg |
Mon, 07 Dec 2015 14:02:55 -0800 | |
changeset 34560 | b6a567b677f7 |
parent 27852 | 2e6ad0e4fe20 |
child 37633 | d16d6d59446d |
permissions | -rw-r--r-- |
1208 | 1 |
|
2 |
package com.sun.tools.javac.file; |
|
3 |
||
4 |
import java.io.IOException; |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
5 |
import java.nio.file.Files; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
6 |
import java.nio.file.Path; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
7 |
import java.nio.file.Paths; |
1208 | 8 |
import java.util.ArrayList; |
9 |
import java.util.Collections; |
|
10 |
import java.util.List; |
|
11 |
import java.util.StringTokenizer; |
|
12 |
import java.util.jar.Attributes; |
|
13 |
import java.util.jar.JarFile; |
|
14 |
import java.util.jar.Manifest; |
|
15 |
||
16 |
import com.sun.tools.javac.util.Context; |
|
17 |
||
18 |
/** |
|
19 |
* Get meta-info about files. Default direct (non-caching) implementation. |
|
20 |
* @see CacheFSInfo |
|
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1208
diff
changeset
|
21 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
3380
diff
changeset
|
22 |
* <p><b>This is NOT part of any supported API. |
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1208
diff
changeset
|
23 |
* If you write code that depends on this, you do so at your own risk. |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1208
diff
changeset
|
24 |
* This code and its internal interfaces are subject to change or |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1208
diff
changeset
|
25 |
* deletion without notice.</b> |
1208 | 26 |
*/ |
27 |
public class FSInfo { |
|
28 |
||
29 |
/** Get the FSInfo instance for this context. |
|
30 |
* @param context the context |
|
31 |
* @return the Paths instance for this context |
|
32 |
*/ |
|
33 |
public static FSInfo instance(Context context) { |
|
34 |
FSInfo instance = context.get(FSInfo.class); |
|
35 |
if (instance == null) |
|
36 |
instance = new FSInfo(); |
|
37 |
return instance; |
|
38 |
} |
|
39 |
||
40 |
protected FSInfo() { |
|
41 |
} |
|
42 |
||
43 |
protected FSInfo(Context context) { |
|
44 |
context.put(FSInfo.class, this); |
|
45 |
} |
|
46 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
47 |
public Path getCanonicalFile(Path file) { |
1208 | 48 |
try { |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
49 |
return file.toRealPath(); |
1208 | 50 |
} catch (IOException e) { |
34560
b6a567b677f7
8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents:
27852
diff
changeset
|
51 |
return file.toAbsolutePath().normalize(); |
1208 | 52 |
} |
53 |
} |
|
54 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
55 |
public boolean exists(Path file) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
56 |
return Files.exists(file); |
1208 | 57 |
} |
58 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
59 |
public boolean isDirectory(Path file) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
60 |
return Files.isDirectory(file); |
1208 | 61 |
} |
62 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
63 |
public boolean isFile(Path file) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
64 |
return Files.isRegularFile(file); |
1208 | 65 |
} |
66 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
67 |
public List<Path> getJarClassPath(Path file) throws IOException { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
68 |
Path parent = file.getParent(); |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
69 |
try (JarFile jarFile = new JarFile(file.toFile())) { |
1208 | 70 |
Manifest man = jarFile.getManifest(); |
71 |
if (man == null) |
|
72 |
return Collections.emptyList(); |
|
73 |
||
74 |
Attributes attr = man.getMainAttributes(); |
|
75 |
if (attr == null) |
|
76 |
return Collections.emptyList(); |
|
77 |
||
78 |
String path = attr.getValue(Attributes.Name.CLASS_PATH); |
|
79 |
if (path == null) |
|
80 |
return Collections.emptyList(); |
|
81 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
82 |
List<Path> list = new ArrayList<>(); |
1208 | 83 |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
5847
diff
changeset
|
84 |
for (StringTokenizer st = new StringTokenizer(path); |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
5847
diff
changeset
|
85 |
st.hasMoreTokens(); ) { |
1208 | 86 |
String elt = st.nextToken(); |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
87 |
Path f = Paths.get(elt); |
26535 | 88 |
if (!f.isAbsolute() && parent != null) |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
89 |
f = parent.resolve(f).toAbsolutePath(); |
26535 | 90 |
list.add(f); |
1208 | 91 |
} |
92 |
||
93 |
return list; |
|
94 |
} |
|
95 |
} |
|
96 |
} |