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

Print this page

        

@@ -26,111 +26,109 @@
 
 import com.sun.tools.classfile.Annotation;
 import com.sun.tools.classfile.Annotation.*;
 import com.sun.tools.classfile.Attribute;
 import com.sun.tools.classfile.ClassFile;
-import com.sun.tools.classfile.ConstantPool;
 import com.sun.tools.classfile.ConstantPool.*;
 import com.sun.tools.classfile.ConstantPoolException;
 import com.sun.tools.classfile.RuntimeAnnotations_attribute;
-import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.*;
 import java.util.jar.JarFile;
 
 /**
  * Build the profile information from ct.sym if exists.
  */
-class Profiles {
-    private static final Map<String,Profile> map = initProfiles();
+enum Profile {
+
+    COMPACT1("compact1", 1),
+    COMPACT2("compact2", 2),
+    COMPACT3("compact3", 3),
+    FULL_JRE("Full JRE", 4);
+
+    final String name;
+    final int profile;
+    final Set<String> packages;
+    final Set<String> proprietaryPkgs;
+
+    Profile(String name, int profile) {
+        this.name = name;
+        this.profile = profile;
+        this.packages = new HashSet<>();
+        this.proprietaryPkgs = new HashSet<>();
+    }
+
+    @Override
+    public String toString() {
+        return name;
+    }
+
+    public static int getProfileCount() {
+        return PackageToProfile.map.values().size();
+    }
+
     /**
-     * Returns the name of the profile for the given package name.
-     * It returns an empty string if the given package is not in any profile.
+     * Returns the Profile for the given package name. It returns an empty
+     * string if the given package is not in any profile.
      */
-    public static String getProfileName(String pn) {
-        Profile profile = map.get(pn);
+    public static Profile getProfile(String pn) {
+        Profile profile = PackageToProfile.map.get(pn);
         return (profile != null && profile.packages.contains(pn))
-                    ? profile.name : "";
+                ? profile : null;
     }
 
-    public static int getProfileCount() {
-        return new HashSet<Profile>(map.values()).size();
-    }
+    static class PackageToProfile {
+        static Map<String, Profile> map = initProfiles();
 
-    private static Map<String,Profile> initProfiles() {
-        List<Profile> profiles = new ArrayList<Profile>();
+        private static Map<String, Profile> initProfiles() {
         try {
             String profilesProps = System.getProperty("jdeps.profiles");
             if (profilesProps != null) {
                 // for testing for JDK development build where ct.sym doesn't exist
-                initProfilesFromProperties(profiles, profilesProps);
+                    initProfilesFromProperties(profilesProps);
             } else {
                 Path home = Paths.get(System.getProperty("java.home"));
                 if (home.endsWith("jre")) {
                     home = home.getParent();
                 }
                 Path ctsym = home.resolve("lib").resolve("ct.sym");
-                if (ctsym.toFile().exists()) {
-                    // add a default Full JRE
-                    profiles.add(0, new Profile("Full JRE", 0));
+                    if (Files.exists(ctsym)) {
                     // parse ct.sym and load information about profiles
                     try (JarFile jf = new JarFile(ctsym.toFile())) {
                         ClassFileReader reader = ClassFileReader.newInstance(ctsym, jf);
                         for (ClassFile cf : reader.getClassFiles()) {
-                            findProfile(profiles, cf);
-                        }
-                    }
-
-                    // merge the last Profile with the "Full JRE"
-                    if (profiles.size() > 1) {
-                        Profile fullJRE = profiles.get(0);
-                        Profile p = profiles.remove(profiles.size() - 1);
-                        for (String pn : fullJRE.packages) {
-                            // The last profile contains the packages determined from ct.sym.
-                            // Move classes annotated profile==0 or no attribute that are
-                            // added in the fullJRE profile to either supported or proprietary
-                            // packages appropriately
-                            if (p.proprietaryPkgs.contains(pn)) {
-                                p.proprietaryPkgs.add(pn);
-                            } else {
-                                p.packages.add(pn);
-                            }
+                                findProfile(cf);
                         }
-                        fullJRE.packages.clear();
-                        fullJRE.proprietaryPkgs.clear();
-                        fullJRE.packages.addAll(p.packages);
-                        fullJRE.proprietaryPkgs.addAll(p.proprietaryPkgs);
                     }
                 }
             }
         } catch (IOException | ConstantPoolException e) {
             throw new Error(e);
         }
-        HashMap<String,Profile> map = new HashMap<String,Profile>();
-        for (Profile profile : profiles) {
-            // Inner classes are not annotated with the profile annotation
-            // packages may be in one profile but also appear in the Full JRE
-            // Full JRE is always the first element in profiles list and
-            // so the map will contain the appropriate Profile
+            HashMap<String,Profile> map = new HashMap<>();
+            for (Profile profile : Profile.values()) {
             for (String pn : profile.packages) {
+                    if (!map.containsKey(pn)) {
+                        // split packages in the JRE: use the smaller compact
                 map.put(pn, profile);
             }
+                }
             for (String pn : profile.proprietaryPkgs) {
+                    if (!map.containsKey(pn)) {
                 map.put(pn, profile);
             }
         }
+            }
         return map;
     }
-
     private static final String PROFILE_ANNOTATION = "Ljdk/Profile+Annotation;";
     private static final String PROPRIETARY_ANNOTATION = "Lsun/Proprietary+Annotation;";
-    private static Profile findProfile(List<Profile> profiles, ClassFile cf)
-            throws ConstantPoolException
-    {
+        private static Profile findProfile(ClassFile cf) throws ConstantPoolException {
         RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)
             cf.attributes.get(Attribute.RuntimeInvisibleAnnotations);
         int index = 0;
         boolean proprietary = false;
         if (attr != null) {

@@ -138,104 +136,92 @@
                 Annotation ann = attr.annotations[i];
                 String annType = cf.constant_pool.getUTF8Value(ann.type_index);
                 if (PROFILE_ANNOTATION.equals(annType)) {
                     for (int j = 0; j < ann.num_element_value_pairs; j++) {
                         Annotation.element_value_pair pair = ann.element_value_pairs[j];
-                        Primitive_element_value ev = (Primitive_element_value)pair.value;
+                            Primitive_element_value ev = (Primitive_element_value) pair.value;
                         CONSTANT_Integer_info info = (CONSTANT_Integer_info)
                              cf.constant_pool.get(ev.const_value_index);
                         index = info.value;
                         break;
                     }
                 } else if (PROPRIETARY_ANNOTATION.equals(annType)) {
                     proprietary = true;
                 }
             }
-            if (index >= profiles.size()) {
-                Profile p = null;
-                for (int i = profiles.size(); i <= index; i++) {
-                    p = new Profile(i);
-                    profiles.add(p);
-                }
             }
+
+            Profile p = null;  // default
+            switch (index) {
+                case 1:
+                    p = Profile.COMPACT1; break;
+                case 2:
+                    p = Profile.COMPACT2; break;
+                case 3:
+                    p = Profile.COMPACT3; break;
+                case 4:
+                    p = Profile.FULL_JRE; break;
+                default:
+                    // skip classes with profile=0
+                    // Inner classes are not annotated with the profile annotation
+                    return null;
         }
 
-        Profile p = profiles.get(index);
         String name = cf.getName();
         int i = name.lastIndexOf('/');
-        name = (i > 0) ? name.substring(0, i).replace('/','.') : "";
+            name = (i > 0) ? name.substring(0, i).replace('/', '.') : "";
         if (proprietary) {
             p.proprietaryPkgs.add(name);
         } else {
             p.packages.add(name);
         }
         return p;
     }
 
-    private static void initProfilesFromProperties(List<Profile> profiles, String path)
-            throws IOException
-    {
+        private static void initProfilesFromProperties(String path) throws IOException {
         Properties props = new Properties();
         try (FileReader reader = new FileReader(path)) {
             props.load(reader);
         }
-        int i=1;
-        String key;
-        while (props.containsKey((key = "profile." + i + ".name"))) {
-            Profile profile = new Profile(props.getProperty(key), i);
-            profiles.add(profile);
+            for (Profile prof : Profile.values()) {
+                int i = prof.profile;
+                String key = props.getProperty("profile." + i + ".name");
+                if (key == null) {
+                    throw new RuntimeException(key + " missing in " + path);
+                }
             String n = props.getProperty("profile." + i + ".packages");
             String[] pkgs = n.split("\\s+");
             for (String p : pkgs) {
                 if (p.isEmpty()) continue;
-                profile.packages.add(p);
-            }
-            i++;
-        }
+                    prof.packages.add(p);
     }
-
-    private static class Profile {
-        final String name;
-        final int profile;
-        final Set<String> packages;
-        final Set<String> proprietaryPkgs;
-        Profile(int profile) {
-            this("compact" + profile, profile);
-        }
-        Profile(String name, int profile) {
-            this.name = name;
-            this.profile = profile;
-            this.packages = new HashSet<String>();
-            this.proprietaryPkgs = new HashSet<String>();
         }
-        public String toString() {
-            return name;
         }
     }
 
     // for debugging
     public static void main(String[] args) {
         if (args.length == 0) {
-            Profile[] profiles = new Profile[getProfileCount()];
-            for (Profile p : map.values()) {
-                // move the zeroth profile to the last
-                int index = p.profile == 0 ? profiles.length-1 : p.profile-1;
-                profiles[index] = p;
+            if (Profile.getProfileCount() == 0) {
+                System.err.println("No profile is present in this JDK");
             }
-            for (Profile p : profiles) {
+            for (Profile p : Profile.values()) {
                 String profileName = p.name;
-                SortedSet<String> set = new TreeSet<String>(p.packages);
+                SortedSet<String> set = new TreeSet<>(p.packages);
                 for (String s : set) {
                     // filter out the inner classes that are not annotated with
                     // the profile annotation
-                    if (map.get(s) == p) {
-                        System.out.format("%-10s  %s%n", profileName, s);
+                    if (PackageToProfile.map.get(s) == p) {
+                        System.out.format("%2d: %-10s  %s%n", p.profile, profileName, s);
                         profileName = "";
+                    } else {
+                        System.err.format("Split package: %s in %s and %s %n",
+                            s, PackageToProfile.map.get(s).name, p.name);
                     }
                 }
             }
         }
         for (String pn : args) {
-            System.out.format("%s in %s%n", pn, getProfileName(pn));
+            System.out.format("%s in %s%n", pn, getProfile(pn));
         }
     }
 }