public class TypeArgInferenceUtil extends Object
Constructor and Description |
---|
TypeArgInferenceUtil() |
Modifier and Type | Method and Description |
---|---|
static AnnotatedTypeMirror |
assignedTo(AnnotatedTypeFactory atypeFactory,
TreePath path)
Returns the annotated type that the leaf of path is assigned to, if it is within an
assignment context.
|
static AnnotatedTypeMirror |
assignedToVariable(AnnotatedTypeFactory atypeFactory,
Tree assignmentContext)
If the variable's type is a type variable, return getAnnotatedTypeLhsNoTypeVarDefault(tree).
|
static AnnotationMirrorMap<AnnotationMirror> |
createHierarchyMap(AnnotationMirrorSet annos,
QualifierHierarchy qualifierHierarchy)
Take a set of annotations and separate them into a mapping of (
hierarchy top ⇒
annotations in hierarchy ) |
static List<? extends ExpressionTree> |
expressionToArgTrees(ExpressionTree expression)
Takes an expression tree that must be either a MethodInovcationTree or a NewClassTree
(constructor invocation) and returns the arguments to its formal parameters.
|
static boolean |
isATarget(AnnotatedTypeMirror type,
Set<TypeVariable> targetTypeVars)
Given a set of type variables for which we are inferring a type, returns true if type is a
use of a type variable in the list of targetTypeVars.
|
static AnnotatedTypeMirror |
leastUpperBound(AnnotatedTypeFactory typeFactory,
Iterable<AnnotatedTypeMirror> types)
Successively calls least upper bound on the elements of types.
|
static Set<TypeVariable> |
methodTypeToTargets(AnnotatedTypeMirror.AnnotatedExecutableType methodType)
Given an AnnotatedExecutableType return a set of type variables that represents the generic
type parameters of that method
|
static AnnotatedTypeMirror |
substitute(Map<TypeVariable,AnnotatedTypeMirror> substitutions,
AnnotatedTypeMirror toModify)
Create a copy of toModify.
|
static AnnotatedTypeMirror |
substitute(TypeVariable typeVariable,
AnnotatedTypeMirror substitution,
AnnotatedTypeMirror toModify)
Replace all uses of typeVariable with substitution in a copy of toModify using the normal
substitution rules, (@see TypeVariableSubstitutor).Return the copy
|
static List<AnnotatedTypeMirror> |
treesToTypes(List<? extends ExpressionTree> argTrees,
AnnotatedTypeFactory typeFactory)
Calls get annotated types on a List of trees using the given type factory.
|
public static List<? extends ExpressionTree> expressionToArgTrees(ExpressionTree expression)
expression
- a MethodInvocationTree or a NewClassTreepublic static List<AnnotatedTypeMirror> treesToTypes(List<? extends ExpressionTree> argTrees, AnnotatedTypeFactory typeFactory)
public static boolean isATarget(AnnotatedTypeMirror type, Set<TypeVariable> targetTypeVars)
public static Set<TypeVariable> methodTypeToTargets(AnnotatedTypeMirror.AnnotatedExecutableType methodType)
public static AnnotatedTypeMirror assignedTo(AnnotatedTypeFactory atypeFactory, TreePath path)
public static AnnotatedTypeMirror assignedToVariable(AnnotatedTypeFactory atypeFactory, Tree assignmentContext)
For example:
<S> S bar () {...}
<T> T foo(T p) {
T local = bar();
return local;
}
During type argument inference of bar
, the assignment context is local
. If
the local variable default is used, then the type of assignment context type is
@Nullable T
and the type argument inferred for bar()
is @Nullable T
.
And an incompatible types in return error is issued.
If instead, the local variable default is not applied, then the assignment context type is
T
(with lower bound @NonNull Void
and upper bound @Nullable Object
)
and the type argument inferred for bar()
is T
. During dataflow, the type of
local
is refined to T
and the return is legal.
If the assignment context type was a declared type, for example:
<S> S bar () {...}
Object foo() {
Object local = bar();
return local;
}
The local variable default must be used or else the assignment context type is missing an
annotation. So, an incompatible types in return error is issued in the above code. We could
improve type argument inference in this case and by using the lower bound of S
instead of the local variable default.atypeFactory
- AnnotatedTypeFactoryassignmentContext
- VariableTreepublic static AnnotationMirrorMap<AnnotationMirror> createHierarchyMap(AnnotationMirrorSet annos, QualifierHierarchy qualifierHierarchy)
hierarchy top ⇒
annotations in hierarchy
)public static AnnotatedTypeMirror substitute(TypeVariable typeVariable, AnnotatedTypeMirror substitution, AnnotatedTypeMirror toModify)
public static AnnotatedTypeMirror substitute(Map<TypeVariable,AnnotatedTypeMirror> substitutions, AnnotatedTypeMirror toModify)
typeVariable ⇒ annotated
type
replace uses of typeVariable with the corresponding annotated type using normal
substitution rules (@see TypeVariableSubstitutor) Return the copypublic static AnnotatedTypeMirror leastUpperBound(AnnotatedTypeFactory typeFactory, Iterable<AnnotatedTypeMirror> types)