Lines 27-33
Link Here
|
27 |
* Contributor(s): |
27 |
* Contributor(s): |
28 |
* |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2010 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
33 |
* If you wish your version of this file to be governed by only the CDDL |
Lines 45-53
Link Here
|
45 |
|
45 |
|
46 |
import com.sun.tools.javac.code.Type; |
46 |
import com.sun.tools.javac.code.Type; |
47 |
import com.sun.tools.javac.code.Types; |
47 |
import com.sun.tools.javac.code.Types; |
|
|
48 |
import java.util.Arrays; |
49 |
import java.util.EnumSet; |
50 |
import java.util.Iterator; |
48 |
import java.util.List; |
51 |
import java.util.List; |
|
|
52 |
import java.util.Set; |
53 |
import javax.lang.model.element.Element; |
54 |
import javax.lang.model.element.TypeElement; |
55 |
import javax.lang.model.type.ArrayType; |
56 |
import javax.lang.model.type.DeclaredType; |
57 |
import javax.lang.model.type.ErrorType; |
49 |
import javax.lang.model.type.TypeKind; |
58 |
import javax.lang.model.type.TypeKind; |
50 |
import javax.lang.model.type.TypeMirror; |
59 |
import javax.lang.model.type.TypeMirror; |
|
|
60 |
import javax.lang.model.type.TypeVariable; |
61 |
import javax.lang.model.type.WildcardType; |
62 |
import javax.lang.model.util.SimpleTypeVisitor6; |
63 |
import org.netbeans.api.annotations.common.CheckReturnValue; |
64 |
import org.netbeans.api.annotations.common.NonNull; |
65 |
import org.netbeans.api.annotations.common.NullAllowed; |
51 |
|
66 |
|
52 |
/**Various utilities related to the {@link TypeMirror}s. |
67 |
/**Various utilities related to the {@link TypeMirror}s. |
53 |
* |
68 |
* |
Lines 114-117
Link Here
|
114 |
return Types.instance(info.impl.getJavacTask().getContext()).subst((Type)type, l1, l2); |
129 |
return Types.instance(info.impl.getJavacTask().getContext()).subst((Type)type, l1, l2); |
115 |
} |
130 |
} |
116 |
|
131 |
|
|
|
132 |
/**Get textual representation of the given type. |
133 |
* |
134 |
* @param type to print |
135 |
* @param options allows to specify various adjustments to the output text |
136 |
* @return textual representation of the given type |
137 |
* @since 0.62 |
138 |
*/ |
139 |
public @NonNull @CheckReturnValue CharSequence getTypeName(@NullAllowed TypeMirror type, @NonNull TypeNameOptions... options) { |
140 |
if (type == null) |
141 |
return ""; //NOI18N |
142 |
Set<TypeNameOptions> opt = EnumSet.noneOf(TypeNameOptions.class); |
143 |
opt.addAll(Arrays.asList(options)); |
144 |
return new TypeNameVisitor(opt.contains(TypeNameOptions.PRINT_AS_VARARG)).visit(type, opt.contains(TypeNameOptions.PRINT_FQN)).toString(); |
145 |
} |
146 |
|
147 |
/**Options for the {@link #getTypeName(javax.lang.model.type.TypeMirror, org.netbeans.api.java.source.TypeUtilities.TypeNameOptions[]) } method. |
148 |
* @since 0.62 |
149 |
*/ |
150 |
public enum TypeNameOptions { |
151 |
/** |
152 |
* Print declared types as fully qualified names. |
153 |
*/ |
154 |
PRINT_FQN, |
155 |
/** |
156 |
* Print "..." instead of "[]". |
157 |
*/ |
158 |
PRINT_AS_VARARG; |
159 |
} |
160 |
|
161 |
private static final String UNKNOWN = "<unknown>"; //NOI18N |
162 |
private static final String CAPTURED_WILDCARD = "<captured wildcard>"; //NOI18N |
163 |
private static class TypeNameVisitor extends SimpleTypeVisitor6<StringBuilder,Boolean> { |
164 |
|
165 |
private boolean varArg; |
166 |
private boolean insideCapturedWildcard = false; |
167 |
|
168 |
private TypeNameVisitor(boolean varArg) { |
169 |
super(new StringBuilder()); |
170 |
this.varArg = varArg; |
171 |
} |
172 |
|
173 |
@Override |
174 |
public StringBuilder defaultAction(TypeMirror t, Boolean p) { |
175 |
return DEFAULT_VALUE.append(t); |
176 |
} |
177 |
|
178 |
@Override |
179 |
public StringBuilder visitDeclared(DeclaredType t, Boolean p) { |
180 |
Element e = t.asElement(); |
181 |
if (e instanceof TypeElement) { |
182 |
TypeElement te = (TypeElement)e; |
183 |
DEFAULT_VALUE.append((p ? te.getQualifiedName() : te.getSimpleName()).toString()); |
184 |
Iterator<? extends TypeMirror> it = t.getTypeArguments().iterator(); |
185 |
if (it.hasNext()) { |
186 |
DEFAULT_VALUE.append("<"); //NOI18N |
187 |
while(it.hasNext()) { |
188 |
visit(it.next(), p); |
189 |
if (it.hasNext()) |
190 |
DEFAULT_VALUE.append(", "); //NOI18N |
191 |
} |
192 |
DEFAULT_VALUE.append(">"); //NOI18N |
193 |
} |
194 |
return DEFAULT_VALUE; |
195 |
} else { |
196 |
return DEFAULT_VALUE.append(UNKNOWN); //NOI18N |
197 |
} |
198 |
} |
199 |
|
200 |
@Override |
201 |
public StringBuilder visitArray(ArrayType t, Boolean p) { |
202 |
boolean isVarArg = varArg; |
203 |
varArg = false; |
204 |
visit(t.getComponentType(), p); |
205 |
return DEFAULT_VALUE.append(isVarArg ? "..." : "[]"); //NOI18N |
206 |
} |
207 |
|
208 |
@Override |
209 |
public StringBuilder visitTypeVariable(TypeVariable t, Boolean p) { |
210 |
Element e = t.asElement(); |
211 |
if (e != null) { |
212 |
String name = e.getSimpleName().toString(); |
213 |
if (!CAPTURED_WILDCARD.equals(name)) |
214 |
return DEFAULT_VALUE.append(name); |
215 |
} |
216 |
DEFAULT_VALUE.append("?"); //NOI18N |
217 |
if (!insideCapturedWildcard) { |
218 |
insideCapturedWildcard = true; |
219 |
TypeMirror bound = t.getLowerBound(); |
220 |
if (bound != null && bound.getKind() != TypeKind.NULL) { |
221 |
DEFAULT_VALUE.append(" super "); //NOI18N |
222 |
visit(bound, p); |
223 |
} else { |
224 |
bound = t.getUpperBound(); |
225 |
if (bound != null && bound.getKind() != TypeKind.NULL) { |
226 |
DEFAULT_VALUE.append(" extends "); //NOI18N |
227 |
if (bound.getKind() == TypeKind.TYPEVAR) |
228 |
bound = ((TypeVariable)bound).getLowerBound(); |
229 |
visit(bound, p); |
230 |
} |
231 |
} |
232 |
insideCapturedWildcard = false; |
233 |
} |
234 |
return DEFAULT_VALUE; |
235 |
} |
236 |
|
237 |
@Override |
238 |
public StringBuilder visitWildcard(WildcardType t, Boolean p) { |
239 |
int len = DEFAULT_VALUE.length(); |
240 |
DEFAULT_VALUE.append("?"); //NOI18N |
241 |
TypeMirror bound = t.getSuperBound(); |
242 |
if (bound == null) { |
243 |
bound = t.getExtendsBound(); |
244 |
if (bound != null) { |
245 |
DEFAULT_VALUE.append(" extends "); //NOI18N |
246 |
if (bound.getKind() == TypeKind.WILDCARD) |
247 |
bound = ((WildcardType)bound).getSuperBound(); |
248 |
visit(bound, p); |
249 |
} else if (len == 0) { |
250 |
bound = SourceUtils.getBound(t); |
251 |
if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N |
252 |
DEFAULT_VALUE.append(" extends "); //NOI18N |
253 |
visit(bound, p); |
254 |
} |
255 |
} |
256 |
} else { |
257 |
DEFAULT_VALUE.append(" super "); //NOI18N |
258 |
visit(bound, p); |
259 |
} |
260 |
return DEFAULT_VALUE; |
261 |
} |
262 |
|
263 |
@Override |
264 |
public StringBuilder visitError(ErrorType t, Boolean p) { |
265 |
Element e = t.asElement(); |
266 |
if (e instanceof TypeElement) { |
267 |
TypeElement te = (TypeElement)e; |
268 |
return DEFAULT_VALUE.append((p ? te.getQualifiedName() : te.getSimpleName()).toString()); |
269 |
} |
270 |
return DEFAULT_VALUE; |
271 |
} |
272 |
} |
117 |
} |
273 |
} |