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

Fri, 09 May 2014 20:33:21 -0700

author
mfang
date
Fri, 09 May 2014 20:33:21 -0700
changeset 2388
0add97444be9
parent 2172
aa91bc6e8480
child 2525
2eb010b6cb22
child 2538
1e39ae45d8ac
permissions
-rw-r--r--

8041424: 8u20 l10n resource file translation update 1
Reviewed-by: naoto, yhuang

mchung@1638 1 /*
mchung@1638 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
mchung@1638 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mchung@1638 4 *
mchung@1638 5 * This code is free software; you can redistribute it and/or modify it
mchung@1638 6 * under the terms of the GNU General Public License version 2 only, as
mchung@1638 7 * published by the Free Software Foundation. Oracle designates this
mchung@1638 8 * particular file as subject to the "Classpath" exception as provided
mchung@1638 9 * by Oracle in the LICENSE file that accompanied this code.
mchung@1638 10 *
mchung@1638 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mchung@1638 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mchung@1638 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mchung@1638 14 * version 2 for more details (a copy is included in the LICENSE file that
mchung@1638 15 * accompanied this code).
mchung@1638 16 *
mchung@1638 17 * You should have received a copy of the GNU General Public License version
mchung@1638 18 * 2 along with this work; if not, write to the Free Software Foundation,
mchung@1638 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mchung@1638 20 *
mchung@1638 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mchung@1638 22 * or visit www.oracle.com if you need additional information or have any
mchung@1638 23 * questions.
mchung@1638 24 */
mchung@1638 25 package com.sun.tools.jdeps;
mchung@1638 26
mchung@1638 27 import com.sun.tools.classfile.Annotation;
mchung@1638 28 import com.sun.tools.classfile.Annotation.*;
mchung@1638 29 import com.sun.tools.classfile.Attribute;
mchung@1638 30 import com.sun.tools.classfile.ClassFile;
mchung@1638 31 import com.sun.tools.classfile.ConstantPool.*;
mchung@1638 32 import com.sun.tools.classfile.ConstantPoolException;
mchung@1638 33 import com.sun.tools.classfile.RuntimeAnnotations_attribute;
mchung@1638 34 import java.io.FileReader;
mchung@1638 35 import java.io.IOException;
mchung@2139 36 import java.nio.file.Files;
mchung@1638 37 import java.nio.file.Path;
mchung@1638 38 import java.nio.file.Paths;
mchung@1638 39 import java.util.*;
mchung@1638 40 import java.util.jar.JarFile;
mchung@1638 41
mchung@1638 42 /**
mchung@1638 43 * Build the profile information from ct.sym if exists.
mchung@1638 44 */
mchung@2139 45 enum Profile {
mchung@2139 46
mchung@2139 47 COMPACT1("compact1", 1),
mchung@2139 48 COMPACT2("compact2", 2),
mchung@2139 49 COMPACT3("compact3", 3),
mchung@2139 50 FULL_JRE("Full JRE", 4);
mchung@2139 51
mchung@2139 52 final String name;
mchung@2139 53 final int profile;
mchung@2139 54 final Set<String> packages;
mchung@2139 55 final Set<String> proprietaryPkgs;
mchung@2139 56
mchung@2139 57 Profile(String name, int profile) {
mchung@2139 58 this.name = name;
mchung@2139 59 this.profile = profile;
mchung@2139 60 this.packages = new HashSet<>();
mchung@2139 61 this.proprietaryPkgs = new HashSet<>();
mchung@2139 62 }
mchung@2139 63
mchung@2139 64 @Override
mchung@2139 65 public String toString() {
mchung@2139 66 return name;
mchung@1638 67 }
mchung@1638 68
mchung@1638 69 public static int getProfileCount() {
mchung@2139 70 return PackageToProfile.map.values().size();
mchung@1638 71 }
mchung@1638 72
mchung@2139 73 /**
mchung@2139 74 * Returns the Profile for the given package name. It returns an empty
mchung@2139 75 * string if the given package is not in any profile.
mchung@2139 76 */
mchung@2139 77 public static Profile getProfile(String pn) {
mchung@2139 78 Profile profile = PackageToProfile.map.get(pn);
mchung@2139 79 return (profile != null && profile.packages.contains(pn))
mchung@2139 80 ? profile : null;
mchung@2139 81 }
mchung@2139 82
mchung@2139 83 static class PackageToProfile {
mchung@2172 84 static String[] JAVAX_CRYPTO_PKGS = new String[] {
mchung@2172 85 "javax.crypto",
mchung@2172 86 "javax.crypto.interfaces",
mchung@2172 87 "javax.crypto.spec"
mchung@2172 88 };
mchung@2139 89 static Map<String, Profile> map = initProfiles();
mchung@2139 90 private static Map<String, Profile> initProfiles() {
mchung@2139 91 try {
mchung@2139 92 String profilesProps = System.getProperty("jdeps.profiles");
mchung@2139 93 if (profilesProps != null) {
mchung@2139 94 // for testing for JDK development build where ct.sym doesn't exist
mchung@2139 95 initProfilesFromProperties(profilesProps);
mchung@2139 96 } else {
mchung@2139 97 Path home = Paths.get(System.getProperty("java.home"));
mchung@2139 98 if (home.endsWith("jre")) {
mchung@2139 99 home = home.getParent();
mchung@2139 100 }
mchung@2139 101 Path ctsym = home.resolve("lib").resolve("ct.sym");
mchung@2139 102 if (Files.exists(ctsym)) {
mchung@2139 103 // parse ct.sym and load information about profiles
mchung@2139 104 try (JarFile jf = new JarFile(ctsym.toFile())) {
mchung@2139 105 ClassFileReader reader = ClassFileReader.newInstance(ctsym, jf);
mchung@2139 106 for (ClassFile cf : reader.getClassFiles()) {
mchung@2139 107 findProfile(cf);
mchung@2139 108 }
mchung@1638 109 }
mchung@2172 110 // special case for javax.crypto.* classes that are not
mchung@2172 111 // included in ct.sym since they are in jce.jar
mchung@2172 112 Collections.addAll(Profile.COMPACT1.packages, JAVAX_CRYPTO_PKGS);
mchung@1638 113 }
mchung@2139 114 }
mchung@2139 115 } catch (IOException | ConstantPoolException e) {
mchung@2139 116 throw new Error(e);
mchung@2139 117 }
mchung@2139 118 HashMap<String,Profile> map = new HashMap<>();
mchung@2139 119 for (Profile profile : Profile.values()) {
mchung@2139 120 for (String pn : profile.packages) {
mchung@2139 121 if (!map.containsKey(pn)) {
mchung@2139 122 // split packages in the JRE: use the smaller compact
mchung@2139 123 map.put(pn, profile);
mchung@2139 124 }
mchung@2139 125 }
mchung@2139 126 for (String pn : profile.proprietaryPkgs) {
mchung@2139 127 if (!map.containsKey(pn)) {
mchung@2139 128 map.put(pn, profile);
mchung@1638 129 }
mchung@1638 130 }
mchung@1638 131 }
mchung@2139 132 return map;
mchung@1638 133 }
mchung@2139 134 private static final String PROFILE_ANNOTATION = "Ljdk/Profile+Annotation;";
mchung@2139 135 private static final String PROPRIETARY_ANNOTATION = "Lsun/Proprietary+Annotation;";
mchung@2139 136 private static Profile findProfile(ClassFile cf) throws ConstantPoolException {
mchung@2139 137 RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)
mchung@2139 138 cf.attributes.get(Attribute.RuntimeInvisibleAnnotations);
mchung@2139 139 int index = 0;
mchung@2139 140 boolean proprietary = false;
mchung@2139 141 if (attr != null) {
mchung@2139 142 for (int i = 0; i < attr.annotations.length; i++) {
mchung@2139 143 Annotation ann = attr.annotations[i];
mchung@2139 144 String annType = cf.constant_pool.getUTF8Value(ann.type_index);
mchung@2139 145 if (PROFILE_ANNOTATION.equals(annType)) {
mchung@2139 146 for (int j = 0; j < ann.num_element_value_pairs; j++) {
mchung@2139 147 Annotation.element_value_pair pair = ann.element_value_pairs[j];
mchung@2139 148 Primitive_element_value ev = (Primitive_element_value) pair.value;
mchung@2139 149 CONSTANT_Integer_info info = (CONSTANT_Integer_info)
mchung@2139 150 cf.constant_pool.get(ev.const_value_index);
mchung@2139 151 index = info.value;
mchung@2139 152 break;
mchung@2139 153 }
mchung@2139 154 } else if (PROPRIETARY_ANNOTATION.equals(annType)) {
mchung@2139 155 proprietary = true;
mchung@1638 156 }
mchung@1638 157 }
mchung@1638 158 }
mchung@2139 159
mchung@2139 160 Profile p = null; // default
mchung@2139 161 switch (index) {
mchung@2139 162 case 1:
mchung@2139 163 p = Profile.COMPACT1; break;
mchung@2139 164 case 2:
mchung@2139 165 p = Profile.COMPACT2; break;
mchung@2139 166 case 3:
mchung@2139 167 p = Profile.COMPACT3; break;
mchung@2139 168 case 4:
mchung@2139 169 p = Profile.FULL_JRE; break;
mchung@2139 170 default:
mchung@2139 171 // skip classes with profile=0
mchung@2139 172 // Inner classes are not annotated with the profile annotation
mchung@2139 173 return null;
mchung@2139 174 }
mchung@2139 175
mchung@2139 176 String name = cf.getName();
mchung@2139 177 int i = name.lastIndexOf('/');
mchung@2139 178 name = (i > 0) ? name.substring(0, i).replace('/', '.') : "";
mchung@2139 179 if (proprietary) {
mchung@2139 180 p.proprietaryPkgs.add(name);
mchung@2139 181 } else {
mchung@2139 182 p.packages.add(name);
mchung@2139 183 }
mchung@2139 184 return p;
mchung@2139 185 }
mchung@2139 186
mchung@2139 187 private static void initProfilesFromProperties(String path) throws IOException {
mchung@2139 188 Properties props = new Properties();
mchung@2139 189 try (FileReader reader = new FileReader(path)) {
mchung@2139 190 props.load(reader);
mchung@2139 191 }
mchung@2139 192 for (Profile prof : Profile.values()) {
mchung@2139 193 int i = prof.profile;
mchung@2139 194 String key = props.getProperty("profile." + i + ".name");
mchung@2139 195 if (key == null) {
mchung@2139 196 throw new RuntimeException(key + " missing in " + path);
mchung@2139 197 }
mchung@2139 198 String n = props.getProperty("profile." + i + ".packages");
mchung@2139 199 String[] pkgs = n.split("\\s+");
mchung@2139 200 for (String p : pkgs) {
mchung@2139 201 if (p.isEmpty()) continue;
mchung@2139 202 prof.packages.add(p);
mchung@1638 203 }
mchung@1638 204 }
mchung@1638 205 }
mchung@1638 206 }
mchung@1638 207
mchung@1638 208 // for debugging
mchung@1638 209 public static void main(String[] args) {
mchung@1638 210 if (args.length == 0) {
mchung@2139 211 if (Profile.getProfileCount() == 0) {
mchung@2139 212 System.err.println("No profile is present in this JDK");
mchung@1638 213 }
mchung@2139 214 for (Profile p : Profile.values()) {
mchung@1638 215 String profileName = p.name;
mchung@2139 216 SortedSet<String> set = new TreeSet<>(p.packages);
mchung@1638 217 for (String s : set) {
mchung@1638 218 // filter out the inner classes that are not annotated with
mchung@1638 219 // the profile annotation
mchung@2139 220 if (PackageToProfile.map.get(s) == p) {
mchung@2139 221 System.out.format("%2d: %-10s %s%n", p.profile, profileName, s);
mchung@1638 222 profileName = "";
mchung@2139 223 } else {
mchung@2139 224 System.err.format("Split package: %s in %s and %s %n",
mchung@2139 225 s, PackageToProfile.map.get(s).name, p.name);
mchung@1638 226 }
mchung@1638 227 }
mchung@1638 228 }
mchung@1638 229 }
mchung@1638 230 for (String pn : args) {
mchung@2139 231 System.out.format("%s in %s%n", pn, getProfile(pn));
mchung@1638 232 }
mchung@1638 233 }
mchung@1638 234 }

mercurial