Search in sources :

Example 21 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.

the class StubParser method findTypeOfName.

/**
 * Returns the TypeElement with the name {@code name}, if one exists. Otherwise, checks the
 * class and package of {@code parseState} for a class named {@code name}.
 *
 * @param name classname (simple, or Outer.Inner, or fully-qualified)
 * @return the TypeElement for {@code name}, or null if not found
 */
@Nullable
private TypeElement findTypeOfName(String name) {
    String packageName = parseState.packageName;
    String packagePrefix = (packageName == null) ? "" : packageName + ".";
    // stubWarn("findTypeOfName(%s), parseState %s %s%n", name, packageName, enclosingClass);
    // As soon as typeElement is set to a non-null value, it will be returned.
    TypeElement typeElement = getTypeElementOrNull(name);
    if (typeElement == null && packageName != null) {
        typeElement = getTypeElementOrNull(packagePrefix + name);
    }
    String enclosingClass = parseState.className;
    while (typeElement == null && enclosingClass != null) {
        typeElement = getTypeElementOrNull(packagePrefix + enclosingClass + "." + name);
        int lastDot = enclosingClass.lastIndexOf('.');
        if (lastDot == -1) {
            break;
        } else {
            enclosingClass = enclosingClass.substring(0, lastDot);
        }
    }
    if (typeElement == null && !"java.lang".equals(packageName)) {
        typeElement = getTypeElementOrNull("java.lang." + name);
    }
    return typeElement;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 22 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.

the class AnnotatedTypeFactory method getCurrentMethodReceiver.

/**
 * Returns the receiver type of the current method being visited, and returns null if the
 * visited tree is not within a method or if that method has no receiver (e.g. a static method).
 *
 * <p>The method uses the parameter only if the most enclosing method cannot be found directly.
 *
 * @return receiver type of the most enclosing method being visited
 */
@Nullable
protected final AnnotatedDeclaredType getCurrentMethodReceiver(Tree tree) {
    AnnotatedDeclaredType res = visitorState.getMethodReceiver();
    if (res == null) {
        TreePath path = getPath(tree);
        if (path != null) {
            MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
            ClassTree enclosingClass = TreeUtils.enclosingClass(path);
            boolean found = false;
            for (Tree member : enclosingClass.getMembers()) {
                if (member.getKind() == Tree.Kind.METHOD) {
                    if (member == enclosingMethod) {
                        found = true;
                    }
                }
            }
            if (found && enclosingMethod != null) {
                AnnotatedExecutableType method = getAnnotatedType(enclosingMethod);
                res = method.getReceiverType();
            // TODO: three tests fail if one adds the following, which would make
            // sense, or not?
            // visitorState.setMethodReceiver(res);
            } else {
                // We are within an anonymous class or field initializer
                res = this.getAnnotatedType(enclosingClass);
            }
        }
    }
    return res;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ReturnTree(com.sun.source.tree.ReturnTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 23 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.

the class UnitsRelationsDefault method multiplication.

/**
 * Provides rules for resolving the result Unit of the multiplication of checker-framework
 * provided Units
 */
@Override
@Nullable
public AnnotationMirror multiplication(AnnotatedTypeMirror lht, AnnotatedTypeMirror rht) {
    // checking SI units only
    if (UnitsRelationsTools.hasSpecificUnitIgnoringPrefix(lht, m) && UnitsRelationsTools.hasSpecificUnitIgnoringPrefix(rht, m)) {
        if (UnitsRelationsTools.hasNoPrefix(lht) && UnitsRelationsTools.hasNoPrefix(rht)) {
            // m * m
            return m2;
        }
        Prefix lhtPrefix = UnitsRelationsTools.getPrefix(lht);
        Prefix rhtPrefix = UnitsRelationsTools.getPrefix(rht);
        if (bothHaveSpecificPrefix(lhtPrefix, rhtPrefix, Prefix.kilo)) {
            // km * km
            return km2;
        } else if (bothHaveSpecificPrefix(lhtPrefix, rhtPrefix, Prefix.one)) {
            // m(Prefix.one) * m(Prefix.one)
            return m2;
        } else if (bothHaveSpecificPrefix(lhtPrefix, rhtPrefix, Prefix.milli)) {
            // mm * mm
            return mm2;
        } else {
            return null;
        }
    } else if (havePairOfUnitsIgnoringOrder(lht, s, rht, mPERs)) {
        // s * mPERs or mPERs * s => m
        return m;
    } else if (havePairOfUnitsIgnoringOrder(lht, s, rht, mPERs2)) {
        // s * mPERs2 or mPERs2 * s => mPERs
        return mPERs;
    } else if (havePairOfUnitsIgnoringOrder(lht, h, rht, kmPERh)) {
        // h * kmPERh or kmPERh * h => km
        return km;
    } else {
        return null;
    }
}
Also used : Prefix(org.checkerframework.checker.units.qual.Prefix) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 24 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.

the class UnitsRelationsTools method buildAnnoMirrorWithSpecificPrefix.

/**
 * Creates an AnnotationMirror representing a unit defined by annoClass, with the specific
 * Prefix p
 *
 * @param env the Checker Processing Environment, provided as a parameter in init() of a
 *     UnitsRelations implementation
 * @param annoClass the Class of an Annotation representing a Unit (eg m.class for meters)
 * @param p a Prefix value
 * @return an AnnotationMirror of the Unit with the Prefix p, or null if it cannot be
 *     constructed
 */
@Nullable
public static AnnotationMirror buildAnnoMirrorWithSpecificPrefix(final ProcessingEnvironment env, final Class<? extends Annotation> annoClass, final Prefix p) {
    if (env == null || annoClass == null || p == null) {
        return null;
    }
    AnnotationBuilder builder = new AnnotationBuilder(env, annoClass);
    builder.setValue("value", p);
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 25 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project checker-framework by typetools.

the class ElementUtils method enclosingClass.

/**
 * Returns the innermost type element enclosing the given element
 *
 * @param elem the enclosed element of a class
 * @return the innermost type element
 */
public static TypeElement enclosingClass(final Element elem) {
    Element result = elem;
    while (result != null && !result.getKind().isClass() && !result.getKind().isInterface()) {
        @Nullable Element encl = result.getEnclosingElement();
        result = encl;
    }
    return (TypeElement) result;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

Nullable (org.checkerframework.checker.nullness.qual.Nullable)27 TypeElement (javax.lang.model.element.TypeElement)7 VariableElement (javax.lang.model.element.VariableElement)4 TreePath (com.sun.source.util.TreePath)3 Method (java.lang.reflect.Method)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 Element (javax.lang.model.element.Element)3 ClassTree (com.sun.source.tree.ClassTree)2 MethodTree (com.sun.source.tree.MethodTree)2 VariableTree (com.sun.source.tree.VariableTree)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 PackageElement (javax.lang.model.element.PackageElement)2 Prefix (org.checkerframework.checker.units.qual.Prefix)2 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)2 Node (org.checkerframework.dataflow.cfg.node.Node)2 Pure (org.checkerframework.dataflow.qual.Pure)2 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)2 AnnotationTree (com.sun.source.tree.AnnotationTree)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1 BlockTree (com.sun.source.tree.BlockTree)1