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.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 }