src/share/classes/com/sun/tools/sjavac/CompileProperties.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1504
22e417cdddee
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

ohrstrom@1504 1 /*
ksrini@2227 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504 4 *
ohrstrom@1504 5 * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504 6 * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504 7 * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504 8 * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504 9 * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504 10 *
ohrstrom@1504 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504 14 * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504 15 * accompanied this code).
ohrstrom@1504 16 *
ohrstrom@1504 17 * You should have received a copy of the GNU General Public License version
ohrstrom@1504 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504 20 *
ohrstrom@1504 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504 22 * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504 23 * questions.
ohrstrom@1504 24 */
ohrstrom@1504 25
ohrstrom@1504 26 package com.sun.tools.sjavac;
ohrstrom@1504 27
ohrstrom@1504 28 import java.io.*;
ohrstrom@1504 29 import java.net.URI;
ohrstrom@1504 30 import java.text.MessageFormat;
ohrstrom@1504 31 import java.util.ArrayList;
ohrstrom@1504 32 import java.util.Collections;
ohrstrom@1504 33 import java.util.Iterator;
ohrstrom@1504 34 import java.util.List;
ohrstrom@1504 35 import java.util.Properties;
ohrstrom@1504 36 import java.util.Set;
ohrstrom@1504 37 import java.util.HashSet;
ohrstrom@1504 38 import java.util.Map;
ohrstrom@1504 39
ohrstrom@1504 40 /**
ohrstrom@1504 41 * Compile properties transform a properties file into a Java source file.
ohrstrom@1504 42 * Java has built in support for reading properties from either a text file
ohrstrom@1504 43 * in the source or a compiled java source file.
ohrstrom@1504 44 *
ohrstrom@1504 45 * <p><b>This is NOT part of any supported API.
ohrstrom@1504 46 * If you write code that depends on this, you do so at your own
ohrstrom@1504 47 * risk. This code and its internal interfaces are subject to change
ohrstrom@1504 48 * or deletion without notice.</b></p>
ohrstrom@1504 49 */
ohrstrom@1504 50 public class CompileProperties implements Transformer
ohrstrom@1504 51 {
ohrstrom@1504 52 // Any extra information passed from the command line, for example if:
ohrstrom@1504 53 // -tr .proppp=com.sun.tools.javac.smart.CompileProperties,sun.util.resources.LocaleNamesBundle
ohrstrom@1504 54 // then extra will be "sun.util.resources.LocaleNamesBundle"
ohrstrom@1504 55 String extra;
ohrstrom@1504 56
ohrstrom@1504 57 public void setExtra(String e) {
ohrstrom@1504 58 extra = e;
ohrstrom@1504 59 }
ohrstrom@1504 60
ohrstrom@1504 61 public void setExtra(String[] a) {
ohrstrom@1504 62 }
ohrstrom@1504 63
ohrstrom@1504 64 public boolean transform(Map<String,Set<URI>> pkgSrcs,
ohrstrom@1504 65 Set<URI> visibleSrcs,
ohrstrom@1504 66 Map<URI,Set<String>> visibleClasses,
ohrstrom@1504 67 Map<String,Set<String>> oldPackageDependents,
ohrstrom@1504 68 URI destRoot,
ohrstrom@1504 69 Map<String,Set<URI>> packageArtifacts,
ohrstrom@1504 70 Map<String,Set<String>> packageDependencies,
ohrstrom@1504 71 Map<String,String> packagePublicApis,
ohrstrom@1504 72 int debugLevel,
ohrstrom@1504 73 boolean incremental,
ohrstrom@1504 74 int numCores,
ohrstrom@1504 75 PrintStream out,
ohrstrom@1504 76 PrintStream err) {
ohrstrom@1504 77 boolean rc = true;
ohrstrom@1504 78 for (String pkgName : pkgSrcs.keySet()) {
ohrstrom@1504 79 String pkgNameF = Util.toFileSystemPath(pkgName);
ohrstrom@1504 80 for (URI u : pkgSrcs.get(pkgName)) {
ohrstrom@1504 81 File src = new File(u);
ohrstrom@1504 82 boolean r = compile(pkgName, pkgNameF, src, new File(destRoot), debugLevel,
ohrstrom@1504 83 packageArtifacts);
ohrstrom@1504 84 if (r == false) {
ohrstrom@1504 85 rc = false;
ohrstrom@1504 86 }
ohrstrom@1504 87 }
ohrstrom@1504 88 }
ohrstrom@1504 89 return rc;
ohrstrom@1504 90 }
ohrstrom@1504 91
ohrstrom@1504 92 boolean compile(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel,
ohrstrom@1504 93 Map<String,Set<URI>> packageArtifacts)
ohrstrom@1504 94 {
ohrstrom@1504 95 String superClass = "java.util.ListResourceBundle";
ohrstrom@1504 96
ohrstrom@1504 97 if (extra != null) {
ohrstrom@1504 98 superClass = extra;
ohrstrom@1504 99 }
ohrstrom@1504 100 // Load the properties file.
ohrstrom@1504 101 Properties p = new Properties();
ohrstrom@1504 102 try {
ohrstrom@1504 103 p.load(new FileInputStream(src));
ohrstrom@1504 104 } catch (IOException e) {
ohrstrom@1504 105 Log.error("Error reading file "+src.getPath());
ohrstrom@1504 106 return false;
ohrstrom@1504 107 }
ohrstrom@1504 108
ohrstrom@1504 109 // Calculate the name of the Java source file to be generated.
ohrstrom@1504 110 int dp = src.getName().lastIndexOf(".");
ohrstrom@1504 111 String classname = src.getName().substring(0,dp);
ohrstrom@1504 112
ohrstrom@1504 113 // Sort the properties in increasing key order.
ohrstrom@1504 114 List<String> sortedKeys = new ArrayList<String>();
ohrstrom@1504 115 for (Object key : p.keySet()) {
ohrstrom@1504 116 sortedKeys.add((String)key);
ohrstrom@1504 117 }
ohrstrom@1504 118 Collections.sort(sortedKeys);
ohrstrom@1504 119 Iterator<String> keys = sortedKeys.iterator();
ohrstrom@1504 120
ohrstrom@1504 121 // Collect the properties into a string buffer.
ohrstrom@1504 122 StringBuilder data = new StringBuilder();
ohrstrom@1504 123 while (keys.hasNext()) {
ohrstrom@1504 124 String key = keys.next();
ohrstrom@1504 125 data.append(" { \"" + escape(key) + "\", \"" +
ohrstrom@1504 126 escape((String)p.get(key)) + "\" },\n");
ohrstrom@1504 127 }
ohrstrom@1504 128
ohrstrom@1504 129 // Create dest file name. It is derived from the properties file name.
ohrstrom@1504 130 String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+classname+".java";
ohrstrom@1504 131 File dest = new File(destFilename);
ohrstrom@1504 132
ohrstrom@1504 133 // Make sure the dest directories exist.
ohrstrom@1504 134 if (!dest.getParentFile().isDirectory()) {
ohrstrom@1504 135 if (!dest.getParentFile().mkdirs()) {
ohrstrom@1504 136 Log.error("Could not create the directory "+dest.getParentFile().getPath());
ohrstrom@1504 137 return false;
ohrstrom@1504 138 }
ohrstrom@1504 139 }
ohrstrom@1504 140
ohrstrom@1504 141 Set<URI> as = packageArtifacts.get(pkgName);
ohrstrom@1504 142 if (as == null) {
ohrstrom@1504 143 as = new HashSet<URI>();
ohrstrom@1504 144 packageArtifacts.put(pkgName, as);
ohrstrom@1504 145 }
ohrstrom@1504 146 as.add(dest.toURI());
ohrstrom@1504 147
ohrstrom@1504 148 if (dest.exists() && dest.lastModified() > src.lastModified()) {
ohrstrom@1504 149 // A generated file exists, and its timestamp is newer than the source.
ohrstrom@1504 150 // Assume that we do not need to regenerate the dest file!
ohrstrom@1504 151 // Thus we are done.
ohrstrom@1504 152 return true;
ohrstrom@1504 153 }
ohrstrom@1504 154
ohrstrom@1504 155 String packageString = "package " + pkgNameF.replace(File.separatorChar,'.') + ";\n\n";
ohrstrom@1504 156
ohrstrom@1504 157 Log.info("Compiling property file "+pkgNameF+File.separator+src.getName());
ohrstrom@1504 158 try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {
ohrstrom@1504 159 MessageFormat format = new MessageFormat(FORMAT);
ohrstrom@1504 160 writer.write(format.format(new Object[] { packageString, classname, superClass, data }));
ohrstrom@1504 161 } catch ( IOException e ) {
ohrstrom@1504 162 Log.error("Could not write file "+dest.getPath());
ohrstrom@1504 163 return false;
ohrstrom@1504 164 }
ohrstrom@1504 165 return true;
ohrstrom@1504 166 }
ohrstrom@1504 167
ohrstrom@1504 168 private static final String FORMAT =
ohrstrom@1504 169 "{0}" +
ohrstrom@1504 170 "public final class {1} extends {2} '{'\n" +
ohrstrom@1504 171 " protected final Object[][] getContents() '{'\n" +
ohrstrom@1504 172 " return new Object[][] '{'\n" +
ohrstrom@1504 173 "{3}" +
ohrstrom@1504 174 " };\n" +
ohrstrom@1504 175 " }\n" +
ohrstrom@1504 176 "}\n";
ohrstrom@1504 177
ohrstrom@1504 178 public static String escape(String theString) {
ohrstrom@1504 179 int len = theString.length();
ohrstrom@1504 180 StringBuilder outBuffer = new StringBuilder(len*2);
ohrstrom@1504 181
ohrstrom@1504 182 for(int x=0; x<len; x++) {
ohrstrom@1504 183 char aChar = theString.charAt(x);
ohrstrom@1504 184 switch(aChar) {
ohrstrom@1504 185 case '\\':outBuffer.append('\\'); outBuffer.append('\\');
ohrstrom@1504 186 break;
ohrstrom@1504 187 case '\t':outBuffer.append('\\'); outBuffer.append('t');
ohrstrom@1504 188 break;
ohrstrom@1504 189 case '\n':outBuffer.append('\\'); outBuffer.append('n');
ohrstrom@1504 190 break;
ohrstrom@1504 191 case '\r':outBuffer.append('\\'); outBuffer.append('r');
ohrstrom@1504 192 break;
ohrstrom@1504 193 case '\f':outBuffer.append('\\'); outBuffer.append('f');
ohrstrom@1504 194 break;
ohrstrom@1504 195 default:
ohrstrom@1504 196 if ((aChar < 0x0020) || (aChar > 0x007e)) {
ohrstrom@1504 197 outBuffer.append('\\');
ohrstrom@1504 198 outBuffer.append('u');
ohrstrom@1504 199 outBuffer.append(toHex((aChar >> 12) & 0xF));
ohrstrom@1504 200 outBuffer.append(toHex((aChar >> 8) & 0xF));
ohrstrom@1504 201 outBuffer.append(toHex((aChar >> 4) & 0xF));
ohrstrom@1504 202 outBuffer.append(toHex( aChar & 0xF));
ohrstrom@1504 203 } else {
ohrstrom@1504 204 if (aChar == '"') {
ohrstrom@1504 205 outBuffer.append('\\');
ohrstrom@1504 206 }
ohrstrom@1504 207 outBuffer.append(aChar);
ohrstrom@1504 208 }
ohrstrom@1504 209 }
ohrstrom@1504 210 }
ohrstrom@1504 211 return outBuffer.toString();
ohrstrom@1504 212 }
ohrstrom@1504 213
ohrstrom@1504 214 private static char toHex(int nibble) {
ohrstrom@1504 215 return hexDigit[(nibble & 0xF)];
ohrstrom@1504 216 }
ohrstrom@1504 217
ohrstrom@1504 218 private static final char[] hexDigit = {
ohrstrom@1504 219 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
ohrstrom@1504 220 };
ohrstrom@1504 221 }

mercurial