T
- the type used by the storage to store annotations. See WholeProgramInferenceStorage
public class WholeProgramInferenceImplementation<T> extends Object implements WholeProgramInference
WholeProgramInference
. It uses an instance of
WholeProgramInferenceStorage
to store annotations and to create output files.
This class does not perform inference for an element if the element has explicit annotations.
That is, calling an update*
method on an explicitly annotated field, method return, or
method parameter has no effect.
In addition, whole program inference ignores inferred types in a few scenarios. When discovering a use, WPI ignores an inferred type if:
null
literal, except when doing inference for the
NullnessChecker. (The rationale for this is that null
is a frequently-used default
value, and it would be undesirable to infer the bottom type if null
were the only
value passed as an argument.)
WholeProgramInference.OutputFormat
Modifier and Type | Field and Description |
---|---|
protected AnnotatedTypeFactory |
atypeFactory
The type factory associated with this.
|
Constructor and Description |
---|
WholeProgramInferenceImplementation(AnnotatedTypeFactory atypeFactory,
WholeProgramInferenceStorage<T> storage)
Constructs a new
WholeProgramInferenceImplementation that has not yet inferred any
annotations. |
Modifier and Type | Method and Description |
---|---|
void |
addMethodDeclarationAnnotation(ExecutableElement methodElt,
AnnotationMirror anno)
Updates a method to add a declaration annotation.
|
WholeProgramInferenceStorage<T> |
getStorage()
Returns the storage for inferred annotations.
|
protected boolean |
ignoreFieldInWPI(Element element,
String fieldName)
Returns true if an assignment to the given field should be ignored by WPI.
|
void |
preprocessClassTree(ClassTree classTree)
Performs any preparation required for inference on Elements of a class.
|
protected void |
updateAnnotationSet(T annotationsToUpdate,
TypeUseLocation defLoc,
AnnotatedTypeMirror rhsATM,
AnnotatedTypeMirror lhsATM,
String file)
Updates the set of annotations in a location in a program.
|
protected void |
updateAnnotationSet(T annotationsToUpdate,
TypeUseLocation defLoc,
AnnotatedTypeMirror rhsATM,
AnnotatedTypeMirror lhsATM,
String file,
boolean ignoreIfAnnotated)
Updates the set of annotations in a location in a program.
|
void |
updateContracts(Analysis.BeforeOrAfter preOrPost,
ExecutableElement methodElt,
CFAbstractStore<?,?> store)
Updates the preconditions or postconditions of the current method, from a store.
|
void |
updateFieldFromType(Tree lhsTree,
Element element,
String fieldName,
AnnotatedTypeMirror rhsATM)
Updates the type of
field based on an assignment whose right-hand side has type rhsATM . |
void |
updateFromFieldAssignment(Node lhs,
Node rhs)
Updates the type of
field based on an assignment of rhs to field . |
void |
updateFromFormalParameterAssignment(LocalVariableNode lhs,
Node rhs,
VariableElement paramElt)
Updates the type of
lhs based on an assignment of rhs to lhs . |
void |
updateFromMethodInvocation(MethodInvocationNode methodInvNode,
ExecutableElement methodElt,
CFAbstractStore<?,?> store)
Updates the parameter types of the method
methodElt based on the arguments in the
method invocation methodInvNode . |
void |
updateFromObjectCreation(ObjectCreationNode objectCreationNode,
ExecutableElement constructorElt,
CFAbstractStore<?,?> store)
Updates the parameter types of the constructor
constructorElt based on the arguments in
objectCreationNode . |
void |
updateFromOverride(MethodTree methodTree,
ExecutableElement methodElt,
AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethod)
Updates the parameter types (including the receiver) of the method
methodTree based on
the parameter types of the overridden method overriddenMethod . |
void |
updateFromReturn(ReturnNode retNode,
com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol,
MethodTree methodDeclTree,
Map<AnnotatedTypeMirror.AnnotatedDeclaredType,ExecutableElement> overriddenMethods)
Updates the return type of the method
methodTree based on returnedExpression . |
void |
writeResultsToFile(WholeProgramInference.OutputFormat outputFormat,
BaseTypeChecker checker)
Writes the inferred results to a file.
|
protected final AnnotatedTypeFactory atypeFactory
public WholeProgramInferenceImplementation(AnnotatedTypeFactory atypeFactory, WholeProgramInferenceStorage<T> storage)
WholeProgramInferenceImplementation
that has not yet inferred any
annotations.atypeFactory
- the associated type factorystorage
- the storage used for inferred annotations and for writing output filespublic WholeProgramInferenceStorage<T> getStorage()
public void updateFromObjectCreation(ObjectCreationNode objectCreationNode, ExecutableElement constructorElt, CFAbstractStore<?,?> store)
WholeProgramInference
constructorElt
based on the arguments in
objectCreationNode
.
For each parameter in constructorElt:
updateFromObjectCreation
in interface WholeProgramInference
objectCreationNode
- the Node that invokes the constructorconstructorElt
- the Element of the constructorstore
- the store just before the callpublic void updateFromMethodInvocation(MethodInvocationNode methodInvNode, ExecutableElement methodElt, CFAbstractStore<?,?> store)
WholeProgramInference
methodElt
based on the arguments in the
method invocation methodInvNode
.
For each formal parameter in methodElt (including the receiver):
updateFromMethodInvocation
in interface WholeProgramInference
methodInvNode
- the node representing a method invocationmethodElt
- the element of the method being invokedstore
- the store before the method call, used for inferring method preconditionspublic void updateContracts(Analysis.BeforeOrAfter preOrPost, ExecutableElement methodElt, CFAbstractStore<?,?> store)
WholeProgramInference
updateContracts
in interface WholeProgramInference
preOrPost
- whether to update preconditions or postconditionsmethodElt
- the method or constructor whose preconditions or postconditions to updatestore
- the store at the method's entry or normal exit, for reading types of expressionspublic void updateFromOverride(MethodTree methodTree, ExecutableElement methodElt, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethod)
WholeProgramInference
methodTree
based on
the parameter types of the overridden method overriddenMethod
.
For each formal parameter in methodElt:
updateFromOverride
in interface WholeProgramInference
methodTree
- the tree of the method that contains the parameter(s)methodElt
- the element of the methodoverriddenMethod
- the AnnotatedExecutableType of the overridden methodpublic void updateFromFormalParameterAssignment(LocalVariableNode lhs, Node rhs, VariableElement paramElt)
WholeProgramInference
lhs
based on an assignment of rhs
to lhs
.
updateFromFormalParameterAssignment
in interface WholeProgramInference
lhs
- the node representing the formal parameterrhs
- the node being assigned to the parameter in the method bodyparamElt
- the formal parameterpublic void updateFromFieldAssignment(Node lhs, Node rhs)
WholeProgramInference
field
based on an assignment of rhs
to field
. If
the field has a declaration annotation with the IgnoreInWholeProgramInference
meta-annotation, no type annotation will be inferred for that field.
If there is no stored entry for the field lhs, the entry will be created and its type will be the type of rhs. If there is a stored entry/type for lhs, its new type will be the LUB between the previous type and the type of rhs.
updateFromFieldAssignment
in interface WholeProgramInference
lhs
- the field whose type will be refined. Must be either a FieldAccessNode or a
LocalVariableNode whose element kind is FIELD.rhs
- the expression being assigned to the fieldpublic void updateFieldFromType(Tree lhsTree, Element element, String fieldName, AnnotatedTypeMirror rhsATM)
WholeProgramInference
field
based on an assignment whose right-hand side has type rhsATM
. See more details at WholeProgramInference.updateFromFieldAssignment(org.checkerframework.dataflow.cfg.node.Node, org.checkerframework.dataflow.cfg.node.Node)
.updateFieldFromType
in interface WholeProgramInference
lhsTree
- the tree for the field whose type will be refinedelement
- the element for the field whose type will be refinedfieldName
- the name of the field whose type will be refinedrhsATM
- the type of the expression being assigned to the fieldprotected boolean ignoreFieldInWPI(Element element, String fieldName)
element
- the field's elementfieldName
- the field's namepublic void updateFromReturn(ReturnNode retNode, com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol, MethodTree methodDeclTree, Map<AnnotatedTypeMirror.AnnotatedDeclaredType,ExecutableElement> overriddenMethods)
WholeProgramInference
methodTree
based on returnedExpression
.
Also updates the return types of any methods that this method overrides that are available as
source code.
If there is no stored annotated return type for the method methodTree, then the type of the return expression will be added to the return type of that method. If there is a stored annotated return type for the method methodTree, its new type will be the LUB between the previous type and the type of the return expression.
updateFromReturn
in interface WholeProgramInference
retNode
- the node that contains the expression returnedclassSymbol
- the symbol of the class that contains the methodmethodDeclTree
- the tree of the method whose return type may be updatedoverriddenMethods
- the methods that the given method return overrides, indexed by the
annotated type of the superclass in which each method is definedpublic void addMethodDeclarationAnnotation(ExecutableElement methodElt, AnnotationMirror anno)
WholeProgramInference
addMethodDeclarationAnnotation
in interface WholeProgramInference
methodElt
- the method to annotateanno
- the declaration annotation to add to the methodprotected void updateAnnotationSet(T annotationsToUpdate, TypeUseLocation defLoc, AnnotatedTypeMirror rhsATM, AnnotatedTypeMirror lhsATM, String file)
Subclasses can customize this behavior.
annotationsToUpdate
- the type whose annotations are modified by this methoddefLoc
- the location where the annotation will be addedrhsATM
- the RHS of the annotated type on the source codelhsATM
- the LHS of the annotated type on the source codefile
- the annotation file containing the executable; used for marking the scene as
modified (needing to be written to disk)protected void updateAnnotationSet(T annotationsToUpdate, TypeUseLocation defLoc, AnnotatedTypeMirror rhsATM, AnnotatedTypeMirror lhsATM, String file, boolean ignoreIfAnnotated)
Subclasses can customize this behavior.
annotationsToUpdate
- the type whose annotations are modified by this methoddefLoc
- the location where the annotation will be addedrhsATM
- the RHS of the annotated type on the source codelhsATM
- the LHS of the annotated type on the source codefile
- annotation file containing the executable; used for marking the scene as modified
(needing to be written to disk)ignoreIfAnnotated
- if true, don't update any type that is explicitly annotated in the
source codepublic void writeResultsToFile(WholeProgramInference.OutputFormat outputFormat, BaseTypeChecker checker)
WholeProgramInference
writeResultsToFile
in interface WholeProgramInference
outputFormat
- the file format in which to write the resultschecker
- the checker from which this method is called, for naming stub filespublic void preprocessClassTree(ClassTree classTree)
WholeProgramInference
preprocessClassTree
in interface WholeProgramInference
classTree
- the class to preprocess