Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes related to Non-Empty Checker #6701

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Minor changes related to Non-Empty Checker
  • Loading branch information
mernst committed Jul 7, 2024
commit 9a52091a4e396b189f5bf676f629d8a0d6ad5911
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import org.checkerframework.framework.flow.CFStore;
import org.checkerframework.framework.flow.CFTransfer;
import org.checkerframework.framework.flow.CFValue;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
import org.checkerframework.javacutil.AnnotationBuilder;
import org.checkerframework.javacutil.TreeUtils;

/** The transfer function for the Optional Checker. */
public class OptionalTransfer extends CFTransfer {

/** The {@link OptionalAnnotatedTypeFactory} instance for this transfer class. */
private final OptionalAnnotatedTypeFactory optionalTypeFactory;

/** The @{@link Present} annotation. */
private final AnnotationMirror PRESENT;

Expand All @@ -38,20 +40,17 @@ public class OptionalTransfer extends CFTransfer {
/** The element for java.util.Optional.ifPresentOrElse(), or null. */
private final @Nullable ExecutableElement optionalIfPresentOrElse;

/** The type factory associated with this transfer function. */
private final AnnotatedTypeFactory atypeFactory;

/**
* Create an OptionalTransfer.
*
* @param analysis the Optional Checker instance
*/
public OptionalTransfer(CFAbstractAnalysis<CFValue, CFStore, CFTransfer> analysis) {
super(analysis);
atypeFactory = analysis.getTypeFactory();
Elements elements = atypeFactory.getElementUtils();
optionalTypeFactory = (OptionalAnnotatedTypeFactory) analysis.getTypeFactory();
Elements elements = optionalTypeFactory.getElementUtils();
PRESENT = AnnotationBuilder.fromClass(elements, Present.class);
ProcessingEnvironment env = atypeFactory.getProcessingEnv();
ProcessingEnvironment env = optionalTypeFactory.getProcessingEnv();
optionalIfPresent = TreeUtils.getMethod("java.util.Optional", "ifPresent", 1, env);
optionalIfPresentOrElse =
TreeUtils.getMethodOrNull("java.util.Optional", "ifPresentOrElse", 2, env);
Expand All @@ -70,7 +69,7 @@ public CFStore initialStore(UnderlyingAST underlyingAST, List<LocalVariableNode>
LambdaExpressionTree lambdaTree = cfgLambda.getLambdaTree();
List<? extends VariableTree> lambdaParams = lambdaTree.getParameters();
if (lambdaParams.size() == 1) {
TreePath lambdaPath = atypeFactory.getPath(lambdaTree);
TreePath lambdaPath = optionalTypeFactory.getPath(lambdaTree);
Tree lambdaParent = lambdaPath.getParentPath().getLeaf();
if (lambdaParent.getKind() == Tree.Kind.METHOD_INVOCATION) {
MethodInvocationTree invok = (MethodInvocationTree) lambdaParent;
Expand Down
6 changes: 3 additions & 3 deletions docs/manual/non-empty-checker.tex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
\htmlhr
\chapterAndLabel{Non-Empty Checker for container classes}{non-empty-checker}

The Non-Empty Checker tracks whether a container may be empty.
It works on containers such as
The Non-Empty Checker tracks whether a container is possibly-empty or is
definitely non-empty. It works on containers such as
\<Collection>s, \<Iterator>s, \<Iterable>s, and \<Map>s, but not \<Optional>s.

If the Non-Empty Checker issues no warnings, then your program does not
Expand Down Expand Up @@ -145,7 +145,7 @@
by the Non-Empty Checker:

\begin{Verbatim}
// This method might return an empty list, depending on the argument
// This method might return an empty list, depending on the argument.
List<String> getRegionIds(String region) { ... }

void parseRegions() {
Expand Down