public class DefaultTypeArgumentInference extends Object implements TypeArgumentInference
Note, there are some deviations JLS 7 for the following cases:
<T> T get()
<S> void set(S s)
set(get())
Presumably, we want to detect these situations and combine the set of constraints with
T <: S
.
Constructor and Description |
---|
DefaultTypeArgumentInference() |
Modifier and Type | Method and Description |
---|---|
void |
adaptMethodType(AnnotatedTypeFactory typeFactory,
ExpressionTree invocation,
AnnotatedTypeMirror.AnnotatedExecutableType methodType) |
void |
addConstraintsBetweenTargets(Set<TUConstraint> constraints,
Set<TypeVariable> targets,
boolean asSubtype,
AnnotatedTypeFactory typeFactory)
Declarations of the form:
<A, B extends A> implies a TUConstraint of B <: A . |
AnnotatedTypeMirror.AnnotatedTypeVariable |
addOrGetDeclarations(TypeVariable target,
AnnotatedTypeFactory typeFactory,
Map<TypeVariable,AnnotatedTypeMirror.AnnotatedTypeVariable> declarations) |
protected Set<TUConstraint> |
afToTuConstraints(Set<? extends AFConstraint> afConstraints,
Set<TypeVariable> targets)
Convert AFConstraints to TUConstraints
|
protected Set<AFConstraint> |
createArgumentAFConstraints(AnnotatedTypeFactory typeFactory,
List<AnnotatedTypeMirror> argTypes,
AnnotatedTypeMirror.AnnotatedExecutableType methodType,
Set<TypeVariable> targets,
boolean useNullArguments)
Step 1: Create a constraint
Ai << Fi for each Argument(Ai) to formal parameter(Fi). |
ConstraintMap |
createAssignmentConstraints(AnnotatedTypeMirror assignedTo,
AnnotatedTypeMirror boxedReturnType,
AnnotatedTypeMirror.AnnotatedExecutableType methodType,
Set<AFConstraint> afArgumentConstraints,
Map<TypeVariable,AnnotatedTypeMirror> inferredArgs,
Set<TypeVariable> targets,
AnnotatedTypeFactory typeFactory)
The first half of Step 6.
|
protected Set<FIsA> |
createInitialAssignmentConstraints(AnnotatedTypeMirror assignedTo,
AnnotatedTypeMirror boxedReturnType,
AnnotatedTypeFactory typeFactory,
Set<TypeVariable> targets)
Create a set of constraints between return type and any type to which it is assigned.
|
protected List<AnnotatedTypeMirror> |
getArgumentTypes(ExpressionTree expression,
AnnotatedTypeFactory typeFactory) |
protected AnnotatedTypeMirror |
getAssignedTo(ExpressionTree expression,
AnnotatedTypeFactory typeFactory) |
Map<TypeVariable,AnnotatedTypeMirror> |
inferTypeArgs(AnnotatedTypeFactory typeFactory,
ExpressionTree expressionTree,
ExecutableElement methodElem,
AnnotatedTypeMirror.AnnotatedExecutableType methodType)
Infer the type arguments for the method or constructor invocation given by invocation.
|
protected void |
reduceAfConstraints(AnnotatedTypeFactory typeFactory,
Set<AFConstraint> outgoing,
Queue<AFConstraint> toProcess,
Set<TypeVariable> targets)
Given a set of AFConstraints, remove all constraints that are not relevant to inference and
return a set of AFConstraints in which the F is a use of one of the type parameters to infer.
|
public Map<TypeVariable,AnnotatedTypeMirror> inferTypeArgs(AnnotatedTypeFactory typeFactory, ExpressionTree expressionTree, ExecutableElement methodElem, AnnotatedTypeMirror.AnnotatedExecutableType methodType)
TypeArgumentInference
inferTypeArgs
in interface TypeArgumentInference
typeFactory
- the type factory used to create methodTypeexpressionTree
- a tree representing the method or constructor invocation for which we are
inferring type argumentsmethodElem
- the element for the declaration of the method being invokedmethodType
- the declaration type of method elemNote: We use the Java TypeVariable type because this uniquely identifies a declaration where as two uses of an AnnotatedTypeVariable may be uses of the same declaration but are not .equals to each other.
public void adaptMethodType(AnnotatedTypeFactory typeFactory, ExpressionTree invocation, AnnotatedTypeMirror.AnnotatedExecutableType methodType)
adaptMethodType
in interface TypeArgumentInference
protected List<AnnotatedTypeMirror> getArgumentTypes(ExpressionTree expression, AnnotatedTypeFactory typeFactory)
protected AnnotatedTypeMirror getAssignedTo(ExpressionTree expression, AnnotatedTypeFactory typeFactory)
protected Set<AFConstraint> createArgumentAFConstraints(AnnotatedTypeFactory typeFactory, List<AnnotatedTypeMirror> argTypes, AnnotatedTypeMirror.AnnotatedExecutableType methodType, Set<TypeVariable> targets, boolean useNullArguments)
Ai << Fi
for each Argument(Ai) to formal parameter(Fi).
Remove any constraint that does not involve a type parameter to be inferred. Reduce the
remaining constraints so that Fi = Tj where Tj is a type parameter with an argument to be
inferred. Return the resulting constraint set.typeFactory
- AnnotatedTypeFactoryargTypes
- list of annotated types corresponding to the arguments to the methodmethodType
- annotated type of the methodtargets
- type variables to be inferreduseNullArguments
- whether or not null method arguments should be consideredprotected Set<FIsA> createInitialAssignmentConstraints(AnnotatedTypeMirror assignedTo, AnnotatedTypeMirror boxedReturnType, AnnotatedTypeFactory typeFactory, Set<TypeVariable> targets)
public ConstraintMap createAssignmentConstraints(AnnotatedTypeMirror assignedTo, AnnotatedTypeMirror boxedReturnType, AnnotatedTypeMirror.AnnotatedExecutableType methodType, Set<AFConstraint> afArgumentConstraints, Map<TypeVariable,AnnotatedTypeMirror> inferredArgs, Set<TypeVariable> targets, AnnotatedTypeFactory typeFactory)
This method creates constraints:
protected void reduceAfConstraints(AnnotatedTypeFactory typeFactory, Set<AFConstraint> outgoing, Queue<AFConstraint> toProcess, Set<TypeVariable> targets)
protected Set<TUConstraint> afToTuConstraints(Set<? extends AFConstraint> afConstraints, Set<TypeVariable> targets)
public void addConstraintsBetweenTargets(Set<TUConstraint> constraints, Set<TypeVariable> targets, boolean asSubtype, AnnotatedTypeFactory typeFactory)
<A, B extends A>
implies a TUConstraint of B <: A
.
Add these to the constraint list.public AnnotatedTypeMirror.AnnotatedTypeVariable addOrGetDeclarations(TypeVariable target, AnnotatedTypeFactory typeFactory, Map<TypeVariable,AnnotatedTypeMirror.AnnotatedTypeVariable> declarations)