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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial