author | jlahoda |
Mon, 22 Aug 2016 09:59:43 +0200 | |
changeset 40513 | 39b67170b045 |
parent 37944 | 1153fab98d25 |
permissions | -rw-r--r-- |
37633
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
1 |
/* |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37633
diff
changeset
|
2 |
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. |
37633
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
4 |
* |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
10 |
* |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
15 |
* accompanied this code). |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
16 |
* |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
20 |
* |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
23 |
* questions. |
d16d6d59446d
8036952: copyright issues in jdk9/dev/langtools files
alundblad
parents:
34560
diff
changeset
|
24 |
*/ |
1208 | 25 |
|
26 |
package com.sun.tools.javac.file; |
|
27 |
||
28 |
import java.io.IOException; |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37633
diff
changeset
|
29 |
import java.nio.file.FileSystems; |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
30 |
import java.nio.file.Files; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
31 |
import java.nio.file.Path; |
40513
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
32 |
import java.nio.file.spi.FileSystemProvider; |
1208 | 33 |
import java.util.ArrayList; |
34 |
import java.util.Collections; |
|
35 |
import java.util.List; |
|
36 |
import java.util.StringTokenizer; |
|
37 |
import java.util.jar.Attributes; |
|
38 |
import java.util.jar.JarFile; |
|
39 |
import java.util.jar.Manifest; |
|
40 |
||
41 |
import com.sun.tools.javac.util.Context; |
|
42 |
||
43 |
/** |
|
44 |
* Get meta-info about files. Default direct (non-caching) implementation. |
|
45 |
* @see CacheFSInfo |
|
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1208
diff
changeset
|
46 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
3380
diff
changeset
|
47 |
* <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
|
48 |
* 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
|
49 |
* 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
|
50 |
* deletion without notice.</b> |
1208 | 51 |
*/ |
52 |
public class FSInfo { |
|
53 |
||
54 |
/** Get the FSInfo instance for this context. |
|
55 |
* @param context the context |
|
56 |
* @return the Paths instance for this context |
|
57 |
*/ |
|
58 |
public static FSInfo instance(Context context) { |
|
59 |
FSInfo instance = context.get(FSInfo.class); |
|
60 |
if (instance == null) |
|
61 |
instance = new FSInfo(); |
|
62 |
return instance; |
|
63 |
} |
|
64 |
||
65 |
protected FSInfo() { |
|
66 |
} |
|
67 |
||
68 |
protected FSInfo(Context context) { |
|
69 |
context.put(FSInfo.class, this); |
|
70 |
} |
|
71 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
72 |
public Path getCanonicalFile(Path file) { |
1208 | 73 |
try { |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
74 |
return file.toRealPath(); |
1208 | 75 |
} catch (IOException e) { |
34560
b6a567b677f7
8059976: Convert JavacFileManager to use java.nio.file internally
jjg
parents:
27852
diff
changeset
|
76 |
return file.toAbsolutePath().normalize(); |
1208 | 77 |
} |
78 |
} |
|
79 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
80 |
public boolean exists(Path file) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
81 |
return Files.exists(file); |
1208 | 82 |
} |
83 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
84 |
public boolean isDirectory(Path file) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
85 |
return Files.isDirectory(file); |
1208 | 86 |
} |
87 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
88 |
public boolean isFile(Path file) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
89 |
return Files.isRegularFile(file); |
1208 | 90 |
} |
91 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
92 |
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
|
93 |
Path parent = file.getParent(); |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
94 |
try (JarFile jarFile = new JarFile(file.toFile())) { |
1208 | 95 |
Manifest man = jarFile.getManifest(); |
96 |
if (man == null) |
|
97 |
return Collections.emptyList(); |
|
98 |
||
99 |
Attributes attr = man.getMainAttributes(); |
|
100 |
if (attr == null) |
|
101 |
return Collections.emptyList(); |
|
102 |
||
103 |
String path = attr.getValue(Attributes.Name.CLASS_PATH); |
|
104 |
if (path == null) |
|
105 |
return Collections.emptyList(); |
|
106 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
107 |
List<Path> list = new ArrayList<>(); |
1208 | 108 |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
5847
diff
changeset
|
109 |
for (StringTokenizer st = new StringTokenizer(path); |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
5847
diff
changeset
|
110 |
st.hasMoreTokens(); ) { |
1208 | 111 |
String elt = st.nextToken(); |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37633
diff
changeset
|
112 |
Path f = FileSystems.getDefault().getPath(elt); |
26535 | 113 |
if (!f.isAbsolute() && parent != null) |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
114 |
f = parent.resolve(f).toAbsolutePath(); |
26535 | 115 |
list.add(f); |
1208 | 116 |
} |
117 |
||
118 |
return list; |
|
119 |
} |
|
120 |
} |
|
40513
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
121 |
|
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
122 |
private FileSystemProvider jarFSProvider; |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
123 |
|
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
124 |
public synchronized FileSystemProvider getJarFSProvider() { |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
125 |
if (jarFSProvider != null) { |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
126 |
return jarFSProvider; |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
127 |
} |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
128 |
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
129 |
if (provider.getScheme().equals("jar")) { |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
130 |
return (jarFSProvider = provider); |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
131 |
} |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
132 |
} |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
133 |
return null; |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
134 |
} |
39b67170b045
8153391: an image created for \"jdk.compiler\" fails to run javac
jlahoda
parents:
37944
diff
changeset
|
135 |
|
1208 | 136 |
} |