Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/sjavac/CompileProperties.java
41175 views
1
/*
2
* Copyright (c) 2012, 2016, 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
26
package com.sun.tools.sjavac;
27
28
import java.io.BufferedWriter;
29
import java.io.File;
30
import java.io.FileInputStream;
31
import java.io.FileOutputStream;
32
import java.io.IOException;
33
import java.io.OutputStreamWriter;
34
import java.io.PrintStream;
35
import java.io.Writer;
36
import java.net.URI;
37
import java.text.MessageFormat;
38
import java.util.ArrayList;
39
import java.util.Collections;
40
import java.util.HashSet;
41
import java.util.Iterator;
42
import java.util.List;
43
import java.util.Map;
44
import java.util.Properties;
45
import java.util.Set;
46
47
import com.sun.tools.sjavac.comp.CompilationService;
48
import com.sun.tools.sjavac.options.Options;
49
import com.sun.tools.sjavac.pubapi.PubApi;
50
51
/**
52
* Compile properties transform a properties file into a Java source file.
53
* Java has built in support for reading properties from either a text file
54
* in the source or a compiled java source file.
55
*
56
* <p><b>This is NOT part of any supported API.
57
* If you write code that depends on this, you do so at your own risk.
58
* This code and its internal interfaces are subject to change or
59
* deletion without notice.</b>
60
*/
61
public class CompileProperties implements Transformer {
62
// Any extra information passed from the command line, for example if:
63
// -tr .proppp=com.sun.tools.javac.smart.CompileProperties,sun.util.resources.LocaleNamesBundle
64
// then extra will be "sun.util.resources.LocaleNamesBundle"
65
String extra;
66
67
public void setExtra(String e) {
68
extra = e;
69
}
70
71
public void setExtra(Options a) {
72
}
73
74
public boolean transform(CompilationService compilationService,
75
Map<String,Set<URI>> pkgSrcs,
76
Set<URI> visibleSrcs,
77
Map<String,Set<String>> oldPackageDependents,
78
URI destRoot,
79
Map<String,Set<URI>> packageArtifacts,
80
Map<String,Map<String, Set<String>>> packageDependencies,
81
Map<String,Map<String, Set<String>>> packageCpDependencies,
82
Map<String, PubApi> packagePublicApis,
83
Map<String, PubApi> dependencyPublicApis,
84
int debugLevel,
85
boolean incremental,
86
int numCores) {
87
boolean rc = true;
88
for (String pkgName : pkgSrcs.keySet()) {
89
String pkgNameF = Util.toFileSystemPath(pkgName);
90
for (URI u : pkgSrcs.get(pkgName)) {
91
File src = new File(u);
92
boolean r = compile(pkgName, pkgNameF, src, new File(destRoot), debugLevel,
93
packageArtifacts);
94
if (r == false) {
95
rc = false;
96
}
97
}
98
}
99
return rc;
100
}
101
102
boolean compile(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel,
103
Map<String,Set<URI>> packageArtifacts)
104
{
105
String superClass = "java.util.ListResourceBundle";
106
107
if (extra != null) {
108
superClass = extra;
109
}
110
// Load the properties file.
111
Properties p = new Properties();
112
try {
113
p.load(new FileInputStream(src));
114
} catch (IOException e) {
115
Log.error("Error reading file "+src.getPath());
116
return false;
117
}
118
119
// Calculate the name of the Java source file to be generated.
120
int dp = src.getName().lastIndexOf(".");
121
String classname = src.getName().substring(0,dp);
122
123
// Sort the properties in increasing key order.
124
List<String> sortedKeys = new ArrayList<>();
125
for (Object key : p.keySet()) {
126
sortedKeys.add((String)key);
127
}
128
Collections.sort(sortedKeys);
129
Iterator<String> keys = sortedKeys.iterator();
130
131
// Collect the properties into a string buffer.
132
StringBuilder data = new StringBuilder();
133
while (keys.hasNext()) {
134
String key = keys.next();
135
data.append(" { \"" + escape(key) + "\", \"" +
136
escape((String)p.get(key)) + "\" },\n");
137
}
138
139
// Create dest file name. It is derived from the properties file name.
140
String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+classname+".java";
141
File dest = new File(destFilename);
142
143
// Make sure the dest directories exist.
144
if (!dest.getParentFile().isDirectory()) {
145
if (!dest.getParentFile().mkdirs()) {
146
Log.error("Could not create the directory "+dest.getParentFile().getPath());
147
return false;
148
}
149
}
150
151
Set<URI> as = packageArtifacts.get(pkgName);
152
if (as == null) {
153
as = new HashSet<>();
154
packageArtifacts.put(pkgName, as);
155
}
156
as.add(dest.toURI());
157
158
if (dest.exists() && dest.lastModified() > src.lastModified()) {
159
// A generated file exists, and its timestamp is newer than the source.
160
// Assume that we do not need to regenerate the dest file!
161
// Thus we are done.
162
return true;
163
}
164
165
String packageString = "package " + pkgNameF.replace(File.separatorChar,'.') + ";\n\n";
166
167
Log.info("Compiling property file "+pkgNameF+File.separator+src.getName());
168
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {
169
MessageFormat format = new MessageFormat(FORMAT);
170
writer.write(format.format(new Object[] { packageString, classname, superClass, data }));
171
} catch ( IOException e ) {
172
Log.error("Could not write file "+dest.getPath());
173
return false;
174
}
175
return true;
176
}
177
178
private static final String FORMAT =
179
"{0}" +
180
"public final class {1} extends {2} '{'\n" +
181
" protected final Object[][] getContents() '{'\n" +
182
" return new Object[][] '{'\n" +
183
"{3}" +
184
" };\n" +
185
" }\n" +
186
"}\n";
187
188
public static String escape(String theString) {
189
int len = theString.length();
190
StringBuilder outBuffer = new StringBuilder(len*2);
191
192
for(int x=0; x<len; x++) {
193
char aChar = theString.charAt(x);
194
switch(aChar) {
195
case '\\':outBuffer.append('\\'); outBuffer.append('\\');
196
break;
197
case '\t':outBuffer.append('\\'); outBuffer.append('t');
198
break;
199
case '\n':outBuffer.append('\\'); outBuffer.append('n');
200
break;
201
case '\r':outBuffer.append('\\'); outBuffer.append('r');
202
break;
203
case '\f':outBuffer.append('\\'); outBuffer.append('f');
204
break;
205
default:
206
if ((aChar < 0x0020) || (aChar > 0x007e)) {
207
outBuffer.append('\\');
208
outBuffer.append('u');
209
outBuffer.append(toHex((aChar >> 12) & 0xF));
210
outBuffer.append(toHex((aChar >> 8) & 0xF));
211
outBuffer.append(toHex((aChar >> 4) & 0xF));
212
outBuffer.append(toHex( aChar & 0xF));
213
} else {
214
if (aChar == '"') {
215
outBuffer.append('\\');
216
}
217
outBuffer.append(aChar);
218
}
219
}
220
}
221
return outBuffer.toString();
222
}
223
224
private static char toHex(int nibble) {
225
return hexDigit[(nibble & 0xF)];
226
}
227
228
private static final char[] hexDigit = {
229
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
230
};
231
}
232
233