Lines 43-50
Link Here
|
43 |
*/ |
43 |
*/ |
44 |
package org.netbeans.api.java.source; |
44 |
package org.netbeans.api.java.source; |
45 |
|
45 |
|
|
|
46 |
import com.sun.tools.javac.code.Flags; |
46 |
import com.sun.tools.javac.code.Type; |
47 |
import com.sun.tools.javac.code.Type; |
|
|
48 |
import com.sun.tools.javac.code.Type.CapturedType; |
49 |
import com.sun.tools.javac.code.Type.ClassType; |
50 |
import com.sun.tools.javac.code.Type.TypeVar; |
51 |
import com.sun.tools.javac.code.TypeTag; |
47 |
import com.sun.tools.javac.code.Types; |
52 |
import com.sun.tools.javac.code.Types; |
|
|
53 |
import com.sun.tools.javac.model.JavacTypes; |
48 |
import java.util.Arrays; |
54 |
import java.util.Arrays; |
49 |
import java.util.EnumSet; |
55 |
import java.util.EnumSet; |
50 |
import java.util.Iterator; |
56 |
import java.util.Iterator; |
Lines 163-168
Link Here
|
163 |
return new TypeNameVisitor(opt.contains(TypeNameOptions.PRINT_AS_VARARG)).visit(type, opt.contains(TypeNameOptions.PRINT_FQN)).toString(); |
169 |
return new TypeNameVisitor(opt.contains(TypeNameOptions.PRINT_AS_VARARG)).visit(type, opt.contains(TypeNameOptions.PRINT_FQN)).toString(); |
164 |
} |
170 |
} |
165 |
|
171 |
|
|
|
172 |
/** |
173 |
* Returns a TypeMirror which can be represented in a source as a type declarator. The returned TypeMirror, |
174 |
* if not erroneous, can be used as type of a variable or a method's return type. The method will attempt to |
175 |
* infer proper wildcards or bounds. |
176 |
* <p/> |
177 |
* If the type could be represented in source, the method returns a type of {@link TypeKind#ERROR}. |
178 |
* |
179 |
* @param type the type to be polished |
180 |
* @return the representable type or an error type |
181 |
* @since 2.16 |
182 |
*/ |
183 |
public TypeMirror getDenotableType(TypeMirror type) { |
184 |
Types types = Types.instance(info.impl.getJavacTask().getContext()); |
185 |
if (type == null) { |
186 |
return types.createErrorType( |
187 |
(Type)JavacTypes.instance(info.impl.getJavacTask().getContext()).getNoType(TypeKind.NONE) |
188 |
); |
189 |
} |
190 |
Type inType = (Type)type; |
191 |
TypeKind tk = type.getKind(); |
192 |
if (tk == TypeKind.ERROR) { |
193 |
inType = (Type)info.getTrees().getOriginalType((ErrorType)type); |
194 |
} else if (tk == TypeKind.NONE || tk == TypeKind.OTHER) { |
195 |
return types.createErrorType(inType); |
196 |
} |
197 |
Type t = types.upward(inType, types.captures(inType)); |
198 |
if (!t.isErroneous()) { |
199 |
if (!checkDenotable(t)) { |
200 |
return types.createErrorType(t); |
201 |
} |
202 |
} |
203 |
if (t.hasTag(TypeTag.BOT)) { |
204 |
return types.createErrorType(t); |
205 |
} else { |
206 |
return t; |
207 |
} |
208 |
} |
209 |
|
210 |
boolean checkDenotable(Type t) { |
211 |
return denotableChecker.visit(t, null); |
212 |
} |
213 |
// where |
214 |
|
215 |
/** diamondTypeChecker: A type visitor that descends down the given type looking for non-denotable |
216 |
* types. The visit methods return false as soon as a non-denotable type is encountered and true |
217 |
* otherwise. |
218 |
*/ |
219 |
private static final Types.SimpleVisitor<Boolean, Void> denotableChecker = new Types.SimpleVisitor<Boolean, Void>() { |
220 |
@Override |
221 |
public Boolean visitType(Type t, Void s) { |
222 |
return true; |
223 |
} |
224 |
@Override |
225 |
public Boolean visitClassType(ClassType t, Void s) { |
226 |
if (t.isUnion() || t.isIntersection()) { |
227 |
return false; |
228 |
} |
229 |
for (Type targ : t.allparams()) { |
230 |
if (!visit(targ, s)) { |
231 |
return false; |
232 |
} |
233 |
} |
234 |
return true; |
235 |
} |
236 |
|
237 |
@Override |
238 |
public Boolean visitTypeVar(TypeVar t, Void s) { |
239 |
/* Any type variable mentioned in the inferred type must have been declared as a type parameter |
240 |
(i.e cannot have been produced by inference (18.4)) |
241 |
*/ |
242 |
return (t.tsym.flags() & Flags.SYNTHETIC) == 0; |
243 |
} |
244 |
|
245 |
@Override |
246 |
public Boolean visitCapturedType(CapturedType t, Void s) { |
247 |
/* Any type variable mentioned in the inferred type must have been declared as a type parameter |
248 |
(i.e cannot have been produced by capture conversion (5.1.10)) |
249 |
*/ |
250 |
return false; |
251 |
} |
252 |
|
253 |
|
254 |
@Override |
255 |
public Boolean visitArrayType(Type.ArrayType t, Void s) { |
256 |
return visit(t.elemtype, s); |
257 |
} |
258 |
|
259 |
@Override |
260 |
public Boolean visitWildcardType(Type.WildcardType t, Void s) { |
261 |
return visit(t.type, s); |
262 |
} |
263 |
}; |
264 |
|
166 |
/**Options for the {@link #getTypeName(javax.lang.model.type.TypeMirror, org.netbeans.api.java.source.TypeUtilities.TypeNameOptions[]) } method. |
265 |
/**Options for the {@link #getTypeName(javax.lang.model.type.TypeMirror, org.netbeans.api.java.source.TypeUtilities.TypeNameOptions[]) } method. |
167 |
* @since 0.62 |
266 |
* @since 0.62 |
168 |
*/ |
267 |
*/ |