jjg@1569: /*
jjg@1569: * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1569: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1569: *
jjg@1569: * This code is free software; you can redistribute it and/or modify it
jjg@1569: * under the terms of the GNU General Public License version 2 only, as
jjg@1569: * published by the Free Software Foundation. Oracle designates this
jjg@1569: * particular file as subject to the "Classpath" exception as provided
jjg@1569: * by Oracle in the LICENSE file that accompanied this code.
jjg@1569: *
jjg@1569: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1569: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1569: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1569: * version 2 for more details (a copy is included in the LICENSE file that
jjg@1569: * accompanied this code).
jjg@1569: *
jjg@1569: * You should have received a copy of the GNU General Public License version
jjg@1569: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1569: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1569: *
jjg@1569: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1569: * or visit www.oracle.com if you need additional information or have any
jjg@1569: * questions.
jjg@1569: */
jjg@1569: package com.sun.tools.javac.sym;
jjg@1569:
jjg@1569: import java.io.BufferedInputStream;
jjg@1569: import java.io.BufferedWriter;
jjg@1569: import java.io.File;
jjg@1569: import java.io.FileInputStream;
jjg@1569: import java.io.FileWriter;
jjg@1569: import java.io.IOException;
jjg@1569: import java.nio.charset.Charset;
jjg@1569: import java.nio.file.Files;
jjg@1569: import java.util.HashMap;
jjg@1569: import java.util.Map;
jjg@1569: import java.util.Properties;
jjg@1569: import java.util.Set;
jjg@1569: import java.util.TreeMap;
jjg@1569: import java.util.TreeSet;
jjg@1569:
jjg@1569: import com.sun.tools.javac.util.Assert;
jjg@1569:
jjg@1569: /**
jjg@1569: * Provide details about profile contents.
jjg@1569: *
jjg@1569: *
This is NOT part of any supported API.
jjg@1569: * If you write code that depends on this, you do so at your own
jjg@1569: * risk. This code and its internal interfaces are subject to change
jjg@1569: * or deletion without notice.
jjg@1569: */
jjg@1569: public abstract class Profiles {
jjg@1569: // for debugging
jjg@1569: public static void main(String[] args) throws IOException {
jjg@1569: Profiles p = Profiles.read(new File(args[0]));
jjg@1569: if (args.length >= 2) {
jjg@1569: Map> lists = new TreeMap>();
jjg@1569: for (int i = 1; i <= 4; i++)
jjg@1569: lists.put(i, new TreeSet());
jjg@1569:
jjg@1569: File rt_jar_lst = new File(args[1]);
jjg@1569: for (String line: Files.readAllLines(rt_jar_lst.toPath(), Charset.defaultCharset())) {
jjg@1569: if (line.endsWith(".class")) {
jjg@1569: String type = line.substring(0, line.length() - 6);
jjg@1569: int profile = p.getProfile(type);
jjg@1569: for (int i = profile; i <= 4; i++)
jjg@1569: lists.get(i).add(type);
jjg@1569: }
jjg@1569: }
jjg@1569:
jjg@1569: for (int i = 1; i <= 4; i++) {
jjg@1569: BufferedWriter out = new BufferedWriter(new FileWriter(i + ".txt"));
jjg@1569: try {
jjg@1569: for (String type: lists.get(i)) {
jjg@1569: out.write(type);
jjg@1569: out.newLine();
jjg@1569: }
jjg@1569: } finally {
jjg@1569: out.close();
jjg@1569: }
jjg@1569: }
jjg@1569: }
jjg@1569: }
jjg@1569:
jjg@1569: public static Profiles read(File file) throws IOException {
jjg@1569: BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
jjg@1569: try {
jjg@1569: Properties p = new Properties();
jjg@1569: p.load(in);
jjg@1569: if (p.containsKey("java/lang/Object"))
jjg@1569: return new SimpleProfiles(p);
jjg@1569: else
jjg@1569: return new MakefileProfiles(p);
jjg@1569: } finally {
jjg@1569: in.close();
jjg@1569: }
jjg@1569: }
jjg@1569:
jjg@1569: public abstract int getProfileCount();
jjg@1569:
jjg@1569: public abstract int getProfile(String typeName);
jjg@1569:
jjg@1569: public abstract Set getPackages(int profile);
jjg@1569:
jjg@1569: private static class MakefileProfiles extends Profiles {
jjg@1569: static class Package {
jjg@1569: final Package parent;
jjg@1569: final String name;
jjg@1569:
jjg@1569: Map subpackages = new TreeMap();
jjg@1569:
jjg@1569: int profile;
jjg@1569: Map includedTypes = new TreeMap();
jjg@1569: Map excludedTypes = new TreeMap();
jjg@1569:
jjg@1569: Package(Package parent, String name) {
jjg@1569: this.parent = parent;
jjg@1569: this.name = name;
jjg@1569: }
jjg@1569:
jjg@1569: int getProfile() {
jjg@1569: return (parent == null) ? profile : Math.max(parent.getProfile(), profile);
jjg@1569: }
jjg@1569:
jjg@1569: int getProfile(String simpleTypeName) {
jjg@1569: Integer i;
jjg@1569: if ((i = includedTypes.get(simpleTypeName)) != null)
jjg@1569: return i;
jjg@1569: if ((i = includedTypes.get("*")) != null)
jjg@1569: return i;
jjg@1569: if ((i = excludedTypes.get(simpleTypeName)) != null)
jjg@1569: return i + 1;
jjg@1569: if ((i = excludedTypes.get("*")) != null)
jjg@1569: return i + 1;
jjg@1569: return getProfile();
jjg@1569: }
jjg@1569:
jjg@1569: String getName() {
jjg@1569: return (parent == null) ? name : (parent.getName() + "/" + name);
jjg@1569: }
jjg@1569:
jjg@1569: void getPackages(int profile, Set results) {
jjg@1569: int prf = getProfile();
jjg@1569: if (prf != 0 && profile >= prf)
jjg@1569: results.add(getName());
jjg@1569: for (Package pkg: subpackages.values())
jjg@1569: pkg.getPackages(profile, results);
jjg@1569: }
jjg@1569: }
jjg@1569:
jjg@1569: final static Map packages = new TreeMap();
dholmes@1636:
dholmes@1636: final int maxProfile = 4; // Three compact profiles plus full JRE
jjg@1569:
jjg@1569: MakefileProfiles(Properties p) {
dholmes@1636: for (int profile = 1; profile <= maxProfile; profile++) {
dholmes@1636: String prefix = (profile < maxProfile ? "PROFILE_" + profile : "FULL_JRE");
dholmes@1636: String inclPackages = p.getProperty(prefix + "_RTJAR_INCLUDE_PACKAGES");
jjg@1569: if (inclPackages == null)
jjg@1569: break;
jjg@1569: for (String pkg: inclPackages.substring(1).trim().split("\\s+")) {
jjg@1569: if (pkg.endsWith("/"))
jjg@1569: pkg = pkg.substring(0, pkg.length() - 1);
jjg@1569: includePackage(profile, pkg);
jjg@1569: }
dholmes@1636: String inclTypes = p.getProperty(prefix + "_RTJAR_INCLUDE_TYPES");
jjg@1569: if (inclTypes != null) {
jjg@1569: for (String type: inclTypes.replace("$$", "$").split("\\s+")) {
jjg@1569: if (type.endsWith(".class"))
jjg@1569: includeType(profile, type.substring(0, type.length() - 6));
jjg@1569: }
jjg@1569: }
dholmes@1636: String exclTypes = p.getProperty(prefix + "_RTJAR_EXCLUDE_TYPES");
jjg@1569: if (exclTypes != null) {
jjg@1569: for (String type: exclTypes.replace("$$", "$").split("\\s+")) {
jjg@1569: if (type.endsWith(".class"))
jjg@1569: excludeType(profile, type.substring(0, type.length() - 6));
jjg@1569: }
jjg@1569: }
jjg@1569: }
jjg@1569: }
jjg@1569:
jjg@1569: @Override
jjg@1569: public int getProfileCount() {
jjg@1569: return maxProfile;
jjg@1569: }
jjg@1569:
jjg@1569: @Override
jjg@1569: public int getProfile(String typeName) {
jjg@1569: int sep = typeName.lastIndexOf("/");
jjg@1569: String packageName = typeName.substring(0, sep);
jjg@1569: String simpleName = typeName.substring(sep + 1);
jjg@1569:
jjg@1569: Package p = getPackage(packageName);
jjg@1569: return p.getProfile(simpleName);
jjg@1569: }
jjg@1569:
jjg@1569: @Override
jjg@1569: public Set getPackages(int profile) {
jjg@1569: Set results = new TreeSet();
jjg@1569: for (Package p: packages.values())
jjg@1569: p.getPackages(profile, results);
jjg@1569: return results;
jjg@1569: }
jjg@1569:
jjg@1569: private void includePackage(int profile, String packageName) {
jjg@1569: // System.err.println("include package " + packageName);
jjg@1569: Package p = getPackage(packageName);
jjg@1569: Assert.check(p.profile == 0);
jjg@1569: p.profile = profile;
jjg@1569: }
jjg@1569:
jjg@1569: private void includeType(int profile, String typeName) {
jjg@1569: // System.err.println("include type " + typeName);
jjg@1569: int sep = typeName.lastIndexOf("/");
jjg@1569: String packageName = typeName.substring(0, sep);
jjg@1569: String simpleName = typeName.substring(sep + 1);
jjg@1569:
jjg@1569: Package p = getPackage(packageName);
jjg@1569: Assert.check(!p.includedTypes.containsKey(simpleName));
jjg@1569: p.includedTypes.put(simpleName, profile);
jjg@1569: }
jjg@1569:
jjg@1569: private void excludeType(int profile, String typeName) {
jjg@1569: // System.err.println("exclude type " + typeName);
jjg@1569: int sep = typeName.lastIndexOf("/");
jjg@1569: String packageName = typeName.substring(0, sep);
jjg@1569: String simpleName = typeName.substring(sep + 1);
jjg@1569:
jjg@1569: Package p = getPackage(packageName);
jjg@1569: Assert.check(!p.excludedTypes.containsKey(simpleName));
jjg@1569: p.excludedTypes.put(simpleName, profile);
jjg@1569: }
jjg@1569:
jjg@1569: private Package getPackage(String packageName) {
jjg@1569: int sep = packageName.lastIndexOf("/");
jjg@1569: Package parent;
jjg@1569: Map parentSubpackages;
jjg@1569: String simpleName;
jjg@1569: if (sep == -1) {
jjg@1569: parent = null;
jjg@1569: parentSubpackages = packages;
jjg@1569: simpleName = packageName;
jjg@1569: } else {
jjg@1569: parent = getPackage(packageName.substring(0, sep));
jjg@1569: parentSubpackages = parent.subpackages;
jjg@1569: simpleName = packageName.substring(sep + 1);
jjg@1569: }
jjg@1569:
jjg@1569: Package p = parentSubpackages.get(simpleName);
jjg@1569: if (p == null) {
jjg@1569: parentSubpackages.put(simpleName, p = new Package(parent, simpleName));
jjg@1569: }
jjg@1569: return p;
jjg@1569: }
jjg@1569: }
jjg@1569:
jjg@1569: private static class SimpleProfiles extends Profiles {
jjg@1569: private final Map map;
jjg@1569: private final int profileCount;
jjg@1569:
jjg@1569: SimpleProfiles(Properties p) {
jjg@1569: int max = 0;
jjg@1569: map = new HashMap();
jjg@1569: for (Map.Entry