src/share/classes/com/sun/tools/jdeps/Profile.java

Thu, 17 Jul 2014 15:23:08 -0700

author
mchung
date
Thu, 17 Jul 2014 15:23:08 -0700
changeset 2538
1e39ae45d8ac
parent 2172
aa91bc6e8480
child 2702
9ca8d8713094
permissions
-rw-r--r--

8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
8031092: jdeps does not recognize --help option.
8048063: (jdeps) Add filtering capability
Reviewed-by: alanb, dfuchs

     1 /*
     2  * Copyright (c) 2013, 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.jdeps;
    27 import com.sun.tools.classfile.Annotation;
    28 import com.sun.tools.classfile.Annotation.*;
    29 import com.sun.tools.classfile.Attribute;
    30 import com.sun.tools.classfile.ClassFile;
    31 import com.sun.tools.classfile.ConstantPool.*;
    32 import com.sun.tools.classfile.ConstantPoolException;
    33 import com.sun.tools.classfile.RuntimeAnnotations_attribute;
    34 import java.io.FileReader;
    35 import java.io.IOException;
    36 import java.nio.file.Files;
    37 import java.nio.file.Path;
    38 import java.nio.file.Paths;
    39 import java.util.*;
    40 import java.util.jar.JarFile;
    42 /**
    43  * Build the profile information from ct.sym if exists.
    44  */
    45 enum Profile {
    46     COMPACT1("compact1", 1),
    47     COMPACT2("compact2", 2),
    48     COMPACT3("compact3", 3),
    49     FULL_JRE("Full JRE", 4);
    51     final String name;
    52     final int profile;
    53     final Set<String> packages;
    54     final Set<String> proprietaryPkgs;
    56     Profile(String name, int profile) {
    57         this.name = name;
    58         this.profile = profile;
    59         this.packages = new HashSet<>();
    60         this.proprietaryPkgs = new HashSet<>();
    61     }
    63     public String profileName() {
    64         return name;
    65     }
    67     public static int getProfileCount() {
    68         return PackageToProfile.map.values().size();
    69     }
    71     /**
    72      * Returns the Profile for the given package name. It returns an empty
    73      * string if the given package is not in any profile.
    74      */
    75     public static Profile getProfile(String pn) {
    76         Profile profile = PackageToProfile.map.get(pn);
    77         return (profile != null && profile.packages.contains(pn))
    78                     ? profile : null;
    79     }
    81     static class PackageToProfile {
    82         static String[] JAVAX_CRYPTO_PKGS = new String[] {
    83             "javax.crypto",
    84             "javax.crypto.interfaces",
    85             "javax.crypto.spec"
    86         };
    87         static Map<String, Profile> map = initProfiles();
    88         private static Map<String, Profile> initProfiles() {
    89             try {
    90                 String profilesProps = System.getProperty("jdeps.profiles");
    91                 if (profilesProps != null) {
    92                     // for testing for JDK development build where ct.sym doesn't exist
    93                     initProfilesFromProperties(profilesProps);
    94                 } else {
    95                     Path home = Paths.get(System.getProperty("java.home"));
    96                     if (home.endsWith("jre")) {
    97                         home = home.getParent();
    98                     }
    99                     Path ctsym = home.resolve("lib").resolve("ct.sym");
   100                     if (Files.exists(ctsym)) {
   101                         // parse ct.sym and load information about profiles
   102                         try (JarFile jf = new JarFile(ctsym.toFile())) {
   103                             ClassFileReader reader = ClassFileReader.newInstance(ctsym, jf);
   104                             for (ClassFile cf : reader.getClassFiles()) {
   105                                 findProfile(cf);
   106                             }
   107                         }
   108                         // special case for javax.crypto.* classes that are not
   109                         // included in ct.sym since they are in jce.jar
   110                         Collections.addAll(Profile.COMPACT1.packages, JAVAX_CRYPTO_PKGS);
   111                     }
   112                 }
   113             } catch (IOException | ConstantPoolException e) {
   114                 throw new Error(e);
   115             }
   116             HashMap<String,Profile> map = new HashMap<>();
   117             for (Profile profile : Profile.values()) {
   118                 for (String pn : profile.packages) {
   119                     if (!map.containsKey(pn)) {
   120                         // split packages in the JRE: use the smaller compact
   121                         map.put(pn, profile);
   122                     }
   123                 }
   124                 for (String pn : profile.proprietaryPkgs) {
   125                     if (!map.containsKey(pn)) {
   126                         map.put(pn, profile);
   127                     }
   128                 }
   129             }
   130             return map;
   131         }
   132         private static final String PROFILE_ANNOTATION = "Ljdk/Profile+Annotation;";
   133         private static final String PROPRIETARY_ANNOTATION = "Lsun/Proprietary+Annotation;";
   134         private static Profile findProfile(ClassFile cf) throws ConstantPoolException {
   135             RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)
   136                 cf.attributes.get(Attribute.RuntimeInvisibleAnnotations);
   137             int index = 0;
   138             boolean proprietary = false;
   139             if (attr != null) {
   140                 for (int i = 0; i < attr.annotations.length; i++) {
   141                     Annotation ann = attr.annotations[i];
   142                     String annType = cf.constant_pool.getUTF8Value(ann.type_index);
   143                     if (PROFILE_ANNOTATION.equals(annType)) {
   144                         for (int j = 0; j < ann.num_element_value_pairs; j++) {
   145                             Annotation.element_value_pair pair = ann.element_value_pairs[j];
   146                             Primitive_element_value ev = (Primitive_element_value) pair.value;
   147                             CONSTANT_Integer_info info = (CONSTANT_Integer_info)
   148                                 cf.constant_pool.get(ev.const_value_index);
   149                             index = info.value;
   150                             break;
   151                         }
   152                     } else if (PROPRIETARY_ANNOTATION.equals(annType)) {
   153                         proprietary = true;
   154                     }
   155                 }
   156             }
   158             Profile p = null;  // default
   159             switch (index) {
   160                 case 1:
   161                     p = Profile.COMPACT1; break;
   162                 case 2:
   163                     p = Profile.COMPACT2; break;
   164                 case 3:
   165                     p = Profile.COMPACT3; break;
   166                 case 4:
   167                     p = Profile.FULL_JRE; break;
   168                 default:
   169                     // skip classes with profile=0
   170                     // Inner classes are not annotated with the profile annotation
   171                     return null;
   172             }
   174             String name = cf.getName();
   175             int i = name.lastIndexOf('/');
   176             name = (i > 0) ? name.substring(0, i).replace('/', '.') : "";
   177             if (proprietary) {
   178                 p.proprietaryPkgs.add(name);
   179             } else {
   180                 p.packages.add(name);
   181             }
   182             return p;
   183         }
   185         private static void initProfilesFromProperties(String path) throws IOException {
   186             Properties props = new Properties();
   187             try (FileReader reader = new FileReader(path)) {
   188                 props.load(reader);
   189             }
   190             for (Profile prof : Profile.values()) {
   191                 int i = prof.profile;
   192                 String key = props.getProperty("profile." + i + ".name");
   193                 if (key == null) {
   194                     throw new RuntimeException(key + " missing in " + path);
   195                 }
   196                 String n = props.getProperty("profile." + i + ".packages");
   197                 String[] pkgs = n.split("\\s+");
   198                 for (String p : pkgs) {
   199                     if (p.isEmpty()) continue;
   200                     prof.packages.add(p);
   201                 }
   202             }
   203         }
   204     }
   206     // for debugging
   207     public static void main(String[] args) {
   208         if (args.length == 0) {
   209             if (Profile.getProfileCount() == 0) {
   210                 System.err.println("No profile is present in this JDK");
   211             }
   212             for (Profile p : Profile.values()) {
   213                 String profileName = p.name;
   214                 SortedSet<String> set = new TreeSet<>(p.packages);
   215                 for (String s : set) {
   216                     // filter out the inner classes that are not annotated with
   217                     // the profile annotation
   218                     if (PackageToProfile.map.get(s) == p) {
   219                         System.out.format("%2d: %-10s  %s%n", p.profile, profileName, s);
   220                         profileName = "";
   221                     } else {
   222                         System.err.format("Split package: %s in %s and %s %n",
   223                             s, PackageToProfile.map.get(s).name, p.name);
   224                     }
   225                 }
   226             }
   227         }
   228         for (String pn : args) {
   229             System.out.format("%s in %s%n", pn, getProfile(pn));
   230         }
   231     }
   232 }

mercurial