use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.
the class DOTCFGVisualizer method visualize.
@Override
@Nullable
public Map<String, Object> visualize(ControlFlowGraph cfg, Block entry, @Nullable Analysis<V, S, T> analysis) {
String dotGraph = visualizeGraph(cfg, entry, analysis);
String dotFileName = dotOutputFileName(cfg.underlyingAST);
try {
FileWriter fStream = new FileWriter(dotFileName);
BufferedWriter out = new BufferedWriter(fStream);
out.write(dotGraph);
out.close();
} catch (IOException e) {
throw new UserError("Error creating dot file (is the path valid?): " + dotFileName, e);
}
return Collections.singletonMap("dotFileName", dotFileName);
}
use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.
the class UnitsRelationsTools method getPrefix.
/**
* Retrieves the SI Prefix of an Annotation.
*
* @param unitsAnnotation an AnnotationMirror representing a Units Annotation
* @return a Prefix value (including Prefix.one), or null if it has none
*/
@Nullable
public static Prefix getPrefix(final AnnotationMirror unitsAnnotation) {
AnnotationValue annotationValue = getAnnotationMirrorPrefix(unitsAnnotation);
// if this Annotation has no prefix, return null
if (hasNoPrefix(annotationValue)) {
return null;
}
// if the Annotation has a value, then detect and match the string name of the prefix, and
// return the matching Prefix
String prefixString = annotationValue.getValue().toString();
for (Prefix prefix : Prefix.values()) {
if (prefixString.equals(prefix.toString())) {
return prefix;
}
}
// if none of the strings match, then return null
return null;
}
use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.
the class MustCallTransfer method createTemporaryVar.
/**
* Creates a variable declaration for the given expression node, if possible.
*
* @param node an expression node
* @return a variable tree for the node, or null if an appropriate containing element cannot be
* located
*/
@Nullable
protected VariableTree createTemporaryVar(Node node) {
ExpressionTree tree = (ExpressionTree) node.getTree();
TypeMirror treeType = TreeUtils.typeOf(tree);
Element enclosingElement;
TreePath path = atypeFactory.getPath(tree);
if (path == null) {
enclosingElement = TreeUtils.elementFromTree(tree).getEnclosingElement();
} else {
ClassTree classTree = TreePathUtil.enclosingClass(path);
enclosingElement = TreeUtils.elementFromTree(classTree);
}
if (enclosingElement == null) {
return null;
}
// Declare and initialize a new, unique variable
VariableTree tmpVarTree = treeBuilder.buildVariableDecl(treeType, uniqueName("temp-var"), enclosingElement, tree);
return tmpVarTree;
}
use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.
the class OptionalVisitor method isCallToIsPresent.
/**
* Is the expression a call to {@code isPresent} or {@code isEmpty}? If not, returns null. If so,
* returns a pair of (boolean, receiver expression). The boolean is true if the given expression
* is a call to {@code isPresent} and is false if the given expression is a call to {@code
* isEmpty}.
*
* @param expression an expression
* @return a pair of a boolean (indicating whether the expression is a call to {@code
* Optional.isPresent} or to {@code Optional.isEmpty}) and its receiver; or null if not a call
* to either of the methods
*/
@Nullable
private Pair<Boolean, ExpressionTree> isCallToIsPresent(ExpressionTree expression) {
ProcessingEnvironment env = checker.getProcessingEnvironment();
boolean negate = false;
while (true) {
switch(expression.getKind()) {
case PARENTHESIZED:
expression = ((ParenthesizedTree) expression).getExpression();
break;
case LOGICAL_COMPLEMENT:
expression = ((UnaryTree) expression).getExpression();
negate = !negate;
break;
case METHOD_INVOCATION:
if (TreeUtils.isMethodInvocation(expression, optionalIsPresent, env)) {
return Pair.of(!negate, TreeUtils.getReceiverTree(expression));
} else if (optionalIsEmpty != null && TreeUtils.isMethodInvocation(expression, optionalIsEmpty, env)) {
return Pair.of(negate, TreeUtils.getReceiverTree(expression));
} else {
return null;
}
default:
return null;
}
}
}
use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.
the class CreatesMustCallForElementSupplier method getCreatesMustCallForExpression.
/**
* Parses a single CreatesMustCallFor annotation. Clients should use {@link
* #getCreatesMustCallForExpressions(MethodInvocationNode, GenericAnnotatedTypeFactory,
* CreatesMustCallForElementSupplier)}, which handles the possibility of multiple such
* annotations, instead.
*
* @param createsMustCallFor a @CreatesMustCallFor annotation
* @param n the invocation of a reset method
* @param atypeFactory the type factory
* @param supplier a type factory that can supply the executable elements for CreatesMustCallFor
* and CreatesMustCallFor.List's value elements. Usually, you should just pass atypeFactory
* again. The arguments are different so that the given type factory's adherence to both
* protocols are checked by the type system.
* @return the Java expression representing the target, or null if the target is unparseable
*/
@Nullable
static JavaExpression getCreatesMustCallForExpression(AnnotationMirror createsMustCallFor, MethodInvocationNode n, GenericAnnotatedTypeFactory<?, ?, ?, ?> atypeFactory, CreatesMustCallForElementSupplier supplier) {
// Unfortunately, there is no way to avoid passing the default string "this" here. The default
// must be hard-coded into the client, such as here. That is the price for the efficiency of not
// having to query the annotation definition (such queries are expensive).
String targetStrWithoutAdaptation = AnnotationUtils.getElementValue(createsMustCallFor, supplier.getCreatesMustCallForValueElement(), String.class, "this");
// TODO: find a way to also check if the target is a known tempvar, and if so return that. That
// should improve the quality of the error messages we give.
JavaExpression targetExpr;
try {
targetExpr = StringToJavaExpression.atMethodInvocation(targetStrWithoutAdaptation, n, atypeFactory.getChecker());
if (targetExpr instanceof Unknown) {
issueUnparseableError(n, atypeFactory, targetStrWithoutAdaptation);
return null;
}
} catch (JavaExpressionParseException e) {
issueUnparseableError(n, atypeFactory, targetStrWithoutAdaptation);
return null;
}
return targetExpr;
}
Aggregations