src/share/classes/com/sun/tools/javac/sym/Profiles.java

changeset 1569
475eb15dfdad
child 1636
82dc1e827c2a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/sym/Profiles.java	Mon Jan 21 01:27:42 2013 -0500
     1.3 @@ -0,0 +1,298 @@
     1.4 +/*
     1.5 + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.javac.sym;
    1.29 +
    1.30 +import java.io.BufferedInputStream;
    1.31 +import java.io.BufferedWriter;
    1.32 +import java.io.File;
    1.33 +import java.io.FileInputStream;
    1.34 +import java.io.FileWriter;
    1.35 +import java.io.IOException;
    1.36 +import java.nio.charset.Charset;
    1.37 +import java.nio.file.Files;
    1.38 +import java.util.HashMap;
    1.39 +import java.util.Map;
    1.40 +import java.util.Properties;
    1.41 +import java.util.Set;
    1.42 +import java.util.TreeMap;
    1.43 +import java.util.TreeSet;
    1.44 +
    1.45 +import com.sun.tools.javac.util.Assert;
    1.46 +
    1.47 +/**
    1.48 + * Provide details about profile contents.
    1.49 + *
    1.50 + * <p><b>This is NOT part of any supported API.
    1.51 + * If you write code that depends on this, you do so at your own
    1.52 + * risk.  This code and its internal interfaces are subject to change
    1.53 + * or deletion without notice.</b></p>
    1.54 + */
    1.55 +public abstract class Profiles {
    1.56 +    // for debugging
    1.57 +    public static void main(String[] args) throws IOException {
    1.58 +        Profiles p = Profiles.read(new File(args[0]));
    1.59 +        if (args.length >= 2) {
    1.60 +            Map<Integer,Set<String>> lists = new TreeMap<Integer,Set<String>>();
    1.61 +            for (int i = 1; i <= 4; i++)
    1.62 +                lists.put(i, new TreeSet<String>());
    1.63 +
    1.64 +            File rt_jar_lst = new File(args[1]);
    1.65 +            for (String line: Files.readAllLines(rt_jar_lst.toPath(), Charset.defaultCharset())) {
    1.66 +                if (line.endsWith(".class")) {
    1.67 +                    String type = line.substring(0, line.length() - 6);
    1.68 +                    int profile = p.getProfile(type);
    1.69 +                    for (int i = profile; i <= 4; i++)
    1.70 +                        lists.get(i).add(type);
    1.71 +                }
    1.72 +            }
    1.73 +
    1.74 +            for (int i = 1; i <= 4; i++) {
    1.75 +                BufferedWriter out = new BufferedWriter(new FileWriter(i + ".txt"));
    1.76 +                try {
    1.77 +                    for (String type: lists.get(i)) {
    1.78 +                        out.write(type);
    1.79 +                        out.newLine();
    1.80 +                    }
    1.81 +                } finally {
    1.82 +                    out.close();
    1.83 +                }
    1.84 +            }
    1.85 +        }
    1.86 +    }
    1.87 +
    1.88 +    public static Profiles read(File file) throws IOException {
    1.89 +        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    1.90 +        try {
    1.91 +            Properties p = new Properties();
    1.92 +            p.load(in);
    1.93 +            if (p.containsKey("java/lang/Object"))
    1.94 +                return new SimpleProfiles(p);
    1.95 +            else
    1.96 +                return new MakefileProfiles(p);
    1.97 +        } finally {
    1.98 +            in.close();
    1.99 +        }
   1.100 +    }
   1.101 +
   1.102 +    public abstract int getProfileCount();
   1.103 +
   1.104 +    public abstract int getProfile(String typeName);
   1.105 +
   1.106 +    public abstract Set<String> getPackages(int profile);
   1.107 +
   1.108 +    private static class MakefileProfiles extends Profiles {
   1.109 +        static class Package {
   1.110 +            final Package parent;
   1.111 +            final String name;
   1.112 +
   1.113 +            Map<String, Package> subpackages = new TreeMap<String, Package>();
   1.114 +
   1.115 +            int profile;
   1.116 +            Map<String, Integer> includedTypes = new TreeMap<String,Integer>();
   1.117 +            Map<String, Integer> excludedTypes = new TreeMap<String,Integer>();
   1.118 +
   1.119 +            Package(Package parent, String name) {
   1.120 +                this.parent = parent;
   1.121 +                this.name = name;
   1.122 +            }
   1.123 +
   1.124 +            int getProfile() {
   1.125 +                return (parent == null) ? profile : Math.max(parent.getProfile(), profile);
   1.126 +            }
   1.127 +
   1.128 +            int getProfile(String simpleTypeName) {
   1.129 +                Integer i;
   1.130 +                if ((i = includedTypes.get(simpleTypeName)) != null)
   1.131 +                    return i;
   1.132 +                if ((i = includedTypes.get("*")) != null)
   1.133 +                    return i;
   1.134 +                if ((i = excludedTypes.get(simpleTypeName)) != null)
   1.135 +                    return i + 1;
   1.136 +                if ((i = excludedTypes.get("*")) != null)
   1.137 +                    return i + 1;
   1.138 +                return getProfile();
   1.139 +            }
   1.140 +
   1.141 +            String getName() {
   1.142 +                return (parent == null) ? name : (parent.getName() + "/" + name);
   1.143 +            }
   1.144 +
   1.145 +            void getPackages(int profile, Set<String> results) {
   1.146 +                int prf = getProfile();
   1.147 +                if (prf != 0 && profile >= prf)
   1.148 +                    results.add(getName());
   1.149 +                for (Package pkg: subpackages.values())
   1.150 +                    pkg.getPackages(profile, results);
   1.151 +            }
   1.152 +        }
   1.153 +
   1.154 +        final static Map<String, Package> packages = new TreeMap<String, Package>();
   1.155 +        int maxProfile;
   1.156 +
   1.157 +        MakefileProfiles(Properties p) {
   1.158 +            int profile = 1;
   1.159 +            while (true) {
   1.160 +                String inclPackages = p.getProperty("PROFILE_" + profile + "_RTJAR_INCLUDE_PACKAGES");
   1.161 +                if (inclPackages == null)
   1.162 +                    break;
   1.163 +                for (String pkg: inclPackages.substring(1).trim().split("\\s+")) {
   1.164 +                    if (pkg.endsWith("/"))
   1.165 +                        pkg = pkg.substring(0, pkg.length() - 1);
   1.166 +                    includePackage(profile, pkg);
   1.167 +                }
   1.168 +                String inclTypes =  p.getProperty("PROFILE_" + profile + "_RTJAR_INCLUDE_TYPES");
   1.169 +                if (inclTypes != null) {
   1.170 +                    for (String type: inclTypes.replace("$$", "$").split("\\s+")) {
   1.171 +                        if (type.endsWith(".class"))
   1.172 +                            includeType(profile, type.substring(0, type.length() - 6));
   1.173 +                    }
   1.174 +                }
   1.175 +                String exclTypes =  p.getProperty("PROFILE_" + profile + "_RTJAR_EXCLUDE_TYPES");
   1.176 +                if (exclTypes != null) {
   1.177 +                    for (String type: exclTypes.replace("$$", "$").split("\\s+")) {
   1.178 +                        if (type.endsWith(".class"))
   1.179 +                            excludeType(profile, type.substring(0, type.length() - 6));
   1.180 +                    }
   1.181 +                }
   1.182 +                maxProfile = profile;
   1.183 +                profile++;
   1.184 +            }
   1.185 +        }
   1.186 +
   1.187 +        @Override
   1.188 +        public int getProfileCount() {
   1.189 +            return maxProfile;
   1.190 +        }
   1.191 +
   1.192 +        @Override
   1.193 +        public int getProfile(String typeName) {
   1.194 +            int sep = typeName.lastIndexOf("/");
   1.195 +            String packageName = typeName.substring(0, sep);
   1.196 +            String simpleName = typeName.substring(sep + 1);
   1.197 +
   1.198 +            Package p = getPackage(packageName);
   1.199 +            return p.getProfile(simpleName);
   1.200 +        }
   1.201 +
   1.202 +        @Override
   1.203 +        public Set<String> getPackages(int profile) {
   1.204 +            Set<String> results = new TreeSet<String>();
   1.205 +            for (Package p: packages.values())
   1.206 +                p.getPackages(profile, results);
   1.207 +            return results;
   1.208 +        }
   1.209 +
   1.210 +        private void includePackage(int profile, String packageName) {
   1.211 +//            System.err.println("include package " + packageName);
   1.212 +            Package p = getPackage(packageName);
   1.213 +            Assert.check(p.profile == 0);
   1.214 +            p.profile = profile;
   1.215 +        }
   1.216 +
   1.217 +        private void includeType(int profile, String typeName) {
   1.218 +//            System.err.println("include type " + typeName);
   1.219 +            int sep = typeName.lastIndexOf("/");
   1.220 +            String packageName = typeName.substring(0, sep);
   1.221 +            String simpleName = typeName.substring(sep + 1);
   1.222 +
   1.223 +            Package p = getPackage(packageName);
   1.224 +            Assert.check(!p.includedTypes.containsKey(simpleName));
   1.225 +            p.includedTypes.put(simpleName, profile);
   1.226 +        }
   1.227 +
   1.228 +        private void excludeType(int profile, String typeName) {
   1.229 +//            System.err.println("exclude type " + typeName);
   1.230 +            int sep = typeName.lastIndexOf("/");
   1.231 +            String packageName = typeName.substring(0, sep);
   1.232 +            String simpleName = typeName.substring(sep + 1);
   1.233 +
   1.234 +            Package p = getPackage(packageName);
   1.235 +            Assert.check(!p.excludedTypes.containsKey(simpleName));
   1.236 +            p.excludedTypes.put(simpleName, profile);
   1.237 +        }
   1.238 +
   1.239 +        private Package getPackage(String packageName) {
   1.240 +            int sep = packageName.lastIndexOf("/");
   1.241 +            Package parent;
   1.242 +            Map<String, Package> parentSubpackages;
   1.243 +            String simpleName;
   1.244 +            if (sep == -1) {
   1.245 +                parent = null;
   1.246 +                parentSubpackages = packages;
   1.247 +                simpleName = packageName;
   1.248 +            } else {
   1.249 +                parent = getPackage(packageName.substring(0, sep));
   1.250 +                parentSubpackages = parent.subpackages;
   1.251 +                simpleName = packageName.substring(sep + 1);
   1.252 +            }
   1.253 +
   1.254 +            Package p = parentSubpackages.get(simpleName);
   1.255 +            if (p == null) {
   1.256 +                parentSubpackages.put(simpleName, p = new Package(parent, simpleName));
   1.257 +            }
   1.258 +            return p;
   1.259 +        }
   1.260 +    }
   1.261 +
   1.262 +    private static class SimpleProfiles extends Profiles {
   1.263 +        private final Map<String, Integer> map;
   1.264 +        private final int profileCount;
   1.265 +
   1.266 +        SimpleProfiles(Properties p) {
   1.267 +            int max = 0;
   1.268 +            map = new HashMap<String, Integer>();
   1.269 +            for (Map.Entry<Object,Object> e: p.entrySet()) {
   1.270 +                String typeName = (String) e.getKey();
   1.271 +                int profile = Integer.valueOf((String) e.getValue());
   1.272 +                map.put(typeName, profile);
   1.273 +                max = Math.max(max, profile);
   1.274 +            }
   1.275 +            profileCount = max;
   1.276 +        }
   1.277 +
   1.278 +        @Override
   1.279 +        public int getProfileCount() {
   1.280 +            return profileCount;
   1.281 +        }
   1.282 +
   1.283 +        @Override
   1.284 +        public int getProfile(String typeName) {
   1.285 +            return map.get(typeName);
   1.286 +        }
   1.287 +
   1.288 +        @Override
   1.289 +        public Set<String> getPackages(int profile) {
   1.290 +            Set<String> results = new TreeSet<String>();
   1.291 +            for (Map.Entry<String,Integer> e: map.entrySet()) {
   1.292 +                String tn = e.getKey();
   1.293 +                int prf = e.getValue();
   1.294 +                int sep = tn.lastIndexOf("/");
   1.295 +                if (sep > 0 && profile >= prf)
   1.296 +                    results.add(tn);
   1.297 +            }
   1.298 +            return results;
   1.299 +        }
   1.300 +    }
   1.301 +}

mercurial