Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/sjavac/CleanProperties.java
38899 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.sjavac;2627import java.io.*;28import java.net.URI;29import java.util.ArrayList;30import java.util.Collections;31import java.util.Iterator;32import java.util.List;33import java.util.Set;34import java.util.HashSet;35import java.util.Map;36import java.util.Properties;3738/**39* The clean properties transform should not be necessary.40* Eventually we will cleanup the property file sources in the OpenJDK instead.41*42* <p><b>This is NOT part of any supported API.43* If you write code that depends on this, you do so at your own44* risk. This code and its internal interfaces are subject to change45* or deletion without notice.</b></p>46*/47public class CleanProperties implements Transformer48{49public void setExtra(String e) {50// Any extra information is ignored for clean properties.51}5253public void setExtra(String[] a) {54// Any extra information is ignored for clean properties.55}5657public boolean transform(Map<String,Set<URI>> pkgSrcs,58Set<URI> visibleSrcs,59Map<URI,Set<String>> visibleClasses,60Map<String,Set<String>> oldPackageDependencies,61URI destRoot,62Map<String,Set<URI>> packageArtifacts,63Map<String,Set<String>> packageDependencies,64Map<String,String> packagePublicApis,65int debugLevel,66boolean incremental,67int numCores,68PrintStream out,69PrintStream err)70{71boolean rc = true;72for (String pkgName : pkgSrcs.keySet()) {73String pkgNameF = pkgName.replace('.',File.separatorChar);74for (URI u : pkgSrcs.get(pkgName)) {75File src = new File(u);76boolean r = clean(pkgName, pkgNameF, src, new File(destRoot), debugLevel,77packageArtifacts);78if (r == false) {79rc = false;80}81}82}83return rc;84}8586boolean clean(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel,87Map<String,Set<URI>> packageArtifacts)88{89// Load the properties file.90Properties p = new Properties();91try {92p.load(new FileInputStream(src));93} catch (IOException e) {94Log.error("Error reading file "+src.getPath());95return false;96}9798// Sort the properties in increasing key order.99List<String> sortedKeys = new ArrayList<String>();100for (Object key : p.keySet()) {101sortedKeys.add((String)key);102}103Collections.sort(sortedKeys);104Iterator<String> keys = sortedKeys.iterator();105106// Collect the properties into a string buffer.107StringBuilder data = new StringBuilder();108while (keys.hasNext()) {109String key = keys.next();110data.append(CompileProperties.escape(key)+":"+CompileProperties.escape((String)p.get(key))+"\n");111}112113String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();114File dest = new File(destFilename);115116// Make sure the dest directories exist.117if (!dest.getParentFile().isDirectory()) {118if (!dest.getParentFile().mkdirs()) {119Log.error("Could not create the directory "+dest.getParentFile().getPath());120return false;121}122}123124Set<URI> as = packageArtifacts.get(pkgName);125if (as == null) {126as = new HashSet<URI>();127packageArtifacts.put(pkgName, as);128}129as.add(dest.toURI());130131if (dest.exists() && dest.lastModified() > src.lastModified()) {132// A cleaned property file exists, and its timestamp is newer than the source.133// Assume that we do not need to clean!134// Thus we are done.135return true;136}137138Log.info("Cleaning property file "+pkgNameF+File.separator+src.getName());139try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {140writer.write(data.toString());141} catch ( IOException e ) {142Log.error("Could not write file "+dest.getPath());143return false;144}145return true;146}147}148149150