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

Print this page




  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;
  26 
  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.ConstantPool.*;
  33 import com.sun.tools.classfile.ConstantPoolException;
  34 import com.sun.tools.classfile.RuntimeAnnotations_attribute;
  35 import java.io.File;
  36 import java.io.FileReader;
  37 import java.io.IOException;

  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.*;
  41 import java.util.jar.JarFile;
  42 
  43 /**
  44  * Build the profile information from ct.sym if exists.
  45  */
  46 class Profiles {
  47     private static final Map<String,Profile> map = initProfiles();


























  48     /**
  49      * Returns the name of the profile for the given package name.
  50      * It returns an empty string if the given package is not in any profile.
  51      */
  52     public static String getProfileName(String pn) {
  53         Profile profile = map.get(pn);
  54         return (profile != null && profile.packages.contains(pn))
  55                     ? profile.name : "";
  56     }
  57 
  58     public static int getProfileCount() {
  59         return new HashSet<Profile>(map.values()).size();
  60     }
  61 
  62     private static Map<String,Profile> initProfiles() {
  63         List<Profile> profiles = new ArrayList<Profile>();
  64         try {
  65             String profilesProps = System.getProperty("jdeps.profiles");
  66             if (profilesProps != null) {
  67                 // for testing for JDK development build where ct.sym doesn't exist
  68                 initProfilesFromProperties(profiles, profilesProps);
  69             } else {
  70                 Path home = Paths.get(System.getProperty("java.home"));
  71                 if (home.endsWith("jre")) {
  72                     home = home.getParent();
  73                 }
  74                 Path ctsym = home.resolve("lib").resolve("ct.sym");
  75                 if (ctsym.toFile().exists()) {
  76                     // add a default Full JRE
  77                     profiles.add(0, new Profile("Full JRE", 0));
  78                     // parse ct.sym and load information about profiles
  79                     try (JarFile jf = new JarFile(ctsym.toFile())) {
  80                         ClassFileReader reader = ClassFileReader.newInstance(ctsym, jf);
  81                         for (ClassFile cf : reader.getClassFiles()) {
  82                             findProfile(profiles, cf);
  83                         }
  84                     }
  85 
  86                     // merge the last Profile with the "Full JRE"
  87                     if (profiles.size() > 1) {
  88                         Profile fullJRE = profiles.get(0);
  89                         Profile p = profiles.remove(profiles.size() - 1);
  90                         for (String pn : fullJRE.packages) {
  91                             // The last profile contains the packages determined from ct.sym.
  92                             // Move classes annotated profile==0 or no attribute that are
  93                             // added in the fullJRE profile to either supported or proprietary
  94                             // packages appropriately
  95                             if (p.proprietaryPkgs.contains(pn)) {
  96                                 p.proprietaryPkgs.add(pn);
  97                             } else {
  98                                 p.packages.add(pn);
  99                             }
 100                         }
 101                         fullJRE.packages.clear();
 102                         fullJRE.proprietaryPkgs.clear();
 103                         fullJRE.packages.addAll(p.packages);
 104                         fullJRE.proprietaryPkgs.addAll(p.proprietaryPkgs);
 105                     }
 106                 }
 107             }
 108         } catch (IOException | ConstantPoolException e) {
 109             throw new Error(e);
 110         }
 111         HashMap<String,Profile> map = new HashMap<String,Profile>();
 112         for (Profile profile : profiles) {
 113             // Inner classes are not annotated with the profile annotation
 114             // packages may be in one profile but also appear in the Full JRE
 115             // Full JRE is always the first element in profiles list and
 116             // so the map will contain the appropriate Profile
 117             for (String pn : profile.packages) {


 118                 map.put(pn, profile);
 119             }

 120             for (String pn : profile.proprietaryPkgs) {

 121                 map.put(pn, profile);
 122             }
 123         }

 124         return map;
 125     }
 126 
 127     private static final String PROFILE_ANNOTATION = "Ljdk/Profile+Annotation;";
 128     private static final String PROPRIETARY_ANNOTATION = "Lsun/Proprietary+Annotation;";
 129     private static Profile findProfile(List<Profile> profiles, ClassFile cf)
 130             throws ConstantPoolException
 131     {
 132         RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)
 133             cf.attributes.get(Attribute.RuntimeInvisibleAnnotations);
 134         int index = 0;
 135         boolean proprietary = false;
 136         if (attr != null) {
 137             for (int i = 0; i < attr.annotations.length; i++) {
 138                 Annotation ann = attr.annotations[i];
 139                 String annType = cf.constant_pool.getUTF8Value(ann.type_index);
 140                 if (PROFILE_ANNOTATION.equals(annType)) {
 141                     for (int j = 0; j < ann.num_element_value_pairs; j++) {
 142                         Annotation.element_value_pair pair = ann.element_value_pairs[j];
 143                         Primitive_element_value ev = (Primitive_element_value)pair.value;
 144                         CONSTANT_Integer_info info = (CONSTANT_Integer_info)
 145                              cf.constant_pool.get(ev.const_value_index);
 146                         index = info.value;
 147                         break;
 148                     }
 149                 } else if (PROPRIETARY_ANNOTATION.equals(annType)) {
 150                     proprietary = true;
 151                 }
 152             }
 153             if (index >= profiles.size()) {
 154                 Profile p = null;
 155                 for (int i = profiles.size(); i <= index; i++) {
 156                     p = new Profile(i);
 157                     profiles.add(p);
 158                 }
 159             }















 160         }
 161 
 162         Profile p = profiles.get(index);
 163         String name = cf.getName();
 164         int i = name.lastIndexOf('/');
 165         name = (i > 0) ? name.substring(0, i).replace('/','.') : "";
 166         if (proprietary) {
 167             p.proprietaryPkgs.add(name);
 168         } else {
 169             p.packages.add(name);
 170         }
 171         return p;
 172     }
 173 
 174     private static void initProfilesFromProperties(List<Profile> profiles, String path)
 175             throws IOException
 176     {
 177         Properties props = new Properties();
 178         try (FileReader reader = new FileReader(path)) {
 179             props.load(reader);
 180         }
 181         int i=1;
 182         String key;
 183         while (props.containsKey((key = "profile." + i + ".name"))) {
 184             Profile profile = new Profile(props.getProperty(key), i);
 185             profiles.add(profile);

 186             String n = props.getProperty("profile." + i + ".packages");
 187             String[] pkgs = n.split("\\s+");
 188             for (String p : pkgs) {
 189                 if (p.isEmpty()) continue;
 190                 profile.packages.add(p);
 191             }
 192             i++;
 193         }
 194     }
 195 
 196     private static class Profile {
 197         final String name;
 198         final int profile;
 199         final Set<String> packages;
 200         final Set<String> proprietaryPkgs;
 201         Profile(int profile) {
 202             this("compact" + profile, profile);
 203         }
 204         Profile(String name, int profile) {
 205             this.name = name;
 206             this.profile = profile;
 207             this.packages = new HashSet<String>();
 208             this.proprietaryPkgs = new HashSet<String>();
 209         }
 210         public String toString() {
 211             return name;
 212         }
 213     }
 214 
 215     // for debugging
 216     public static void main(String[] args) {
 217         if (args.length == 0) {
 218             Profile[] profiles = new Profile[getProfileCount()];
 219             for (Profile p : map.values()) {
 220                 // move the zeroth profile to the last
 221                 int index = p.profile == 0 ? profiles.length-1 : p.profile-1;
 222                 profiles[index] = p;
 223             }
 224             for (Profile p : profiles) {
 225                 String profileName = p.name;
 226                 SortedSet<String> set = new TreeSet<String>(p.packages);
 227                 for (String s : set) {
 228                     // filter out the inner classes that are not annotated with
 229                     // the profile annotation
 230                     if (map.get(s) == p) {
 231                         System.out.format("%-10s  %s%n", profileName, s);
 232                         profileName = "";



 233                     }
 234                 }
 235             }
 236         }
 237         for (String pn : args) {
 238             System.out.format("%s in %s%n", pn, getProfileName(pn));
 239         }
 240     }
 241 }


  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;
  26 
  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;
  41 
  42 /**
  43  * Build the profile information from ct.sym if exists.
  44  */
  45 enum Profile {
  46 
  47     COMPACT1("compact1", 1),
  48     COMPACT2("compact2", 2),
  49     COMPACT3("compact3", 3),
  50     FULL_JRE("Full JRE", 4);
  51 
  52     final String name;
  53     final int profile;
  54     final Set<String> packages;
  55     final Set<String> proprietaryPkgs;
  56 
  57     Profile(String name, int profile) {
  58         this.name = name;
  59         this.profile = profile;
  60         this.packages = new HashSet<>();
  61         this.proprietaryPkgs = new HashSet<>();
  62     }
  63 
  64     @Override
  65     public String toString() {
  66         return name;
  67     }
  68 
  69     public static int getProfileCount() {
  70         return PackageToProfile.map.values().size();
  71     }
  72 
  73     /**
  74      * Returns the Profile for the given package name. It returns an empty
  75      * string if the given package is not in any profile.
  76      */
  77     public static Profile getProfile(String pn) {
  78         Profile profile = PackageToProfile.map.get(pn);
  79         return (profile != null && profile.packages.contains(pn))
  80                 ? profile : null;
  81     }
  82 
  83     static class PackageToProfile {
  84         static Map<String, Profile> map = initProfiles();

  85 
  86         private static Map<String, Profile> initProfiles() {

  87             try {
  88                 String profilesProps = System.getProperty("jdeps.profiles");
  89                 if (profilesProps != null) {
  90                     // for testing for JDK development build where ct.sym doesn't exist
  91                     initProfilesFromProperties(profilesProps);
  92                 } else {
  93                     Path home = Paths.get(System.getProperty("java.home"));
  94                     if (home.endsWith("jre")) {
  95                         home = home.getParent();
  96                     }
  97                     Path ctsym = home.resolve("lib").resolve("ct.sym");
  98                     if (Files.exists(ctsym)) {


  99                         // parse ct.sym and load information about profiles
 100                         try (JarFile jf = new JarFile(ctsym.toFile())) {
 101                             ClassFileReader reader = ClassFileReader.newInstance(ctsym, jf);
 102                             for (ClassFile cf : reader.getClassFiles()) {
 103                                 findProfile(cf);

















 104                             }




 105                         }
 106                     }
 107                 }
 108             } catch (IOException | ConstantPoolException e) {
 109                 throw new Error(e);
 110             }
 111             HashMap<String,Profile> map = new HashMap<>();
 112             for (Profile profile : Profile.values()) {




 113                 for (String pn : profile.packages) {
 114                     if (!map.containsKey(pn)) {
 115                         // split packages in the JRE: use the smaller compact
 116                         map.put(pn, profile);
 117                     }
 118                 }
 119                 for (String pn : profile.proprietaryPkgs) {
 120                     if (!map.containsKey(pn)) {
 121                         map.put(pn, profile);
 122                     }
 123                 }
 124             }
 125             return map;
 126         }

 127         private static final String PROFILE_ANNOTATION = "Ljdk/Profile+Annotation;";
 128         private static final String PROPRIETARY_ANNOTATION = "Lsun/Proprietary+Annotation;";
 129         private static Profile findProfile(ClassFile cf) throws ConstantPoolException {


 130             RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)
 131                 cf.attributes.get(Attribute.RuntimeInvisibleAnnotations);
 132             int index = 0;
 133             boolean proprietary = false;
 134             if (attr != null) {
 135                 for (int i = 0; i < attr.annotations.length; i++) {
 136                     Annotation ann = attr.annotations[i];
 137                     String annType = cf.constant_pool.getUTF8Value(ann.type_index);
 138                     if (PROFILE_ANNOTATION.equals(annType)) {
 139                         for (int j = 0; j < ann.num_element_value_pairs; j++) {
 140                             Annotation.element_value_pair pair = ann.element_value_pairs[j];
 141                             Primitive_element_value ev = (Primitive_element_value) pair.value;
 142                             CONSTANT_Integer_info info = (CONSTANT_Integer_info)
 143                                 cf.constant_pool.get(ev.const_value_index);
 144                             index = info.value;
 145                             break;
 146                         }
 147                     } else if (PROPRIETARY_ANNOTATION.equals(annType)) {
 148                         proprietary = true;
 149                     }
 150                 }






 151             }
 152 
 153             Profile p = null;  // default
 154             switch (index) {
 155                 case 1:
 156                     p = Profile.COMPACT1; break;
 157                 case 2:
 158                     p = Profile.COMPACT2; break;
 159                 case 3:
 160                     p = Profile.COMPACT3; break;
 161                 case 4:
 162                     p = Profile.FULL_JRE; break;
 163                 default:
 164                     // skip classes with profile=0
 165                     // Inner classes are not annotated with the profile annotation
 166                     return null;
 167             }
 168 

 169             String name = cf.getName();
 170             int i = name.lastIndexOf('/');
 171             name = (i > 0) ? name.substring(0, i).replace('/', '.') : "";
 172             if (proprietary) {
 173                 p.proprietaryPkgs.add(name);
 174             } else {
 175                 p.packages.add(name);
 176             }
 177             return p;
 178         }
 179 
 180         private static void initProfilesFromProperties(String path) throws IOException {


 181             Properties props = new Properties();
 182             try (FileReader reader = new FileReader(path)) {
 183                 props.load(reader);
 184             }
 185             for (Profile prof : Profile.values()) {
 186                 int i = prof.profile;
 187                 String key = props.getProperty("profile." + i + ".name");
 188                 if (key == null) {
 189                     throw new RuntimeException(key + " missing in " + path);
 190                 }
 191                 String n = props.getProperty("profile." + i + ".packages");
 192                 String[] pkgs = n.split("\\s+");
 193                 for (String p : pkgs) {
 194                     if (p.isEmpty()) continue;
 195                     prof.packages.add(p);



 196                 }














 197             }


 198         }
 199     }
 200 
 201     // for debugging
 202     public static void main(String[] args) {
 203         if (args.length == 0) {
 204             if (Profile.getProfileCount() == 0) {
 205                 System.err.println("No profile is present in this JDK");



 206             }
 207             for (Profile p : Profile.values()) {
 208                 String profileName = p.name;
 209                 SortedSet<String> set = new TreeSet<>(p.packages);
 210                 for (String s : set) {
 211                     // filter out the inner classes that are not annotated with
 212                     // the profile annotation
 213                     if (PackageToProfile.map.get(s) == p) {
 214                         System.out.format("%2d: %-10s  %s%n", p.profile, profileName, s);
 215                         profileName = "";
 216                     } else {
 217                         System.err.format("Split package: %s in %s and %s %n",
 218                             s, PackageToProfile.map.get(s).name, p.name);
 219                     }
 220                 }
 221             }
 222         }
 223         for (String pn : args) {
 224             System.out.format("%s in %s%n", pn, getProfile(pn));
 225         }
 226     }
 227 }