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