Class ParamApplier
- java.lang.Object
-
- org.checkerframework.framework.util.element.ParamApplier
-
public class ParamApplier extends java.lang.Object
Adds annotations to one formal parameter of a method or lambda within a method.
-
-
Field Summary
Fields Modifier and Type Field Description protected javax.lang.model.element.Element
element
An Element that type represents.static int
RECEIVER_PARAM_INDEX
protected AnnotatedTypeMirror
type
The type to which we wish to apply annotations.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static boolean
accepts(AnnotatedTypeMirror type, javax.lang.model.element.Element element)
protected com.sun.tools.javac.code.TargetType[]
annotatedTargets()
Returns {TargetType.METHOD_FORMAL_PARAMETER, TargetType.METHOD_RECEIVER}.static void
apply(AnnotatedTypeMirror type, javax.lang.model.element.VariableElement element, AnnotatedTypeFactory typeFactory)
Apply annotations fromelement
totype
.void
extractAndApply()
Reads the list of annotations that apply to this element (see getRawTypeAttributes).int
getElementIndex()
Returns the index of element its parent method's parameter list.static com.sun.tools.javac.code.Symbol.MethodSymbol
getParentMethod(javax.lang.model.element.Element methodChildElem)
Return the enclosing MethodSymbol of the given element, throwing an exception if the symbol's enclosing element is not a MethodSymbol.protected java.lang.Iterable<com.sun.tools.javac.code.Attribute.TypeCompound>
getRawTypeAttributes()
Returns the TypeCompounds (annotations) of the enclosing method for this parameter.int
getTypeCompoundIndex(com.sun.tools.javac.code.Attribute.TypeCompound anno)
Returns the parameter index of anno's TypeAnnotationPosition.protected void
handleInvalid(java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound> invalid)
This implementation reports all invalid annotations as errors.protected void
handleTargeted(java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound> targeted)
This method should apply all annotations that are handled by this object.protected void
handleValid(java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound> valid)
The default implementation of this method does nothing.protected boolean
isAccepted()
Tests element/type fields to ensure that this TargetedElementAnnotationApplier is valid for this element/type pair.protected java.util.Map<org.checkerframework.framework.util.element.TargetedElementAnnotationApplier.TargetClass,java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound>>
sift(java.lang.Iterable<com.sun.tools.javac.code.Attribute.TypeCompound> typeCompounds)
Separate the input annotations into a Map of TargetClass (TARGETED, VALID, INVALID) to the annotations that fall into each of those categories.protected com.sun.tools.javac.code.TargetType[]
validTargets()
Returns any annotation TargetType that can be found on a method.
-
-
-
Field Detail
-
RECEIVER_PARAM_INDEX
public static final int RECEIVER_PARAM_INDEX
- See Also:
- Constant Field Values
-
type
protected final AnnotatedTypeMirror type
The type to which we wish to apply annotations.
-
element
protected final javax.lang.model.element.Element element
An Element that type represents.
-
-
Method Detail
-
apply
public static void apply(AnnotatedTypeMirror type, javax.lang.model.element.VariableElement element, AnnotatedTypeFactory typeFactory) throws ElementAnnotationUtil.UnexpectedAnnotationLocationException
Apply annotations fromelement
totype
.- Parameters:
type
- the type whose annotations to changeelement
- where to get annotations fromtypeFactory
- the type factory- Throws:
ElementAnnotationUtil.UnexpectedAnnotationLocationException
- if there is trouble
-
accepts
public static boolean accepts(AnnotatedTypeMirror type, javax.lang.model.element.Element element)
-
getElementIndex
public int getElementIndex()
Returns the index of element its parent method's parameter list. Integer.MIN_VALUE if the element is the receiver parameter.- Returns:
- the index of element its parent method's parameter list. Integer.MIN_VALUE if the element is the receiver parameter
-
getTypeCompoundIndex
public int getTypeCompoundIndex(com.sun.tools.javac.code.Attribute.TypeCompound anno)
Returns the parameter index of anno's TypeAnnotationPosition.- Parameters:
anno
- an annotation we might wish to apply- Returns:
- the parameter index of anno's TypeAnnotationPosition
-
annotatedTargets
protected com.sun.tools.javac.code.TargetType[] annotatedTargets()
Returns {TargetType.METHOD_FORMAL_PARAMETER, TargetType.METHOD_RECEIVER}.- Returns:
- {TargetType.METHOD_FORMAL_PARAMETER, TargetType.METHOD_RECEIVER}
-
validTargets
protected com.sun.tools.javac.code.TargetType[] validTargets()
Returns any annotation TargetType that can be found on a method.- Returns:
- any annotation TargetType that can be found on a method
-
getRawTypeAttributes
protected java.lang.Iterable<com.sun.tools.javac.code.Attribute.TypeCompound> getRawTypeAttributes()
Returns the TypeCompounds (annotations) of the enclosing method for this parameter.- Returns:
- the TypeCompounds (annotations) of the enclosing method for this parameter
-
sift
protected java.util.Map<org.checkerframework.framework.util.element.TargetedElementAnnotationApplier.TargetClass,java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound>> sift(java.lang.Iterable<com.sun.tools.javac.code.Attribute.TypeCompound> typeCompounds)
Separate the input annotations into a Map of TargetClass (TARGETED, VALID, INVALID) to the annotations that fall into each of those categories.- Parameters:
typeCompounds
- annotations to sift through, should be those returned by getRawTypeAttributes- Returns:
- a Map<TargetClass => Annotations>.
-
handleTargeted
protected void handleTargeted(java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound> targeted) throws ElementAnnotationUtil.UnexpectedAnnotationLocationException
This method should apply all annotations that are handled by this object.- Parameters:
targeted
- type compounds with formal method parameter target types with parameter_index == getIndex- Throws:
ElementAnnotationUtil.UnexpectedAnnotationLocationException
-
isAccepted
protected boolean isAccepted()
Tests element/type fields to ensure that this TargetedElementAnnotationApplier is valid for this element/type pair.- Returns:
- true if the type/element members are handled by this class false otherwise
-
getParentMethod
public static com.sun.tools.javac.code.Symbol.MethodSymbol getParentMethod(javax.lang.model.element.Element methodChildElem)
Return the enclosing MethodSymbol of the given element, throwing an exception if the symbol's enclosing element is not a MethodSymbol.- Parameters:
methodChildElem
- some element that is a child of a method typeDeclaration (e.g. a parameter or return type)- Returns:
- the MethodSymbol of the method containing methodChildElem
-
extractAndApply
public void extractAndApply() throws ElementAnnotationUtil.UnexpectedAnnotationLocationException
Reads the list of annotations that apply to this element (see getRawTypeAttributes). Sifts them into three groups (TARGETED, INVALID, VALID) and then calls the appropriate handle method on them. The handleTargeted method should apply all annotations that are handled by this object.This method will throw a runtime exception if isAccepted returns false.
-
handleValid
protected void handleValid(java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound> valid)
The default implementation of this method does nothing.- Parameters:
valid
- the list of annotations that were returned by getRawTypeAttributes and had a TargetType contained by valid and NOT annotatedTargets
-
handleInvalid
protected void handleInvalid(java.util.List<com.sun.tools.javac.code.Attribute.TypeCompound> invalid)
This implementation reports all invalid annotations as errors.- Parameters:
invalid
- the list of annotations that were returned by getRawTypeAttributes and were not handled by handleTargeted or handleValid
-
-