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

Tue, 17 Sep 2013 14:17:13 -0700

author
jjg
date
Tue, 17 Sep 2013 14:17:13 -0700
changeset 2033
fdfbc5f0c4ed
parent 1638
fd3fdaff0257
permissions
-rw-r--r--

8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
Reviewed-by: darcy

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.ConstantPool.*;
mchung@1638 33 import com.sun.tools.classfile.ConstantPoolException;
mchung@1638 34 import com.sun.tools.classfile.RuntimeAnnotations_attribute;
mchung@1638 35 import java.io.File;
mchung@1638 36 import java.io.FileReader;
mchung@1638 37 import java.io.IOException;
mchung@1638 38 import java.nio.file.Path;
mchung@1638 39 import java.nio.file.Paths;
mchung@1638 40 import java.util.*;
mchung@1638 41 import java.util.jar.JarFile;
mchung@1638 42
mchung@1638 43 /**
mchung@1638 44 * Build the profile information from ct.sym if exists.
mchung@1638 45 */
mchung@1638 46 class Profiles {
mchung@1638 47 private static final Map<String,Profile> map = initProfiles();
mchung@1638 48 /**
mchung@1638 49 * Returns the name of the profile for the given package name.
mchung@1638 50 * It returns an empty string if the given package is not in any profile.
mchung@1638 51 */
mchung@1638 52 public static String getProfileName(String pn) {
mchung@1638 53 Profile profile = map.get(pn);
mchung@1638 54 return (profile != null && profile.packages.contains(pn))
mchung@1638 55 ? profile.name : "";
mchung@1638 56 }
mchung@1638 57
mchung@1638 58 public static int getProfileCount() {
mchung@1638 59 return new HashSet<Profile>(map.values()).size();
mchung@1638 60 }
mchung@1638 61
mchung@1638 62 private static Map<String,Profile> initProfiles() {
mchung@1638 63 List<Profile> profiles = new ArrayList<Profile>();
mchung@1638 64 try {
mchung@1638 65 String profilesProps = System.getProperty("jdeps.profiles");
mchung@1638 66 if (profilesProps != null) {
mchung@1638 67 // for testing for JDK development build where ct.sym doesn't exist
mchung@1638 68 initProfilesFromProperties(profiles, profilesProps);
mchung@1638 69 } else {
mchung@1638 70 Path home = Paths.get(System.getProperty("java.home"));
mchung@1638 71 if (home.endsWith("jre")) {
mchung@1638 72 home = home.getParent();
mchung@1638 73 }
mchung@1638 74 Path ctsym = home.resolve("lib").resolve("ct.sym");
mchung@1638 75 if (ctsym.toFile().exists()) {
mchung@1638 76 // add a default Full JRE
mchung@1638 77 profiles.add(0, new Profile("Full JRE", 0));
mchung@1638 78 // parse ct.sym and load information about profiles
mchung@1638 79 try (JarFile jf = new JarFile(ctsym.toFile())) {
mchung@1638 80 ClassFileReader reader = ClassFileReader.newInstance(ctsym, jf);
mchung@1638 81 for (ClassFile cf : reader.getClassFiles()) {
mchung@1638 82 findProfile(profiles, cf);
mchung@1638 83 }
mchung@1638 84 }
mchung@1638 85
mchung@1638 86 // merge the last Profile with the "Full JRE"
mchung@1638 87 if (profiles.size() > 1) {
mchung@1638 88 Profile fullJRE = profiles.get(0);
mchung@1638 89 Profile p = profiles.remove(profiles.size() - 1);
mchung@1638 90 for (String pn : fullJRE.packages) {
mchung@1638 91 // The last profile contains the packages determined from ct.sym.
mchung@1638 92 // Move classes annotated profile==0 or no attribute that are
mchung@1638 93 // added in the fullJRE profile to either supported or proprietary
mchung@1638 94 // packages appropriately
mchung@1638 95 if (p.proprietaryPkgs.contains(pn)) {
mchung@1638 96 p.proprietaryPkgs.add(pn);
mchung@1638 97 } else {
mchung@1638 98 p.packages.add(pn);
mchung@1638 99 }
mchung@1638 100 }
mchung@1638 101 fullJRE.packages.clear();
mchung@1638 102 fullJRE.proprietaryPkgs.clear();
mchung@1638 103 fullJRE.packages.addAll(p.packages);
mchung@1638 104 fullJRE.proprietaryPkgs.addAll(p.proprietaryPkgs);
mchung@1638 105 }
mchung@1638 106 }
mchung@1638 107 }
mchung@1638 108 } catch (IOException | ConstantPoolException e) {
mchung@1638 109 throw new Error(e);
mchung@1638 110 }
mchung@1638 111 HashMap<String,Profile> map = new HashMap<String,Profile>();
mchung@1638 112 for (Profile profile : profiles) {
mchung@1638 113 // Inner classes are not annotated with the profile annotation
mchung@1638 114 // packages may be in one profile but also appear in the Full JRE
mchung@1638 115 // Full JRE is always the first element in profiles list and
mchung@1638 116 // so the map will contain the appropriate Profile
mchung@1638 117 for (String pn : profile.packages) {
mchung@1638 118 map.put(pn, profile);
mchung@1638 119 }
mchung@1638 120 for (String pn : profile.proprietaryPkgs) {
mchung@1638 121 map.put(pn, profile);
mchung@1638 122 }
mchung@1638 123 }
mchung@1638 124 return map;
mchung@1638 125 }
mchung@1638 126
mchung@1638 127 private static final String PROFILE_ANNOTATION = "Ljdk/Profile+Annotation;";
mchung@1638 128 private static final String PROPRIETARY_ANNOTATION = "Lsun/Proprietary+Annotation;";
mchung@1638 129 private static Profile findProfile(List<Profile> profiles, ClassFile cf)
mchung@1638 130 throws ConstantPoolException
mchung@1638 131 {
mchung@1638 132 RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute)
mchung@1638 133 cf.attributes.get(Attribute.RuntimeInvisibleAnnotations);
mchung@1638 134 int index = 0;
mchung@1638 135 boolean proprietary = false;
mchung@1638 136 if (attr != null) {
mchung@1638 137 for (int i = 0; i < attr.annotations.length; i++) {
mchung@1638 138 Annotation ann = attr.annotations[i];
mchung@1638 139 String annType = cf.constant_pool.getUTF8Value(ann.type_index);
mchung@1638 140 if (PROFILE_ANNOTATION.equals(annType)) {
mchung@1638 141 for (int j = 0; j < ann.num_element_value_pairs; j++) {
mchung@1638 142 Annotation.element_value_pair pair = ann.element_value_pairs[j];
mchung@1638 143 Primitive_element_value ev = (Primitive_element_value)pair.value;
mchung@1638 144 CONSTANT_Integer_info info = (CONSTANT_Integer_info)
mchung@1638 145 cf.constant_pool.get(ev.const_value_index);
mchung@1638 146 index = info.value;
mchung@1638 147 break;
mchung@1638 148 }
mchung@1638 149 } else if (PROPRIETARY_ANNOTATION.equals(annType)) {
mchung@1638 150 proprietary = true;
mchung@1638 151 }
mchung@1638 152 }
mchung@1638 153 if (index >= profiles.size()) {
mchung@1638 154 Profile p = null;
mchung@1638 155 for (int i = profiles.size(); i <= index; i++) {
mchung@1638 156 p = new Profile(i);
mchung@1638 157 profiles.add(p);
mchung@1638 158 }
mchung@1638 159 }
mchung@1638 160 }
mchung@1638 161
mchung@1638 162 Profile p = profiles.get(index);
mchung@1638 163 String name = cf.getName();
mchung@1638 164 int i = name.lastIndexOf('/');
mchung@1638 165 name = (i > 0) ? name.substring(0, i).replace('/','.') : "";
mchung@1638 166 if (proprietary) {
mchung@1638 167 p.proprietaryPkgs.add(name);
mchung@1638 168 } else {
mchung@1638 169 p.packages.add(name);
mchung@1638 170 }
mchung@1638 171 return p;
mchung@1638 172 }
mchung@1638 173
mchung@1638 174 private static void initProfilesFromProperties(List<Profile> profiles, String path)
mchung@1638 175 throws IOException
mchung@1638 176 {
mchung@1638 177 Properties props = new Properties();
mchung@1638 178 try (FileReader reader = new FileReader(path)) {
mchung@1638 179 props.load(reader);
mchung@1638 180 }
mchung@1638 181 int i=1;
mchung@1638 182 String key;
mchung@1638 183 while (props.containsKey((key = "profile." + i + ".name"))) {
mchung@1638 184 Profile profile = new Profile(props.getProperty(key), i);
mchung@1638 185 profiles.add(profile);
mchung@1638 186 String n = props.getProperty("profile." + i + ".packages");
mchung@1638 187 String[] pkgs = n.split("\\s+");
mchung@1638 188 for (String p : pkgs) {
mchung@1638 189 if (p.isEmpty()) continue;
mchung@1638 190 profile.packages.add(p);
mchung@1638 191 }
mchung@1638 192 i++;
mchung@1638 193 }
mchung@1638 194 }
mchung@1638 195
mchung@1638 196 private static class Profile {
mchung@1638 197 final String name;
mchung@1638 198 final int profile;
mchung@1638 199 final Set<String> packages;
mchung@1638 200 final Set<String> proprietaryPkgs;
mchung@1638 201 Profile(int profile) {
mchung@1638 202 this("compact" + profile, profile);
mchung@1638 203 }
mchung@1638 204 Profile(String name, int profile) {
mchung@1638 205 this.name = name;
mchung@1638 206 this.profile = profile;
mchung@1638 207 this.packages = new HashSet<String>();
mchung@1638 208 this.proprietaryPkgs = new HashSet<String>();
mchung@1638 209 }
mchung@1638 210 public String toString() {
mchung@1638 211 return name;
mchung@1638 212 }
mchung@1638 213 }
mchung@1638 214
mchung@1638 215 // for debugging
mchung@1638 216 public static void main(String[] args) {
mchung@1638 217 if (args.length == 0) {
mchung@1638 218 Profile[] profiles = new Profile[getProfileCount()];
mchung@1638 219 for (Profile p : map.values()) {
mchung@1638 220 // move the zeroth profile to the last
mchung@1638 221 int index = p.profile == 0 ? profiles.length-1 : p.profile-1;
mchung@1638 222 profiles[index] = p;
mchung@1638 223 }
mchung@1638 224 for (Profile p : profiles) {
mchung@1638 225 String profileName = p.name;
mchung@1638 226 SortedSet<String> set = new TreeSet<String>(p.packages);
mchung@1638 227 for (String s : set) {
mchung@1638 228 // filter out the inner classes that are not annotated with
mchung@1638 229 // the profile annotation
mchung@1638 230 if (map.get(s) == p) {
mchung@1638 231 System.out.format("%-10s %s%n", profileName, s);
mchung@1638 232 profileName = "";
mchung@1638 233 }
mchung@1638 234 }
mchung@1638 235 }
mchung@1638 236 }
mchung@1638 237 for (String pn : args) {
mchung@1638 238 System.out.format("%s in %s%n", pn, getProfileName(pn));
mchung@1638 239 }
mchung@1638 240 }
mchung@1638 241 }

mercurial