ohrstrom@1504: /*
ohrstrom@1504: * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504: *
ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504: *
ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504: * accompanied this code).
ohrstrom@1504: *
ohrstrom@1504: * You should have received a copy of the GNU General Public License version
ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504: *
ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504: * questions.
ohrstrom@1504: */
ohrstrom@1504:
ohrstrom@1504: package com.sun.tools.sjavac.comp;
ohrstrom@1504:
ohrstrom@1504: import javax.lang.model.element.Element;
ohrstrom@1504: import java.util.Arrays;
ohrstrom@1504: import java.util.Comparator;
ohrstrom@1504: import java.util.HashMap;
ohrstrom@1504: import java.util.HashSet;
ohrstrom@1504: import java.util.Map;
ohrstrom@1504: import java.util.Set;
ohrstrom@1504:
ohrstrom@1504: import com.sun.tools.javac.code.Symbol.ClassSymbol;
ohrstrom@1504: import com.sun.tools.javac.util.Context;
ohrstrom@1504: import com.sun.tools.javac.util.Log;
ohrstrom@1504: import com.sun.tools.javac.util.Name;
ohrstrom@1504:
ohrstrom@1504: /** Utility class containing dependency information between packages
ohrstrom@1504: * and the pubapi for a package.
ohrstrom@1504: *
ohrstrom@1504: *
This is NOT part of any supported API.
ohrstrom@1504: * If you write code that depends on this, you do so at your own
ohrstrom@1504: * risk. This code and its internal interfaces are subject to change
ohrstrom@1504: * or deletion without notice.
ohrstrom@1504: */
ohrstrom@1504: public class Dependencies {
ohrstrom@1504: protected static final Context.Key dependenciesKey =
ohrstrom@1504: new Context.Key();
ohrstrom@1504:
ohrstrom@1504: // The log to be used for error reporting.
ohrstrom@1504: protected Log log;
ohrstrom@1504: // Map from package name to packages that the package depends upon.
ohrstrom@1504: protected Map> deps;
ohrstrom@1504: // This is the set of all packages that are supplied
ohrstrom@1504: // through the java files at the command line.
ohrstrom@1504: protected Set explicitPackages;
ohrstrom@1504:
ohrstrom@1504: // Map from a package name to its public api.
ohrstrom@1504: // Will the Name encode the module in the future?
ohrstrom@1504: // If not, this will have to change to map from Module+Name to public api.
ohrstrom@1504: protected Map publicApiPerClass;
ohrstrom@1504:
ohrstrom@1504: public static Dependencies instance(Context context) {
ohrstrom@1504: Dependencies instance = context.get(dependenciesKey);
ohrstrom@1504: if (instance == null)
ohrstrom@1504: instance = new Dependencies(context);
ohrstrom@1504: return instance;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: private Dependencies(Context context) {
ohrstrom@1504: context.put(dependenciesKey, this);
ohrstrom@1504: log = Log.instance(context);
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: public void reset()
ohrstrom@1504: {
ohrstrom@1504: deps = new HashMap>();
ohrstrom@1504: explicitPackages = new HashSet();
ohrstrom@1504: publicApiPerClass = new HashMap();
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Fetch the set of dependencies that are relevant to the compile
ohrstrom@1504: * that has just been performed. I.e. we are only interested in
ohrstrom@1504: * dependencies for classes that were explicitly compiled.
ohrstrom@1504: * @return
ohrstrom@1504: */
ohrstrom@1504: public Map> getDependencies() {
ohrstrom@1504: Map> new_deps = new HashMap>();
ohrstrom@1504: if (explicitPackages == null) return new_deps;
ohrstrom@1504: for (Name pkg : explicitPackages) {
ohrstrom@1504: Set set = deps.get(pkg);
ohrstrom@1504: if (set != null) {
ohrstrom@1504: Set new_set = new_deps.get(pkg.toString());
ohrstrom@1504: if (new_set == null) {
ohrstrom@1504: new_set = new HashSet();
ohrstrom@1504: // Modules beware....
ohrstrom@1504: new_deps.put(":"+pkg.toString(), new_set);
ohrstrom@1504: }
ohrstrom@1504: for (Name d : set) {
ohrstrom@1504: new_set.add(":"+d.toString());
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: return new_deps;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: class CompareNames implements Comparator {
ohrstrom@1504: public int compare(Name a, Name b) {
ohrstrom@1504: return a.toString().compareTo(b.toString());
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: public boolean equals(Object obj) {
ohrstrom@1504: return super.equals(obj);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Convert the map from class names to their pubapi to a map
ohrstrom@1504: * from package names to their pubapi (which is the sorted concatenation
ohrstrom@1504: * of all the class pubapis)
ohrstrom@1504: */
ohrstrom@1504: public Map getPubapis() {
ohrstrom@1504: Map publicApiPerPackage = new HashMap();
ohrstrom@1504: if (publicApiPerClass == null) return publicApiPerPackage;
ohrstrom@1504: Name[] keys = publicApiPerClass.keySet().toArray(new Name[0]);
ohrstrom@1504: Arrays.sort(keys, new CompareNames());
ohrstrom@1504: StringBuffer newPublicApi = new StringBuffer();
ohrstrom@1504: int i=0;
ohrstrom@1504: String prevPkg = "";
ohrstrom@1504: for (Name k : keys) {
ohrstrom@1504: String cn = k.toString();
ohrstrom@1504: String pn = "";
ohrstrom@1504: int dp = cn.lastIndexOf('.');
ohrstrom@1504: if (dp != -1) {
ohrstrom@1504: pn = cn.substring(0,dp);
ohrstrom@1504: }
ohrstrom@1504: if (!pn.equals(prevPkg)) {
ohrstrom@1504: if (!prevPkg.equals("")) {
ohrstrom@1504: // Add default module name ":"
ohrstrom@1504: publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
ohrstrom@1504: }
ohrstrom@1504: newPublicApi = new StringBuffer();
ohrstrom@1504: prevPkg = pn;
ohrstrom@1504: }
ohrstrom@1504: newPublicApi.append(publicApiPerClass.get(k));
ohrstrom@1504: i++;
ohrstrom@1504: }
ohrstrom@1504: if (!prevPkg.equals(""))
ohrstrom@1504: publicApiPerPackage.put(":"+prevPkg, newPublicApi.toString());
ohrstrom@1504: return publicApiPerPackage;
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Visit the api of a class and construct a pubapi string and
ohrstrom@1504: * store it into the pubapi_perclass map.
ohrstrom@1504: */
ohrstrom@1504: public void visitPubapi(Element e) {
ohrstrom@1504: Name n = ((ClassSymbol)e).fullname;
ohrstrom@1504: Name p = ((ClassSymbol)e).packge().fullname;
ohrstrom@1504: StringBuffer sb = publicApiPerClass.get(n);
ohrstrom@1504: assert(sb == null);
ohrstrom@1504: sb = new StringBuffer();
ohrstrom@1504: PubapiVisitor v = new PubapiVisitor(sb);
ohrstrom@1504: v.visit(e);
ohrstrom@1504: if (sb.length()>0) {
ohrstrom@1504: publicApiPerClass.put(n, sb);
ohrstrom@1504: }
ohrstrom@1504: explicitPackages.add(p);
ohrstrom@1504: }
ohrstrom@1504:
ohrstrom@1504: /**
ohrstrom@1504: * Collect a dependency. curr_pkg is marked as depending on dep_pkg.
ohrstrom@1504: */
ohrstrom@1504: public void collect(Name currPkg, Name depPkg) {
ohrstrom@1504: if (!currPkg.equals(depPkg)) {
ohrstrom@1504: Set theset = deps.get(currPkg);
ohrstrom@1504: if (theset==null) {
ohrstrom@1504: theset = new HashSet();
ohrstrom@1504: deps.put(currPkg, theset);
ohrstrom@1504: }
ohrstrom@1504: theset.add(depPkg);
ohrstrom@1504: }
ohrstrom@1504: }
ohrstrom@1504: }