1 /*
   2  * Copyright (c) 2013, 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;
  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 }