< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java

Print this page

257             return getDeclaredType(typeElem, typeArgs);
258 
259         return getDeclaredType0(outer, sym, typeArgs);
260     }
261     // where
262         private DeclaredType getDeclaredType0(Type outer,
263                                               ClassSymbol sym,
264                                               TypeMirror... typeArgs) {
265             if (typeArgs.length != sym.type.getTypeArguments().length())
266                 throw new IllegalArgumentException(
267                 "Incorrect number of type arguments");
268 
269             ListBuffer<Type> targs = new ListBuffer<>();
270             for (TypeMirror t : typeArgs) {
271                 if (!(t instanceof ReferenceType || t instanceof WildcardType))
272                     throw new IllegalArgumentException(t.toString());
273                 targs.append((Type) t);
274             }
275             // TODO: Would like a way to check that type args match formals.
276 
277             return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym);
278         }
279 
280     /**
281      * Returns the type of an element when that element is viewed as
282      * a member of, or otherwise directly contained by, a given type.
283      * For example,
284      * when viewed as a member of the parameterized type {@code Set<String>},
285      * the {@code Set.add} method is an {@code ExecutableType}
286      * whose parameter is of type {@code String}.
287      *
288      * @param containing  the containing type
289      * @param element     the element
290      * @return the type of the element as viewed from the containing type
291      * @throws IllegalArgumentException if the element is not a valid one
292      *          for the given type
293      */
294     @DefinedBy(Api.LANGUAGE_MODEL)
295     public TypeMirror asMemberOf(DeclaredType containing, Element element) {
296         Type site = (Type)containing;
297         Symbol sym = (Symbol)element;
298         if (types.asSuper(site, sym.getEnclosingElement()) == null)
299             throw new IllegalArgumentException(sym + "@" + site);
300         return types.memberType(site, sym);
301     }
302 
303 
304     private static final Set<TypeKind> EXEC_OR_PKG_OR_MOD =
305         EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE, TypeKind.MODULE);
306 
307     /**
308      * Throws an IllegalArgumentException if a type's kind is one of a set.
309      */
310     private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
311         if (invalidKinds.contains(t.getKind()))
312             throw new IllegalArgumentException(t.toString());
313     }
314 
315     /**
316      * Returns an object cast to the specified type.
317      * @throws NullPointerException if the object is {@code null}
318      * @throws IllegalArgumentException if the object is of the wrong type

257             return getDeclaredType(typeElem, typeArgs);
258 
259         return getDeclaredType0(outer, sym, typeArgs);
260     }
261     // where
262         private DeclaredType getDeclaredType0(Type outer,
263                                               ClassSymbol sym,
264                                               TypeMirror... typeArgs) {
265             if (typeArgs.length != sym.type.getTypeArguments().length())
266                 throw new IllegalArgumentException(
267                 "Incorrect number of type arguments");
268 
269             ListBuffer<Type> targs = new ListBuffer<>();
270             for (TypeMirror t : typeArgs) {
271                 if (!(t instanceof ReferenceType || t instanceof WildcardType))
272                     throw new IllegalArgumentException(t.toString());
273                 targs.append((Type) t);
274             }
275             // TODO: Would like a way to check that type args match formals.
276 
277             return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym, TypeMetadata.EMPTY, sym.type.getFlavor());
278         }
279 
280     /**
281      * Returns the type of an element when that element is viewed as
282      * a member of, or otherwise directly contained by, a given type.
283      * For example,
284      * when viewed as a member of the parameterized type {@code Set<String>},
285      * the {@code Set.add} method is an {@code ExecutableType}
286      * whose parameter is of type {@code String}.
287      *
288      * @param containing  the containing type
289      * @param element     the element
290      * @return the type of the element as viewed from the containing type
291      * @throws IllegalArgumentException if the element is not a valid one
292      *          for the given type
293      */
294     @DefinedBy(Api.LANGUAGE_MODEL)
295     public TypeMirror asMemberOf(DeclaredType containing, Element element) {
296         Type site = (Type)containing;
297         Symbol sym = (Symbol)element;
298         if (types.asSuper(site.referenceProjectionOrSelf(), sym.getEnclosingElement()) == null)
299             throw new IllegalArgumentException(sym + "@" + site);
300         return types.memberType(site, sym);
301     }
302 
303 
304     private static final Set<TypeKind> EXEC_OR_PKG_OR_MOD =
305         EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE, TypeKind.MODULE);
306 
307     /**
308      * Throws an IllegalArgumentException if a type's kind is one of a set.
309      */
310     private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
311         if (invalidKinds.contains(t.getKind()))
312             throw new IllegalArgumentException(t.toString());
313     }
314 
315     /**
316      * Returns an object cast to the specified type.
317      * @throws NullPointerException if the object is {@code null}
318      * @throws IllegalArgumentException if the object is of the wrong type
< prev index next >