1 /*
2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.tools.sjavac.comp;
27 
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 import javax.lang.model.element.Element;
33 import javax.tools.JavaFileObject;
34 
35 import com.sun.tools.javac.code.Symbol.ClassSymbol;
36 import com.sun.tools.javac.util.Context;
37 import com.sun.tools.javac.util.Log;
38 import com.sun.tools.sjavac.pubapi.PubApi;
39 
40 /**
41  * Utility class containing public API information.
42  *
43  *  <p><b>This is NOT part of any supported API.
44  *  If you write code that depends on this, you do so at your own risk.
45  *  This code and its internal interfaces are subject to change or
46  *  deletion without notice.</b>
47  */
48 public class PubAPIs {
49     protected static final Context.Key<PubAPIs> pubApisKey = new Context.Key<>();
50 
51     // The log to be used for error reporting.
52     protected Log log;
53 
54     // Map from a class name to its public api.
55     // Will the Name encode the module in the future?
56     // If not, this will have to change to map from Module+Name to public api.
57     protected Map<ClassSymbol, PubApi> publicApiPerClass = new HashMap<>();
58 
instance(Context context)59     public static PubAPIs instance(Context context) {
60         PubAPIs instance = context.get(pubApisKey);
61         if (instance == null)
62             instance = new PubAPIs(context);
63         return instance;
64     }
65 
PubAPIs(Context context)66     private PubAPIs(Context context) {
67         context.put(pubApisKey, this);
68         log = Log.instance(context);
69     }
70 
71     /**
72      * Convert the map from class names to their pubapi to a map
73      * from package names to their pubapi.
74      */
getPubapis(Collection<JavaFileObject> explicitJFOs, boolean explicits)75     public Map<String, PubApi> getPubapis(Collection<JavaFileObject> explicitJFOs, boolean explicits) {
76 
77         // Maps ":java.lang" to a package level pub api (with only types on top level)
78         Map<String, PubApi> result = new HashMap<>();
79         for (ClassSymbol cs : publicApiPerClass.keySet()) {
80 
81             boolean amongExplicits = explicitJFOs.contains(cs.sourcefile);
82             if (explicits != amongExplicits)
83                 continue;
84 
85             String pkg = ":" + cs.packge().fullname;
86             PubApi currentPubApi = result.getOrDefault(pkg, new PubApi());
87             result.put(pkg, PubApi.mergeTypes(currentPubApi, publicApiPerClass.get(cs)));
88         }
89 
90         return result;
91     }
92 
93     /**
94      * Visit the api of a class and construct a pubapi and
95      * store it into the pubapi_perclass map.
96      */
97     @SuppressWarnings("deprecation")
visitPubapi(Element e)98     public void visitPubapi(Element e) {
99 
100         // Skip anonymous classes for now
101         if (e == null)
102             return;
103 
104         PubapiVisitor v = new PubapiVisitor();
105         v.visit(e);
106         publicApiPerClass.put((ClassSymbol) e, v.getCollectedPubApi());
107     }
108 }
109