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

Fri, 18 Jul 2014 10:43:41 -0700

author
mchung
date
Fri, 18 Jul 2014 10:43:41 -0700
changeset 2539
a51b7fd0543b
parent 2538
1e39ae45d8ac
child 2702
9ca8d8713094
permissions
-rw-r--r--

8050804: (jdeps) Recommend supported API to replace use of JDK internal API
Reviewed-by: dfuchs

mchung@1638 1 /*
mchung@2538 2 * Copyright (c) 2013, 2014, 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 COMPACT1("compact1", 1),
mchung@2139 47 COMPACT2("compact2", 2),
mchung@2139 48 COMPACT3("compact3", 3),
mchung@2139 49 FULL_JRE("Full JRE", 4);
mchung@2139 50
mchung@2139 51 final String name;
mchung@2139 52 final int profile;
mchung@2139 53 final Set<String> packages;
mchung@2139 54 final Set<String> proprietaryPkgs;
mchung@2139 55
mchung@2139 56 Profile(String name, int profile) {
mchung@2139 57 this.name = name;
mchung@2139 58 this.profile = profile;
mchung@2139 59 this.packages = new HashSet<>();
mchung@2139 60 this.proprietaryPkgs = new HashSet<>();
mchung@2139 61 }
mchung@2139 62
mchung@2538 63 public String profileName() {
mchung@2139 64 return name;
mchung@1638 65 }
mchung@1638 66
mchung@1638 67 public static int getProfileCount() {
mchung@2139 68 return PackageToProfile.map.values().size();
mchung@1638 69 }
mchung@1638 70
mchung@2139 71 /**
mchung@2139 72 * Returns the Profile for the given package name. It returns an empty
mchung@2139 73 * string if the given package is not in any profile.
mchung@2139 74 */
mchung@2139 75 public static Profile getProfile(String pn) {
mchung@2139 76 Profile profile = PackageToProfile.map.get(pn);
mchung@2139 77 return (profile != null && profile.packages.contains(pn))
mchung@2538 78 ? profile : null;
mchung@2139 79 }
mchung@2139 80
mchung@2139 81 static class PackageToProfile {
mchung@2172 82 static String[] JAVAX_CRYPTO_PKGS = new String[] {
mchung@2172 83 "javax.crypto",
mchung@2172 84 "javax.crypto.interfaces",
mchung@2172 85 "javax.crypto.spec"
mchung@2172 86 };
mchung@2139 87 static Map<String, Profile> map = initProfiles();
mchung@2139 88 private static Map<String, Profile> initProfiles() {
mchung@2139 89 try {
mchung@2139 90 String profilesProps = System.getProperty("jdeps.profiles");
mchung@2139 91 if (profilesProps != null) {
mchung@2139 92 // for testing for JDK development build where ct.sym doesn't exist
mchung@2139 93 initProfilesFromProperties(profilesProps);
mchung@2139 94 } else {
mchung@2139 95 Path home = Paths.get(System.getProperty("java.home"));
mchung@2139 96 if (home.endsWith("jre")) {
mchung@2139 97 home = home.getParent();
mchung@2139 98 }
mchung@2139 99 Path ctsym = home.resolve("lib").resolve("ct.sym");
mchung@2139 100 if (Files.exists(ctsym)) {
mchung@2139 101 // parse ct.sym and load information about profiles
mchung@2139 102 try (JarFile jf = new JarFile(ctsym.toFile())) {
mchung@2139 103 ClassFileReader reader = ClassFileReader.newInstance(ctsym, jf);
mchung@2139 104 for (ClassFile cf : reader.getClassFiles()) {
mchung@2139 105 findProfile(cf);
mchung@2139 106 }
mchung@1638 107 }
mchung@2172 108 // special case for javax.crypto.* classes that are not
mchung@2172 109 // included in ct.sym since they are in jce.jar
mchung@2172 110 Collections.addAll(Profile.COMPACT1.packages, JAVAX_CRYPTO_PKGS);
mchung@1638 111 }
mchung@2139 112 }
mchung@2139 113 } catch (IOException | ConstantPoolException e) {
mchung@2139 114 throw new Error(e);
mchung@2139 115 }
mchung@2139 116 HashMap<String,Profile> map = new HashMap<>();
mchung@2139 117 for (Profile profile : Profile.values()) {
mchung@2139 118 for (String pn : profile.packages) {
mchung@2139 119 if (!map.containsKey(pn)) {
mchung@2139 120 // split packages in the JRE: use the smaller compact
mchung@2139 121 map.put(pn, profile);
mchung@2139 122 }
mchung@2139 123 }
mchung@2139 124 for (String pn : profile.proprietaryPkgs) {
mchung@2139 125 if (!map.containsKey(pn)) {
mchung@2139 126 map.put(pn, profile);
mchung@1638 127 }
mchung@1638 128 }
mchung@1638 129 }
mchung@2139 130 return map;
mchung@1638 131 }
mchung@2139 132 private static final String PROFILE_ANNOTATION = "Ljdk/Profile+Annotation;";
mchung@2139 133 private static final String PROPRIETARY_ANNOTATION = "Lsun/Proprietary+Annotation;";
mchung@2139 134 private static Profile findProfile(ClassFile cf) throws ConstantPoolException {
mchung@2139 135 RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)
mchung@2139 136 cf.attributes.get(Attribute.RuntimeInvisibleAnnotations);
mchung@2139 137 int index = 0;
mchung@2139 138 boolean proprietary = false;
mchung@2139 139 if (attr != null) {
mchung@2139 140 for (int i = 0; i < attr.annotations.length; i++) {
mchung@2139 141 Annotation ann = attr.annotations[i];
mchung@2139 142 String annType = cf.constant_pool.getUTF8Value(ann.type_index);
mchung@2139 143 if (PROFILE_ANNOTATION.equals(annType)) {
mchung@2139 144 for (int j = 0; j < ann.num_element_value_pairs; j++) {
mchung@2139 145 Annotation.element_value_pair pair = ann.element_value_pairs[j];
mchung@2139 146 Primitive_element_value ev = (Primitive_element_value) pair.value;
mchung@2139 147 CONSTANT_Integer_info info = (CONSTANT_Integer_info)
mchung@2139 148 cf.constant_pool.get(ev.const_value_index);
mchung@2139 149 index = info.value;
mchung@2139 150 break;
mchung@2139 151 }
mchung@2139 152 } else if (PROPRIETARY_ANNOTATION.equals(annType)) {
mchung@2139 153 proprietary = true;
mchung@1638 154 }
mchung@1638 155 }
mchung@1638 156 }
mchung@2139 157
mchung@2139 158 Profile p = null; // default
mchung@2139 159 switch (index) {
mchung@2139 160 case 1:
mchung@2139 161 p = Profile.COMPACT1; break;
mchung@2139 162 case 2:
mchung@2139 163 p = Profile.COMPACT2; break;
mchung@2139 164 case 3:
mchung@2139 165 p = Profile.COMPACT3; break;
mchung@2139 166 case 4:
mchung@2139 167 p = Profile.FULL_JRE; break;
mchung@2139 168 default:
mchung@2139 169 // skip classes with profile=0
mchung@2139 170 // Inner classes are not annotated with the profile annotation
mchung@2139 171 return null;
mchung@2139 172 }
mchung@2139 173
mchung@2139 174 String name = cf.getName();
mchung@2139 175 int i = name.lastIndexOf('/');
mchung@2139 176 name = (i > 0) ? name.substring(0, i).replace('/', '.') : "";
mchung@2139 177 if (proprietary) {
mchung@2139 178 p.proprietaryPkgs.add(name);
mchung@2139 179 } else {
mchung@2139 180 p.packages.add(name);
mchung@2139 181 }
mchung@2139 182 return p;
mchung@2139 183 }
mchung@2139 184
mchung@2139 185 private static void initProfilesFromProperties(String path) throws IOException {
mchung@2139 186 Properties props = new Properties();
mchung@2139 187 try (FileReader reader = new FileReader(path)) {
mchung@2139 188 props.load(reader);
mchung@2139 189 }
mchung@2139 190 for (Profile prof : Profile.values()) {
mchung@2139 191 int i = prof.profile;
mchung@2139 192 String key = props.getProperty("profile." + i + ".name");
mchung@2139 193 if (key == null) {
mchung@2139 194 throw new RuntimeException(key + " missing in " + path);
mchung@2139 195 }
mchung@2139 196 String n = props.getProperty("profile." + i + ".packages");
mchung@2139 197 String[] pkgs = n.split("\\s+");
mchung@2139 198 for (String p : pkgs) {
mchung@2139 199 if (p.isEmpty()) continue;
mchung@2139 200 prof.packages.add(p);
mchung@1638 201 }
mchung@1638 202 }
mchung@1638 203 }
mchung@1638 204 }
mchung@1638 205
mchung@1638 206 // for debugging
mchung@1638 207 public static void main(String[] args) {
mchung@1638 208 if (args.length == 0) {
mchung@2139 209 if (Profile.getProfileCount() == 0) {
mchung@2139 210 System.err.println("No profile is present in this JDK");
mchung@1638 211 }
mchung@2139 212 for (Profile p : Profile.values()) {
mchung@1638 213 String profileName = p.name;
mchung@2139 214 SortedSet<String> set = new TreeSet<>(p.packages);
mchung@1638 215 for (String s : set) {
mchung@1638 216 // filter out the inner classes that are not annotated with
mchung@1638 217 // the profile annotation
mchung@2139 218 if (PackageToProfile.map.get(s) == p) {
mchung@2139 219 System.out.format("%2d: %-10s %s%n", p.profile, profileName, s);
mchung@1638 220 profileName = "";
mchung@2139 221 } else {
mchung@2139 222 System.err.format("Split package: %s in %s and %s %n",
mchung@2139 223 s, PackageToProfile.map.get(s).name, p.name);
mchung@1638 224 }
mchung@1638 225 }
mchung@1638 226 }
mchung@1638 227 }
mchung@1638 228 for (String pn : args) {
mchung@2139 229 System.out.format("%s in %s%n", pn, getProfile(pn));
mchung@1638 230 }
mchung@1638 231 }
mchung@1638 232 }

mercurial