Class InferenceFactory
-
Constructor Summary
ConstructorDescriptionInferenceFactory
(Java8InferenceContext context) Creates an inference factory -
Method Summary
Modifier and TypeMethodDescriptionstatic AnnotatedTypeMirror
assignedToVariable
(AnnotatedTypeFactory atypeFactory, Tree assignmentContext) If the variable's type is a type variable, return getAnnotatedTypeLhsNoTypeVarDefault(tree).Returns the compile-time declaration of the method reference that is the method to which the expression refers.createFreshTypeVariable
(ProperType lowerBound, Set<? extends AnnotationMirror> lowerBoundAnnos, AbstractType upperBound, Set<? extends AnnotationMirror> upperBoundAnnos) Creates a fresh type variable using the upper and lower bounds provided.createThetaForCapture
(ExpressionTree tree, AbstractType capturedType) Creates capture variables for variables introduced by a capture bounds.createThetaForInvocation
(ExpressionTree invocation, InvocationType methodType, Java8InferenceContext context) If a mapping, theta, forinvocation
doesn't exist create it by:createThetaForLambda
(LambdaExpressionTree lambda, AbstractType functionalInterface) If a mapping, theta, forlambda
doesn't exist create it by:createThetaForMethodReference
(MemberReferenceTree memRef, InvocationType compileTimeDecl, Java8InferenceContext context) If a mapping, theta, formemRef
doesn't exist create it by:createWildcard
(ProperType lowerBound, AbstractType upperBound) Creates a wildcard using the upper and lower bounds provided.getCheckedExceptionConstraints
(ExpressionTree expression, AbstractType targetType, Theta map) Creates and returns the set of checked exception constraints for the given lambda or method reference.Return the proper type for object.org.plumelib.util.IPair<AbstractType,
AbstractType> Returns the pair ofa
as the least upper bound ofa
andb
andb
as the least upper bound ofa
andb
.Return the proper type for RuntimeException.getSubsTypeArgs
(List<TypeVariable> typeVar, List<AbstractType> typeArg, List<Variable> types) Returns the result of substitutingtypeArg
fortypeVar
intypes
.Gets the target type for the expression for which type arguments are being inferred.getTypeOfBound
(TypeParameterElement pEle, Theta map) Returns the type ofpEle
using the type variable to inference variable mapping,map
.getTypeOfElement
(Element element, Theta map) Returns the type ofelement
using the type variable to inference variable mapping,map
.getTypeOfMethodAdaptedToUse
(ExpressionTree invocation) Returns the type of the method or constructor invocation adapted to its arguments.static ExecutableType
getTypeOfMethodAdaptedToUse
(ExpressionTree expressionTree, Java8InferenceContext context) Return ExecutableType of the method invocation or new class tree adapted to the call site.glb
(Set<AbstractType> abstractTypes) Returns the greatest lower bound ofabstractTypes
.static TypeMirror
glb
(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) Returns the greatest lower bound oftm1
andtm2
.glb
(AbstractType a, AbstractType b) Returns the greatest lower bound ofa
andb
.lub
(Set<ProperType> properTypes) Return the least upper bounds ofproperTypes
.static TypeMirror
lub
(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) Returns the least upper bound oftm1
andtm2
.
-
Constructor Details
-
InferenceFactory
Creates an inference factory- Parameters:
context
- the context
-
-
Method Details
-
getTargetType
Gets the target type for the expression for which type arguments are being inferred.- Returns:
- target type for the expression for which type arguments are being inferred
-
assignedToVariable
public static AnnotatedTypeMirror assignedToVariable(AnnotatedTypeFactory atypeFactory, Tree assignmentContext) If the variable's type is a type variable, return getAnnotatedTypeLhsNoTypeVarDefault(tree). Rational:For example:
During type argument inference of<S> S bar () {...} <T> T foo(T p) { T local = bar(); return local; }
bar
, the assignment context islocal
. If the local variable default is used, then the type of assignment context type is@Nullable T
and the type argument inferred forbar()
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 forbar()
isT
. During dataflow, the type oflocal
is refined toT
and the return is legal.If the assignment context type was a declared type, for example:
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> S bar () {...} Object foo() { Object local = bar(); return local; }
S
instead of the local variable default.- Parameters:
atypeFactory
- AnnotatedTypeFactoryassignmentContext
- VariableTree- Returns:
- AnnotatedTypeMirror of Assignment context
-
getTypeOfMethodAdaptedToUse
public static ExecutableType getTypeOfMethodAdaptedToUse(ExpressionTree expressionTree, Java8InferenceContext context) Return ExecutableType of the method invocation or new class tree adapted to the call site.- Parameters:
expressionTree
- a method invocation or new class treecontext
- the context- Returns:
- ExecutableType of the method invocation or new class tree adapted to the call site
-
lub
Returns the least upper bound oftm1
andtm2
.- Parameters:
processingEnv
- the processing environmenttm1
- a typetm2
- a type- Returns:
- the least upper bound of
tm1
andtm2
-
glb
Returns the greatest lower bound oftm1
andtm2
.- Parameters:
processingEnv
- the processing environmenttm1
- a typetm2
- a type- Returns:
- the greatest lower bound of
tm1
andtm2
-
createThetaForInvocation
public Theta createThetaForInvocation(ExpressionTree invocation, InvocationType methodType, Java8InferenceContext context) If a mapping, theta, forinvocation
doesn't exist create it by:Creates inference variables for the type parameters to
methodType
for a particularinvocation
. Initializes the bounds of the variables. Returns a mapping from type variables to newly created variables.Otherwise, returns the previously created mapping.
- Parameters:
invocation
- method or constructor invocationmethodType
- type of generic methodcontext
- Java8InferenceContext- Returns:
- a mapping of the type variables of
methodType
to inference variables
-
createThetaForMethodReference
public Theta createThetaForMethodReference(MemberReferenceTree memRef, InvocationType compileTimeDecl, Java8InferenceContext context) If a mapping, theta, formemRef
doesn't exist create it by:Creates inference variables for the type parameters to
compileTimeDecl
for a particular method reference. Initializes the bounds of the variables. Returns a mapping from type variables to newly created variables.Otherwise, returns the previously created mapping.
- Parameters:
memRef
- method reference treecompileTimeDecl
- type of generic methodcontext
- Java8InferenceContext- Returns:
- a mapping of the type variables of
compileTimeDecl
to inference variables
-
createThetaForLambda
If a mapping, theta, forlambda
doesn't exist create it by:Creates inference variables for the type parameters to the functional inference of the lambda. Initializes the bounds of the variables. Returns a mapping from type variables to newly created variables.
Otherwise, returns the previously created mapping.
- Parameters:
lambda
- lambda expression treefunctionalInterface
- functional interface of the lambda- Returns:
- a mapping of the type variables of
compileTimeDecl
to inference variables
-
createThetaForCapture
Creates capture variables for variables introduced by a capture bounds. The new variables correspond to the type parameters ofcapturedType
.- Parameters:
tree
- invocation tree that created the capture boundcapturedType
- type that should be captured- Returns:
- a mapping of the type variables of
capturedType
to capture inference variables
-
getTypeOfMethodAdaptedToUse
Returns the type of the method or constructor invocation adapted to its arguments. This type may include inference variables.- Parameters:
invocation
- method or constructor invocation- Returns:
- the type of the method or constructor invocation adapted to its arguments
-
compileTimeDeclarationType
Returns the compile-time declaration of the method reference that is the method to which the expression refers. See JLS section 15.13.1 for a complete definition.The type of a member reference is a functional interface. The function type of a member reference is the type of the single abstract method declared by the functional interface. The compile-time declaration type is the type of the actual method referenced by the method reference, i.e. the method that is actually being referenced.
For example,
static class MyClass { String field; public static int compareByField(MyClass a, MyClass b) { ... } } Comparator<MyClass> func = MyClass::compareByField;
The function type is
compare(Comparator<MyClass> this, MyClass o1, MyClass o2)
where as the compile-time declaration type iscompareByField(MyClass a, MyClass b)
.- Parameters:
memRef
- method reference tree- Returns:
- the compile-time declaration of the method reference
-
getParameterizedSupers
public org.plumelib.util.IPair<AbstractType,AbstractType> getParameterizedSupers(AbstractType a, AbstractType b) Returns the pair ofa
as the least upper bound ofa
andb
andb
as the least upper bound ofa
andb
.- Parameters:
a
- typeb
- type- Returns:
- the pair of
a
as the least upper bound ofa
andb
and *b
as the least upper bound ofa
andb
-
getTypeOfElement
Returns the type ofelement
using the type variable to inference variable mapping,map
.- Parameters:
element
- some elementmap
- type parameter to inference variables to use- Returns:
- the type of
element
-
getTypeOfBound
Returns the type ofpEle
using the type variable to inference variable mapping,map
.- Parameters:
pEle
- some elementmap
- type parameter to inference variables to use- Returns:
- the type of
pEle
-
getObject
Return the proper type for object.- Returns:
- the proper type for object
-
lub
Return the least upper bounds ofproperTypes
.- Parameters:
properTypes
- types to lub- Returns:
- the least upper bounds of
properTypes
-
glb
Returns the greatest lower bound ofabstractTypes
.- Parameters:
abstractTypes
- types to glb- Returns:
- the greatest upper bounds of
abstractTypes
-
glb
Returns the greatest lower bound ofa
andb
.- Parameters:
a
- type to glbb
- type to glb- Returns:
- the greatest lower bound of
a
andb
-
getRuntimeException
Return the proper type for RuntimeException.- Returns:
- the proper type for RuntimeException
-
getCheckedExceptionConstraints
public ConstraintSet getCheckedExceptionConstraints(ExpressionTree expression, AbstractType targetType, Theta map) Creates and returns the set of checked exception constraints for the given lambda or method reference.- Parameters:
expression
- a lambda or method reference expressiontargetType
- the target type ofexpression
map
- theta- Returns:
- the set of checked exception constraints for the given lambda or method reference
-
createWildcard
Creates a wildcard using the upper and lower bounds provided.- Parameters:
lowerBound
- a proper type or nullupperBound
- an abstract type or null- Returns:
- a wildcard with the provided upper and lower bounds
-
createFreshTypeVariable
public AbstractType createFreshTypeVariable(ProperType lowerBound, Set<? extends AnnotationMirror> lowerBoundAnnos, AbstractType upperBound, Set<? extends AnnotationMirror> upperBoundAnnos) Creates a fresh type variable using the upper and lower bounds provided.- Parameters:
lowerBound
- a proper type or nulllowerBoundAnnos
- annotations to use iflowerBound
is nullupperBound
- an abstract type or nullupperBoundAnnos
- annotations to use ifupperBound
is null- Returns:
- a fresh type variable with the provided upper and lower bounds
-
getSubsTypeArgs
public List<AbstractType> getSubsTypeArgs(List<TypeVariable> typeVar, List<AbstractType> typeArg, List<Variable> types) Returns the result of substitutingtypeArg
fortypeVar
intypes
.- Parameters:
typeVar
- type variablestypeArg
- type argumentstypes
- types- Returns:
- the result of substituting
typeArg
fortypeVar
intypes
-