APIdia Launch Partner
Top Description Fields Constructors Methods
com.sun.tools.javac.model

public Class JavacTypes

extends Object
implements Types
Class Inheritance
All Implemented Interfaces
javax.lang.model.util.Types
Imports
java.util.Collection, .Collections, .EnumSet, .LinkedHashSet, .List, .Set, java.util.stream.Collectors, javax.lang.model.element.*, javax.lang.model.type.*, com.sun.tools.javac.code.*, com.sun.tools.javac.code.Symbol.*, com.sun.tools.javac.util.*, .DefinedBy.Api

Utility methods for operating on types.

This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.

Field Summary

Modifier and TypeField and Description
private static final Set<TypeKind>
private final Symtab
private final Types

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public Element
asElement(TypeMirror t)

Implements javax.lang.model.util.Types.asElement.

Returns the element corresponding to a type.

public TypeMirror

Returns:

the type of the element as viewed from the containing type
asMemberOf
(DeclaredType
the containing type
containing
,
Element
the element
element
)

Implements javax.lang.model.util.Types.asMemberOf.

Returns the type of an element when that element is viewed as a member of, or otherwise directly contained by, a given type.

public TypeElement
boxedClass(PrimitiveType p)

Implements javax.lang.model.util.Types.boxedClass.

Returns the class of a boxed value of the primitive type argument.

public TypeMirror
capture(TypeMirror t)

Implements javax.lang.model.util.Types.capture.

Applies capture conversion to a type.

private static <T> T
cast(Class<T> clazz, Object o)

Returns an object cast to the specified type.

public boolean
contains(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.contains.

Tests whether one type argument contains another.

public List<Type>
directSupertypes(TypeMirror t)

Implements javax.lang.model.util.Types.directSupertypes.

Returns the direct supertypes of a type.

public TypeMirror
erasure(TypeMirror t)

Implements javax.lang.model.util.Types.erasure.

Returns the erasure of a type.

public ArrayType
getArrayType(TypeMirror componentType)

Implements javax.lang.model.util.Types.getArrayType.

Returns an array type with the specified component type.

public DeclaredType
getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs)

Implements javax.lang.model.util.Types.getDeclaredType.

Returns the type corresponding to a type element and actual type arguments.

public DeclaredType
getDeclaredType(DeclaredType enclosing, TypeElement typeElem, TypeMirror... typeArgs)

Implements javax.lang.model.util.Types.getDeclaredType.

Returns the type corresponding to a type element and actual type arguments, given a containing type of which it is a member.

private DeclaredType
public NoType
getNoType(TypeKind kind)

Implements javax.lang.model.util.Types.getNoType.

Returns a pseudo-type used where no actual type is appropriate.

public NullType
getNullType()

Implements javax.lang.model.util.Types.getNullType.

Returns the null type.

public Set<Symbol.MethodSymbol>
public PrimitiveType
getPrimitiveType(TypeKind kind)

Implements javax.lang.model.util.Types.getPrimitiveType.

Returns a primitive type.

public WildcardType
getWildcardType(TypeMirror extendsBound, TypeMirror superBound)

Implements javax.lang.model.util.Types.getWildcardType.

Returns a new wildcard type.

public static JavacTypes
instance(Context context)

public boolean
isAssignable(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.isAssignable.

Tests whether one type is assignable to another.

public boolean
isSameType(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.isSameType.

Tests whether two TypeMirror objects represent the same type.

public boolean
isSubsignature(ExecutableType m1, ExecutableType m2)

Implements javax.lang.model.util.Types.isSubsignature.

Tests whether the signature of one method is a subsignature of another.

public boolean
isSubtype(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.isSubtype.

Tests whether one type is a subtype of another.

public <T extends TypeMirror> T
stripAnnotations(T t)

Overrides default javax.lang.model.util.Types.stripAnnotations.

Returns a type mirror equivalent to the argument, but with no annotations.

public PrimitiveType
unboxedType(TypeMirror t)

Implements javax.lang.model.util.Types.unboxedType.

Returns the type (a primitive type) of unboxed values of a given type.

private void
validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds)

Throws an IllegalArgumentException if a type's kind is one of a set.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EXEC_OR_PKG_OR_MODback to summary
private static final Set<TypeKind> EXEC_OR_PKG_OR_MOD
symsback to summary
private final Symtab syms
typesback to summary
private final Types types

Constructor Detail

JavacTypesback to summary
protected JavacTypes(Context context)
Annotations
@SuppressWarnings:this-escape

Method Detail

asElementback to summary
public Element asElement(TypeMirror t)

Implements javax.lang.model.util.Types.asElement.

Doc from javax.lang.model.util.Types.asElement.

Returns the element corresponding to a type. The type may be one of:

The method returns null if the type is not one with a corresponding element. Types without corresponding elements include:
Annotations
@DefinedBy:LANGUAGE_MODEL
asMemberOfback to summary
public TypeMirror asMemberOf(DeclaredType containing, Element element)

Implements javax.lang.model.util.Types.asMemberOf.

Returns the type of an element when that element is viewed as a member of, or otherwise directly contained by, a given type. For example, when viewed as a member of the parameterized type Set<String>, the Set.add method is an ExecutableType whose parameter is of type String.

Parameters
containing:DeclaredType

the containing type

element:Element

the element

Returns:TypeMirror

the type of the element as viewed from the containing type

Annotations
@DefinedBy:LANGUAGE_MODEL
Exceptions
IllegalArgumentException:
if the element is not a valid one for the given type
boxedClassback to summary
public TypeElement boxedClass(PrimitiveType p)

Implements javax.lang.model.util.Types.boxedClass.

Doc from javax.lang.model.util.Types.boxedClass.

Returns the class of a boxed value of the primitive type argument. That is, boxing conversion is applied.

Annotations
@DefinedBy:LANGUAGE_MODEL
captureback to summary
public TypeMirror capture(TypeMirror t)

Implements javax.lang.model.util.Types.capture.

Doc from javax.lang.model.util.Types.capture.

Applies capture conversion to a type.

Annotations
@DefinedBy:LANGUAGE_MODEL
castback to summary
private static <T> T cast(Class<T> clazz, Object o)

Returns an object cast to the specified type.

Exceptions
NullPointerException:
if the object is null
IllegalArgumentException:
if the object is of the wrong type
containsback to summary
public boolean contains(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.contains.

Doc from javax.lang.model.util.Types.contains.

Tests whether one type argument contains another.

Annotations
@DefinedBy:LANGUAGE_MODEL
directSupertypesback to summary
public List<Type> directSupertypes(TypeMirror t)

Implements javax.lang.model.util.Types.directSupertypes.

Doc from javax.lang.model.util.Types.directSupertypes.

Returns the direct supertypes of a type. The interface types, if any, will appear last in the list. For an interface type with no direct super-interfaces, a type mirror representing java.lang.Object is returned. The type java.lang.Object has no direct supertype (JLS 8.1.4, 8.1.5) so an empty list is returned for the direct supertypes of a type mirror representing java.lang.Object. Annotations on the direct supertypes are preserved.

Annotations
@DefinedBy:LANGUAGE_MODEL
erasureback to summary
public TypeMirror erasure(TypeMirror t)

Implements javax.lang.model.util.Types.erasure.

Doc from javax.lang.model.util.Types.erasure.

Returns the erasure of a type.

Annotations
@DefinedBy:LANGUAGE_MODEL
getArrayTypeback to summary
public ArrayType getArrayType(TypeMirror componentType)

Implements javax.lang.model.util.Types.getArrayType.

Doc from javax.lang.model.util.Types.getArrayType.

Returns an array type with the specified component type. Annotations on the component type are preserved.

Annotations
@DefinedBy:LANGUAGE_MODEL
getDeclaredTypeback to summary
public DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs)

Implements javax.lang.model.util.Types.getDeclaredType.

Doc from javax.lang.model.util.Types.getDeclaredType.

Returns the type corresponding to a type element and actual type arguments. Given the type element for Set and the type mirror for String, for example, this method may be used to get the parameterized type Set<String>. Annotations on the type arguments are preserved.

The number of type arguments must either equal the number of the type element's formal type parameters, or must be zero. If zero, and if the type element is generic, then the type element's raw type is returned.

If a parameterized type is being returned, its type element must not be contained within a generic outer class. The parameterized type Outer<String>.Inner<Number>, for example, may be constructed by first using this method to get the type Outer<String>, and then invoking getDeclaredType(DeclaredType, TypeElement, TypeMirror...).

Annotations
@DefinedBy:LANGUAGE_MODEL
getDeclaredTypeback to summary
public DeclaredType getDeclaredType(DeclaredType enclosing, TypeElement typeElem, TypeMirror... typeArgs)

Implements javax.lang.model.util.Types.getDeclaredType.

Doc from javax.lang.model.util.Types.getDeclaredType.

Returns the type corresponding to a type element and actual type arguments, given a containing type of which it is a member. The parameterized type Outer<String>.Inner<Number>, for example, may be constructed by first using getDeclaredType(TypeElement, TypeMirror...) to get the type Outer<String>, and then invoking this method. Annotations on the type arguments are preserved.

If the containing type is a parameterized type, the number of type arguments must be equal to the number of typeElem's formal type parameters. If it is not parameterized or if it is null, this method is equivalent to getDeclaredType(typeElem, typeArgs).

Annotations
@DefinedBy:LANGUAGE_MODEL
getDeclaredType0back to summary
private DeclaredType getDeclaredType0(Type outer, Symbol.ClassSymbol sym, TypeMirror... typeArgs)
getNoTypeback to summary
public NoType getNoType(TypeKind kind)

Implements javax.lang.model.util.Types.getNoType.

Doc from javax.lang.model.util.Types.getNoType.

Returns a pseudo-type used where no actual type is appropriate. The kind of type to return may be either VOID or NONE.

To get the pseudo-type corresponding to a package or module, call asType() on the element modeling the package or module. Names can be converted to elements for packages or modules using Elements#getPackageElement(CharSequence) or Elements#getModuleElement(CharSequence), respectively.

Annotations
@DefinedBy:LANGUAGE_MODEL
getNullTypeback to summary
public NullType getNullType()

Implements javax.lang.model.util.Types.getNullType.

Doc from javax.lang.model.util.Types.getNullType.

Returns the null type. This is the type of null.

Annotations
@DefinedBy:LANGUAGE_MODEL
getOverriddenMethodsback to summary
public Set<Symbol.MethodSymbol> getOverriddenMethods(Element elem)
getPrimitiveTypeback to summary
public PrimitiveType getPrimitiveType(TypeKind kind)

Implements javax.lang.model.util.Types.getPrimitiveType.

Doc from javax.lang.model.util.Types.getPrimitiveType.

Returns a primitive type.

Annotations
@DefinedBy:LANGUAGE_MODEL
getWildcardTypeback to summary
public WildcardType getWildcardType(TypeMirror extendsBound, TypeMirror superBound)

Implements javax.lang.model.util.Types.getWildcardType.

Doc from javax.lang.model.util.Types.getWildcardType.

Returns a new wildcard type. Either of the wildcard's bounds may be specified, or neither, but not both. Annotations on the bounds are preserved.

Annotations
@DefinedBy:LANGUAGE_MODEL
instanceback to summary
public static JavacTypes instance(Context context)
isAssignableback to summary
public boolean isAssignable(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.isAssignable.

Doc from javax.lang.model.util.Types.isAssignable.

Tests whether one type is assignable to another.

Annotations
@DefinedBy:LANGUAGE_MODEL
isSameTypeback to summary
public boolean isSameType(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.isSameType.

Doc from javax.lang.model.util.Types.isSameType.

Tests whether two TypeMirror objects represent the same type.

Caveat

if either of the arguments to this method represents a wildcard, this method will return false. As a consequence, a wildcard is not the same type as itself. This might be surprising at first, but makes sense once you consider that an example like this must be rejected by the compiler:

  List<?> list = new ArrayList<Object>();
  list.add(list.get(0));

Since annotations are only meta-data associated with a type, the set of annotations on either argument is not taken into account when computing whether or not two TypeMirror objects are the same type. In particular, two TypeMirror objects can have different annotations and still be considered the same.

Annotations
@DefinedBy:LANGUAGE_MODEL
isSubsignatureback to summary
public boolean isSubsignature(ExecutableType m1, ExecutableType m2)

Implements javax.lang.model.util.Types.isSubsignature.

Doc from javax.lang.model.util.Types.isSubsignature.

Tests whether the signature of one method is a subsignature of another.

Annotations
@DefinedBy:LANGUAGE_MODEL
isSubtypeback to summary
public boolean isSubtype(TypeMirror t1, TypeMirror t2)

Implements javax.lang.model.util.Types.isSubtype.

Doc from javax.lang.model.util.Types.isSubtype.

Tests whether one type is a subtype of another. Any type is considered to be a subtype of itself.

Annotations
@DefinedBy:LANGUAGE_MODEL
stripAnnotationsback to summary
public <T extends TypeMirror> T stripAnnotations(T t)

Overrides default javax.lang.model.util.Types.stripAnnotations.

Doc from javax.lang.model.util.Types.stripAnnotations.

Returns a type mirror equivalent to the argument, but with no annotations. If the type mirror is a composite type, such as an array type or a wildcard type, any constituent types, such as the component type of an array and the type of the bounds of a wildcard type, also have no annotations, recursively.

For most kinds of type mirrors, the result of

types.isSameType(typeMirror, types.stripAnnotations(typeMirror))
types.isSameType(typeMirror, types.stripAnnotations(typeMirror))
is true. The predicate is false on wildcard types for reasons discussed elsewhere.
Annotations
@DefinedBy:LANGUAGE_MODEL
@SuppressWarnings:unchecked
unboxedTypeback to summary
public PrimitiveType unboxedType(TypeMirror t)

Implements javax.lang.model.util.Types.unboxedType.

Doc from javax.lang.model.util.Types.unboxedType.

Returns the type (a primitive type) of unboxed values of a given type. That is, unboxing conversion is applied.

Annotations
@DefinedBy:LANGUAGE_MODEL
validateTypeNotInback to summary
private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds)

Throws an IllegalArgumentException if a type's kind is one of a set.