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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2006, 2014, 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;
    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;
    42 import com.sun.tools.javac.util.Assert;
    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) {
    57             Map<Integer,Set<String>> lists = new TreeMap<Integer,Set<String>>();
    58             for (int i = 1; i <= 4; i++)
    59                 lists.put(i, new TreeSet<String>());
    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             }
    71             for (int i = 1; i <= 4; i++) {
    72                 BufferedWriter out = new BufferedWriter(new FileWriter(i + ".txt"));
    73                 try {
    74                     for (String type: lists.get(i)) {
    75                         out.write(type);
    76                         out.newLine();
    77                     }
    78                 } finally {
    79                     out.close();
    80                 }
    81             }
    82         }
    83     }
    85     public static Profiles read(File file) throws IOException {
    86         BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    87         try {
    88             Properties p = new Properties();
    89             p.load(in);
    90             if (p.containsKey("java/lang/Object"))
    91                 return new SimpleProfiles(p);
    92             else
    93                 return new MakefileProfiles(p);
    94         } finally {
    95             in.close();
    96         }
    97     }
    99     public abstract int getProfileCount();
   101     public abstract int getProfile(String typeName);
   103     public abstract Set<String> getPackages(int profile);
   105     private static class MakefileProfiles extends Profiles {
   106         static class Package {
   107             final Package parent;
   108             final String name;
   110             Map<String, Package> subpackages = new TreeMap<String, Package>();
   112             int profile;
   113             Map<String, Integer> includedTypes = new TreeMap<String,Integer>();
   114             Map<String, Integer> excludedTypes = new TreeMap<String,Integer>();
   116             Package(Package parent, String name) {
   117                 this.parent = parent;
   118                 this.name = name;
   119             }
   121             int getProfile() {
   122                 return (parent == null) ? profile : Math.max(parent.getProfile(), profile);
   123             }
   125             int getProfile(String simpleTypeName) {
   126                 Integer i;
   127                 if ((i = includedTypes.get(simpleTypeName)) != null)
   128                     return i;
   129                 if ((i = includedTypes.get("*")) != null)
   130                     return i;
   131                 if ((i = excludedTypes.get(simpleTypeName)) != null)
   132                     return i + 1;
   133                 if ((i = excludedTypes.get("*")) != null)
   134                     return i + 1;
   135                 return getProfile();
   136             }
   138             String getName() {
   139                 return (parent == null) ? name : (parent.getName() + "/" + name);
   140             }
   142             void getPackages(int profile, Set<String> results) {
   143                 int prf = getProfile();
   144                 if (prf != 0 && profile >= prf)
   145                     results.add(getName());
   146                 for (Package pkg: subpackages.values())
   147                     pkg.getPackages(profile, results);
   148             }
   149         }
   151         final Map<String, Package> packages = new TreeMap<String, Package>();
   153         final int maxProfile = 4;  // Three compact profiles plus full JRE
   155         MakefileProfiles(Properties p) {
   156             // consider crypto, only if java/lang package exists
   157             boolean foundJavaLang = false;
   158             for (int profile = 1; profile <= maxProfile; profile++) {
   159                 String prefix = (profile < maxProfile ? "PROFILE_" + profile : "FULL_JRE");
   160                 String inclPackages = p.getProperty(prefix + "_RTJAR_INCLUDE_PACKAGES");
   161                 if (inclPackages == null)
   162                     break;
   163                 for (String pkg: inclPackages.substring(1).trim().split("\\s+")) {
   164                     if (pkg.endsWith("/"))
   165                         pkg = pkg.substring(0, pkg.length() - 1);
   166                     if (foundJavaLang == false && pkg.equals("java/lang"))
   167                         foundJavaLang = true;
   168                     includePackage(profile, pkg);
   169                 }
   170                 String inclTypes =  p.getProperty(prefix + "_RTJAR_INCLUDE_TYPES");
   171                 if (inclTypes != null) {
   172                     for (String type: inclTypes.replace("$$", "$").split("\\s+")) {
   173                         if (type.endsWith(".class"))
   174                             includeType(profile, type.substring(0, type.length() - 6));
   175                     }
   176                 }
   177                 String exclTypes =  p.getProperty(prefix + "_RTJAR_EXCLUDE_TYPES");
   178                 if (exclTypes != null) {
   179                     for (String type: exclTypes.replace("$$", "$").split("\\s+")) {
   180                         if (type.endsWith(".class"))
   181                             excludeType(profile, type.substring(0, type.length() - 6));
   182                     }
   183                 }
   184             }
   185             /*
   186              * A hack to force javax/crypto package into the compact1 profile,
   187              * because this package exists in jce.jar, and therefore not in
   188              * ct.sym. Note javax/crypto should exist in a profile along with
   189              * javax/net/ssl package. Thus, this package is added to compact1,
   190              * implying that it should exist in all three profiles.
   191              */
   192             if (foundJavaLang)
   193                 includePackage(1, "javax/crypto");
   194         }
   196         @Override
   197         public int getProfileCount() {
   198             return maxProfile;
   199         }
   201         @Override
   202         public int getProfile(String typeName) {
   203             int sep = typeName.lastIndexOf("/");
   204             String packageName = typeName.substring(0, sep);
   205             String simpleName = typeName.substring(sep + 1);
   207             Package p = getPackage(packageName);
   208             return p.getProfile(simpleName);
   209         }
   211         @Override
   212         public Set<String> getPackages(int profile) {
   213             Set<String> results = new TreeSet<String>();
   214             for (Package p: packages.values())
   215                 p.getPackages(profile, results);
   216             return results;
   217         }
   219         private void includePackage(int profile, String packageName) {
   220 //            System.err.println("include package " + packageName);
   221             Package p = getPackage(packageName);
   222             Assert.check(p.profile == 0);
   223             p.profile = profile;
   224         }
   226         private void includeType(int profile, String typeName) {
   227 //            System.err.println("include type " + typeName);
   228             int sep = typeName.lastIndexOf("/");
   229             String packageName = typeName.substring(0, sep);
   230             String simpleName = typeName.substring(sep + 1);
   232             Package p = getPackage(packageName);
   233             Assert.check(!p.includedTypes.containsKey(simpleName));
   234             p.includedTypes.put(simpleName, profile);
   235         }
   237         private void excludeType(int profile, String typeName) {
   238 //            System.err.println("exclude type " + typeName);
   239             int sep = typeName.lastIndexOf("/");
   240             String packageName = typeName.substring(0, sep);
   241             String simpleName = typeName.substring(sep + 1);
   243             Package p = getPackage(packageName);
   244             Assert.check(!p.excludedTypes.containsKey(simpleName));
   245             p.excludedTypes.put(simpleName, profile);
   246         }
   248         private Package getPackage(String packageName) {
   249             int sep = packageName.lastIndexOf("/");
   250             Package parent;
   251             Map<String, Package> parentSubpackages;
   252             String simpleName;
   253             if (sep == -1) {
   254                 parent = null;
   255                 parentSubpackages = packages;
   256                 simpleName = packageName;
   257             } else {
   258                 parent = getPackage(packageName.substring(0, sep));
   259                 parentSubpackages = parent.subpackages;
   260                 simpleName = packageName.substring(sep + 1);
   261             }
   263             Package p = parentSubpackages.get(simpleName);
   264             if (p == null) {
   265                 parentSubpackages.put(simpleName, p = new Package(parent, simpleName));
   266             }
   267             return p;
   268         }
   269     }
   271     private static class SimpleProfiles extends Profiles {
   272         private final Map<String, Integer> map;
   273         private final int profileCount;
   275         SimpleProfiles(Properties p) {
   276             int max = 0;
   277             map = new HashMap<String, Integer>();
   278             for (Map.Entry<Object,Object> e: p.entrySet()) {
   279                 String typeName = (String) e.getKey();
   280                 int profile = Integer.valueOf((String) e.getValue());
   281                 map.put(typeName, profile);
   282                 max = Math.max(max, profile);
   283             }
   284             profileCount = max;
   285         }
   287         @Override
   288         public int getProfileCount() {
   289             return profileCount;
   290         }
   292         @Override
   293         public int getProfile(String typeName) {
   294             return map.get(typeName);
   295         }
   297         @Override
   298         public Set<String> getPackages(int profile) {
   299             Set<String> results = new TreeSet<String>();
   300             for (Map.Entry<String,Integer> e: map.entrySet()) {
   301                 String tn = e.getKey();
   302                 int prf = e.getValue();
   303                 int sep = tn.lastIndexOf("/");
   304                 if (sep > 0 && profile >= prf)
   305                     results.add(tn);
   306             }
   307             return results;
   308         }
   309     }
   310 }

mercurial