PubapiVisitor.java revision 2958:27da0c3ac83a
1/*
2 * Copyright (c) 2011, 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
26package com.sun.tools.sjavac.comp;
27
28import static javax.lang.model.element.Modifier.PRIVATE;
29
30import java.util.List;
31import java.util.stream.Collectors;
32
33import javax.lang.model.element.Element;
34import javax.lang.model.element.ExecutableElement;
35import javax.lang.model.element.TypeElement;
36import javax.lang.model.element.TypeParameterElement;
37import javax.lang.model.element.VariableElement;
38import javax.lang.model.type.TypeMirror;
39import javax.lang.model.util.ElementScanner9;
40
41import com.sun.tools.javac.code.Symbol.ClassSymbol;
42import com.sun.tools.javac.util.DefinedBy;
43import com.sun.tools.javac.util.DefinedBy.Api;
44import com.sun.tools.sjavac.pubapi.PubApi;
45import com.sun.tools.sjavac.pubapi.PubApiTypeParam;
46import com.sun.tools.sjavac.pubapi.PubMethod;
47import com.sun.tools.sjavac.pubapi.PubType;
48import com.sun.tools.sjavac.pubapi.PubVar;
49import com.sun.tools.sjavac.pubapi.TypeDesc;
50
51/** Utility class that constructs a textual representation
52 * of the public api of a class.
53 *
54 *  <p><b>This is NOT part of any supported API.
55 *  If you write code that depends on this, you do so at your own risk.
56 *  This code and its internal interfaces are subject to change or
57 *  deletion without notice.</b>
58 */
59public class PubapiVisitor extends ElementScanner9<Void, Void> {
60
61    private PubApi collectedApi = new PubApi();
62
63    private boolean isNonPrivate(Element e) {
64        return !e.getModifiers().contains(PRIVATE);
65    }
66
67    @Override @DefinedBy(Api.LANGUAGE_MODEL)
68    public Void visitType(TypeElement e, Void p) {
69        if (isNonPrivate(e)) {
70            PubApi prevApi = collectedApi;
71            collectedApi = new PubApi();
72            super.visitType(e, p);
73            if (!isAnonymous(e)) {
74                String name = ((ClassSymbol) e).flatname.toString();
75                PubType t = new PubType(e.getModifiers(),
76                                        name,
77                                        //e.getQualifiedName().toString(),
78                                        collectedApi);
79                prevApi.types.put(t.fqName, t);
80            }
81            collectedApi = prevApi;
82        }
83        return null;
84    }
85
86    private boolean isAnonymous(TypeElement e) {
87        return e.getQualifiedName().length() == 0;
88    }
89
90    private static String encodeChar(int c) {
91        return String.format("\\u%04x", c);
92    }
93
94    @Override @DefinedBy(Api.LANGUAGE_MODEL)
95    public Void visitVariable(VariableElement e, Void p) {
96        if (isNonPrivate(e)) {
97            Object constVal = e.getConstantValue();
98            String constValStr = null;
99            // TODO: This doesn't seem to be entirely accurate. What if I change
100            // from, say, 0 to 0L? (And the field is public final static so that
101            // it could get inlined.)
102            if (constVal != null) {
103                if (e.asType().toString().equals("char")) {
104                    // What type is 'value'? Is it already a char?
105                    char c = constVal.toString().charAt(0);
106                    constValStr = "'" + encodeChar(c) + "'";
107                } else {
108                    constValStr = constVal.toString()
109                                          .chars()
110                                          .mapToObj(PubapiVisitor::encodeChar)
111                                          .collect(Collectors.joining("", "\"", "\""));
112                }
113            }
114
115            PubVar v = new PubVar(e.getModifiers(),
116                                  TypeDesc.fromType(e.asType()),
117                                  e.toString(),
118                                  constValStr);
119            collectedApi.variables.put(v.identifier, v);
120        }
121
122        // Safe to not recurse here, because the only thing
123        // to visit here is the constructor of a variable declaration.
124        // If it happens to contain an anonymous inner class (which it might)
125        // then this class is never visible outside of the package anyway, so
126        // we are allowed to ignore it here.
127        return null;
128    }
129
130    @Override @DefinedBy(Api.LANGUAGE_MODEL)
131    public Void visitExecutable(ExecutableElement e, Void p) {
132        if (isNonPrivate(e)) {
133            PubMethod m = new PubMethod(e.getModifiers(),
134                                        getTypeParameters(e.getTypeParameters()),
135                                        TypeDesc.fromType(e.getReturnType()),
136                                        e.getSimpleName().toString(),
137                                        getTypeDescs(getParamTypes(e)),
138                                        getTypeDescs(e.getThrownTypes()));
139            collectedApi.methods.put(m.asSignatureString(), m);
140        }
141        return null;
142    }
143
144    private List<PubApiTypeParam> getTypeParameters(List<? extends TypeParameterElement> elements) {
145        return elements.stream()
146                       .map(e -> new PubApiTypeParam(e.getSimpleName().toString(), getTypeDescs(e.getBounds())))
147                       .collect(Collectors.toList());
148    }
149
150    private List<TypeMirror> getParamTypes(ExecutableElement e) {
151        return e.getParameters()
152                .stream()
153                .map(VariableElement::asType)
154                .collect(Collectors.toList());
155    }
156
157    private List<TypeDesc> getTypeDescs(List<? extends TypeMirror> list) {
158        return list.stream()
159                   .map(TypeDesc::fromType)
160                   .collect(Collectors.toList());
161    }
162
163    public PubApi getCollectedPubApi() {
164        return collectedApi;
165    }
166}
167