author | briangoetz |
Wed, 18 Dec 2013 16:05:18 -0500 | |
changeset 22163 | 3651128c74eb |
parent 22159 | 682da512ec17 |
child 22165 | ec53c8946fc2 |
permissions | -rw-r--r-- |
15724 | 1 |
/* |
2 |
* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
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.sym; |
|
26 |
||
27 |
import java.io.BufferedInputStream; |
|
28 |
import java.io.BufferedWriter; |
|
29 |
import java.io.File; |
|
30 |
import java.io.FileInputStream; |
|
31 |
import java.io.FileWriter; |
|
32 |
import java.io.IOException; |
|
33 |
import java.nio.charset.Charset; |
|
34 |
import java.nio.file.Files; |
|
35 |
import java.util.HashMap; |
|
36 |
import java.util.Map; |
|
37 |
import java.util.Properties; |
|
38 |
import java.util.Set; |
|
39 |
import java.util.TreeMap; |
|
40 |
import java.util.TreeSet; |
|
41 |
||
42 |
import com.sun.tools.javac.util.Assert; |
|
43 |
||
44 |
/** |
|
45 |
* Provide details about profile contents. |
|
46 |
* |
|
47 |
* <p><b>This is NOT part of any supported API. |
|
48 |
* If you write code that depends on this, you do so at your own |
|
49 |
* risk. This code and its internal interfaces are subject to change |
|
50 |
* or deletion without notice.</b></p> |
|
51 |
*/ |
|
52 |
public abstract class Profiles { |
|
53 |
// for debugging |
|
54 |
public static void main(String[] args) throws IOException { |
|
55 |
Profiles p = Profiles.read(new File(args[0])); |
|
56 |
if (args.length >= 2) { |
|
22163 | 57 |
Map<Integer,Set<String>> lists = new TreeMap<>(); |
15724 | 58 |
for (int i = 1; i <= 4; i++) |
22163 | 59 |
lists.put(i, new TreeSet<>()); |
15724 | 60 |
|
61 |
File rt_jar_lst = new File(args[1]); |
|
62 |
for (String line: Files.readAllLines(rt_jar_lst.toPath(), Charset.defaultCharset())) { |
|
63 |
if (line.endsWith(".class")) { |
|
64 |
String type = line.substring(0, line.length() - 6); |
|
65 |
int profile = p.getProfile(type); |
|
66 |
for (int i = profile; i <= 4; i++) |
|
67 |
lists.get(i).add(type); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
for (int i = 1; i <= 4; i++) { |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
19653
diff
changeset
|
72 |
try (BufferedWriter out = new BufferedWriter(new FileWriter(i + ".txt"))) { |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
19653
diff
changeset
|
73 |
for (String type : lists.get(i)) { |
15724 | 74 |
out.write(type); |
75 |
out.newLine(); |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
public static Profiles read(File file) throws IOException { |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
19653
diff
changeset
|
83 |
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) { |
15724 | 84 |
Properties p = new Properties(); |
85 |
p.load(in); |
|
86 |
if (p.containsKey("java/lang/Object")) |
|
87 |
return new SimpleProfiles(p); |
|
88 |
else |
|
89 |
return new MakefileProfiles(p); |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
public abstract int getProfileCount(); |
|
94 |
||
95 |
public abstract int getProfile(String typeName); |
|
96 |
||
97 |
public abstract Set<String> getPackages(int profile); |
|
98 |
||
99 |
private static class MakefileProfiles extends Profiles { |
|
100 |
static class Package { |
|
101 |
final Package parent; |
|
102 |
final String name; |
|
103 |
||
22163 | 104 |
Map<String, Package> subpackages = new TreeMap<>(); |
15724 | 105 |
|
106 |
int profile; |
|
22163 | 107 |
Map<String, Integer> includedTypes = new TreeMap<>(); |
108 |
Map<String, Integer> excludedTypes = new TreeMap<>(); |
|
15724 | 109 |
|
110 |
Package(Package parent, String name) { |
|
111 |
this.parent = parent; |
|
112 |
this.name = name; |
|
113 |
} |
|
114 |
||
115 |
int getProfile() { |
|
116 |
return (parent == null) ? profile : Math.max(parent.getProfile(), profile); |
|
117 |
} |
|
118 |
||
119 |
int getProfile(String simpleTypeName) { |
|
120 |
Integer i; |
|
121 |
if ((i = includedTypes.get(simpleTypeName)) != null) |
|
122 |
return i; |
|
123 |
if ((i = includedTypes.get("*")) != null) |
|
124 |
return i; |
|
125 |
if ((i = excludedTypes.get(simpleTypeName)) != null) |
|
126 |
return i + 1; |
|
127 |
if ((i = excludedTypes.get("*")) != null) |
|
128 |
return i + 1; |
|
129 |
return getProfile(); |
|
130 |
} |
|
131 |
||
132 |
String getName() { |
|
133 |
return (parent == null) ? name : (parent.getName() + "/" + name); |
|
134 |
} |
|
135 |
||
136 |
void getPackages(int profile, Set<String> results) { |
|
137 |
int prf = getProfile(); |
|
138 |
if (prf != 0 && profile >= prf) |
|
139 |
results.add(getName()); |
|
140 |
for (Package pkg: subpackages.values()) |
|
141 |
pkg.getPackages(profile, results); |
|
142 |
} |
|
143 |
} |
|
144 |
||
22163 | 145 |
final Map<String, Package> packages = new TreeMap<>(); |
16548 | 146 |
|
147 |
final int maxProfile = 4; // Three compact profiles plus full JRE |
|
15724 | 148 |
|
149 |
MakefileProfiles(Properties p) { |
|
16548 | 150 |
for (int profile = 1; profile <= maxProfile; profile++) { |
151 |
String prefix = (profile < maxProfile ? "PROFILE_" + profile : "FULL_JRE"); |
|
152 |
String inclPackages = p.getProperty(prefix + "_RTJAR_INCLUDE_PACKAGES"); |
|
15724 | 153 |
if (inclPackages == null) |
154 |
break; |
|
155 |
for (String pkg: inclPackages.substring(1).trim().split("\\s+")) { |
|
156 |
if (pkg.endsWith("/")) |
|
157 |
pkg = pkg.substring(0, pkg.length() - 1); |
|
158 |
includePackage(profile, pkg); |
|
159 |
} |
|
16548 | 160 |
String inclTypes = p.getProperty(prefix + "_RTJAR_INCLUDE_TYPES"); |
15724 | 161 |
if (inclTypes != null) { |
162 |
for (String type: inclTypes.replace("$$", "$").split("\\s+")) { |
|
163 |
if (type.endsWith(".class")) |
|
164 |
includeType(profile, type.substring(0, type.length() - 6)); |
|
165 |
} |
|
166 |
} |
|
16548 | 167 |
String exclTypes = p.getProperty(prefix + "_RTJAR_EXCLUDE_TYPES"); |
15724 | 168 |
if (exclTypes != null) { |
169 |
for (String type: exclTypes.replace("$$", "$").split("\\s+")) { |
|
170 |
if (type.endsWith(".class")) |
|
171 |
excludeType(profile, type.substring(0, type.length() - 6)); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
@Override |
|
178 |
public int getProfileCount() { |
|
179 |
return maxProfile; |
|
180 |
} |
|
181 |
||
182 |
@Override |
|
183 |
public int getProfile(String typeName) { |
|
184 |
int sep = typeName.lastIndexOf("/"); |
|
185 |
String packageName = typeName.substring(0, sep); |
|
186 |
String simpleName = typeName.substring(sep + 1); |
|
187 |
||
188 |
Package p = getPackage(packageName); |
|
189 |
return p.getProfile(simpleName); |
|
190 |
} |
|
191 |
||
192 |
@Override |
|
193 |
public Set<String> getPackages(int profile) { |
|
22163 | 194 |
Set<String> results = new TreeSet<>(); |
15724 | 195 |
for (Package p: packages.values()) |
196 |
p.getPackages(profile, results); |
|
197 |
return results; |
|
198 |
} |
|
199 |
||
200 |
private void includePackage(int profile, String packageName) { |
|
201 |
// System.err.println("include package " + packageName); |
|
202 |
Package p = getPackage(packageName); |
|
203 |
Assert.check(p.profile == 0); |
|
204 |
p.profile = profile; |
|
205 |
} |
|
206 |
||
207 |
private void includeType(int profile, String typeName) { |
|
208 |
// System.err.println("include type " + typeName); |
|
209 |
int sep = typeName.lastIndexOf("/"); |
|
210 |
String packageName = typeName.substring(0, sep); |
|
211 |
String simpleName = typeName.substring(sep + 1); |
|
212 |
||
213 |
Package p = getPackage(packageName); |
|
214 |
Assert.check(!p.includedTypes.containsKey(simpleName)); |
|
215 |
p.includedTypes.put(simpleName, profile); |
|
216 |
} |
|
217 |
||
218 |
private void excludeType(int profile, String typeName) { |
|
219 |
// System.err.println("exclude type " + typeName); |
|
220 |
int sep = typeName.lastIndexOf("/"); |
|
221 |
String packageName = typeName.substring(0, sep); |
|
222 |
String simpleName = typeName.substring(sep + 1); |
|
223 |
||
224 |
Package p = getPackage(packageName); |
|
225 |
Assert.check(!p.excludedTypes.containsKey(simpleName)); |
|
226 |
p.excludedTypes.put(simpleName, profile); |
|
227 |
} |
|
228 |
||
229 |
private Package getPackage(String packageName) { |
|
230 |
int sep = packageName.lastIndexOf("/"); |
|
231 |
Package parent; |
|
232 |
Map<String, Package> parentSubpackages; |
|
233 |
String simpleName; |
|
234 |
if (sep == -1) { |
|
235 |
parent = null; |
|
236 |
parentSubpackages = packages; |
|
237 |
simpleName = packageName; |
|
238 |
} else { |
|
239 |
parent = getPackage(packageName.substring(0, sep)); |
|
240 |
parentSubpackages = parent.subpackages; |
|
241 |
simpleName = packageName.substring(sep + 1); |
|
242 |
} |
|
243 |
||
244 |
Package p = parentSubpackages.get(simpleName); |
|
245 |
if (p == null) { |
|
246 |
parentSubpackages.put(simpleName, p = new Package(parent, simpleName)); |
|
247 |
} |
|
248 |
return p; |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
private static class SimpleProfiles extends Profiles { |
|
253 |
private final Map<String, Integer> map; |
|
254 |
private final int profileCount; |
|
255 |
||
256 |
SimpleProfiles(Properties p) { |
|
257 |
int max = 0; |
|
22163 | 258 |
map = new HashMap<>(); |
15724 | 259 |
for (Map.Entry<Object,Object> e: p.entrySet()) { |
260 |
String typeName = (String) e.getKey(); |
|
261 |
int profile = Integer.valueOf((String) e.getValue()); |
|
262 |
map.put(typeName, profile); |
|
263 |
max = Math.max(max, profile); |
|
264 |
} |
|
265 |
profileCount = max; |
|
266 |
} |
|
267 |
||
268 |
@Override |
|
269 |
public int getProfileCount() { |
|
270 |
return profileCount; |
|
271 |
} |
|
272 |
||
273 |
@Override |
|
274 |
public int getProfile(String typeName) { |
|
275 |
return map.get(typeName); |
|
276 |
} |
|
277 |
||
278 |
@Override |
|
279 |
public Set<String> getPackages(int profile) { |
|
22163 | 280 |
Set<String> results = new TreeSet<>(); |
15724 | 281 |
for (Map.Entry<String,Integer> e: map.entrySet()) { |
282 |
String tn = e.getKey(); |
|
283 |
int prf = e.getValue(); |
|
284 |
int sep = tn.lastIndexOf("/"); |
|
285 |
if (sep > 0 && profile >= prf) |
|
286 |
results.add(tn); |
|
287 |
} |
|
288 |
return results; |
|
289 |
} |
|
290 |
} |
|
291 |
} |