|
1 /* |
|
2 * Copyright (c) 2011, 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.Iterator; |
|
29 import java.util.List; |
|
30 import javax.lang.model.element.Modifier; |
|
31 import javax.lang.model.element.ExecutableElement; |
|
32 import javax.lang.model.element.TypeElement; |
|
33 import javax.lang.model.element.VariableElement; |
|
34 import javax.lang.model.type.TypeMirror; |
|
35 import javax.lang.model.util.ElementScanner6; |
|
36 |
|
37 /** Utility class that constructs a textual representation |
|
38 * of the public api of a class. |
|
39 * |
|
40 * <p><b>This is NOT part of any supported API. |
|
41 * If you write code that depends on this, you do so at your own |
|
42 * risk. This code and its internal interfaces are subject to change |
|
43 * or deletion without notice.</b></p> |
|
44 */ |
|
45 public class PubapiVisitor extends ElementScanner6<Void, Void> { |
|
46 |
|
47 StringBuffer sb; |
|
48 // Important that it is 1! Part of protocol over wire, silly yes. |
|
49 // Fix please. |
|
50 int indent = 1; |
|
51 |
|
52 public PubapiVisitor(StringBuffer sb) { |
|
53 this.sb = sb; |
|
54 } |
|
55 |
|
56 String depth(int l) { |
|
57 return " ".substring(0, l); |
|
58 } |
|
59 |
|
60 @Override |
|
61 public Void visitType(TypeElement e, Void p) { |
|
62 if (e.getModifiers().contains(Modifier.PUBLIC) |
|
63 || e.getModifiers().contains(Modifier.PROTECTED)) |
|
64 { |
|
65 sb.append(depth(indent) + "TYPE " + e.getQualifiedName() + "\n"); |
|
66 indent += 2; |
|
67 Void v = super.visitType(e, p); |
|
68 indent -= 2; |
|
69 return v; |
|
70 } |
|
71 return null; |
|
72 } |
|
73 |
|
74 @Override |
|
75 public Void visitVariable(VariableElement e, Void p) { |
|
76 if (e.getModifiers().contains(Modifier.PUBLIC) |
|
77 || e.getModifiers().contains(Modifier.PROTECTED)) { |
|
78 sb.append(depth(indent)).append("VAR ") |
|
79 .append(makeVariableString(e)).append("\n"); |
|
80 } |
|
81 // Safe to not recurse here, because the only thing |
|
82 // to visit here is the constructor of a variable declaration. |
|
83 // If it happens to contain an anonymous inner class (which it might) |
|
84 // then this class is never visible outside of the package anyway, so |
|
85 // we are allowed to ignore it here. |
|
86 return null; |
|
87 } |
|
88 |
|
89 @Override |
|
90 public Void visitExecutable(ExecutableElement e, Void p) { |
|
91 if (e.getModifiers().contains(Modifier.PUBLIC) |
|
92 || e.getModifiers().contains(Modifier.PROTECTED)) { |
|
93 sb.append(depth(indent)).append("METHOD ") |
|
94 .append(makeMethodString(e)).append("\n"); |
|
95 } |
|
96 return null; |
|
97 } |
|
98 |
|
99 /** |
|
100 * Creates a String representation of a method element with everything |
|
101 * necessary to track all public aspects of it in an API. |
|
102 * @param e Element to create String for. |
|
103 * @return String representation of element. |
|
104 */ |
|
105 protected String makeMethodString(ExecutableElement e) { |
|
106 StringBuilder result = new StringBuilder(); |
|
107 for (Modifier modifier : e.getModifiers()) { |
|
108 result.append(modifier.toString()); |
|
109 result.append(" "); |
|
110 } |
|
111 result.append(e.getReturnType().toString()); |
|
112 result.append(" "); |
|
113 result.append(e.toString()); |
|
114 |
|
115 List<? extends TypeMirror> thrownTypes = e.getThrownTypes(); |
|
116 if (!thrownTypes.isEmpty()) { |
|
117 result.append(" throws "); |
|
118 for (Iterator<? extends TypeMirror> iterator = thrownTypes |
|
119 .iterator(); iterator.hasNext();) { |
|
120 TypeMirror typeMirror = iterator.next(); |
|
121 result.append(typeMirror.toString()); |
|
122 if (iterator.hasNext()) { |
|
123 result.append(", "); |
|
124 } |
|
125 } |
|
126 } |
|
127 return result.toString(); |
|
128 } |
|
129 |
|
130 /** |
|
131 * Creates a String representation of a variable element with everything |
|
132 * necessary to track all public aspects of it in an API. |
|
133 * @param e Element to create String for. |
|
134 * @return String representation of element. |
|
135 */ |
|
136 protected String makeVariableString(VariableElement e) { |
|
137 StringBuilder result = new StringBuilder(); |
|
138 for (Modifier modifier : e.getModifiers()) { |
|
139 result.append(modifier.toString()); |
|
140 result.append(" "); |
|
141 } |
|
142 result.append(e.asType().toString()); |
|
143 result.append(" "); |
|
144 result.append(e.toString()); |
|
145 Object value = e.getConstantValue(); |
|
146 if (value != null) { |
|
147 result.append(" = "); |
|
148 if (e.asType().toString().equals("char")) { |
|
149 int v = (int)value.toString().charAt(0); |
|
150 result.append("'\\u"+Integer.toString(v,16)+"'"); |
|
151 } else { |
|
152 result.append(value.toString()); |
|
153 } |
|
154 } |
|
155 return result.toString(); |
|
156 } |
|
157 } |