Search in sources :

Example 61 with Nullable

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);
}
Also used : UserError(org.checkerframework.javacutil.UserError) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 62 with Nullable

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;
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) Prefix(org.checkerframework.checker.units.qual.Prefix) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 63 with Nullable

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;
}
Also used : TreePath(com.sun.source.util.TreePath) TypeMirror(javax.lang.model.type.TypeMirror) Element(javax.lang.model.element.Element) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) ExpressionTree(com.sun.source.tree.ExpressionTree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 64 with Nullable

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;
        }
    }
}
Also used : ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 65 with Nullable

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;
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) Unknown(org.checkerframework.dataflow.expression.Unknown) JavaExpressionParseException(org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

Nullable (org.checkerframework.checker.nullness.qual.Nullable)285 ArrayList (java.util.ArrayList)27 TypeElement (javax.lang.model.element.TypeElement)23 IOException (java.io.IOException)21 Map (java.util.Map)21 ExecutableElement (javax.lang.model.element.ExecutableElement)19 ServerLevel (net.minecraft.server.level.ServerLevel)19 List (java.util.List)16 Instant (org.joda.time.Instant)16 VariableElement (javax.lang.model.element.VariableElement)15 HashMap (java.util.HashMap)14 Optional (java.util.Optional)14 Element (javax.lang.model.element.Element)13 TreePath (com.sun.source.util.TreePath)12 BlockPos (net.minecraft.core.BlockPos)12 Method (java.lang.reflect.Method)11 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)11 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)10 VariableTree (com.sun.source.tree.VariableTree)8 ClassTree (com.sun.source.tree.ClassTree)7