Search in sources :

Example 76 with Nullable

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

the class TypesUtils method mostSpecific.

/**
 * Returns the most specific type from the list, or null if none exists.
 *
 * @param typeMirrors a list of types
 * @param processingEnv the {@link ProcessingEnvironment} to use
 * @return the most specific of the types, or null if none exists
 */
@Nullable
public static TypeMirror mostSpecific(List<TypeMirror> typeMirrors, ProcessingEnvironment processingEnv) {
    if (typeMirrors.size() == 1) {
        return typeMirrors.get(0);
    } else {
        JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
        com.sun.tools.javac.code.Types types = com.sun.tools.javac.code.Types.instance(javacEnv.getContext());
        com.sun.tools.javac.util.List<Type> typeList = typeMirrorListToTypeList(typeMirrors);
        Type glb = types.glb(typeList);
        for (Type candidate : typeList) {
            if (types.isSameType(glb, candidate)) {
                return candidate;
            }
        }
        return null;
    }
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) UnionType(javax.lang.model.type.UnionType) PrimitiveType(javax.lang.model.type.PrimitiveType) Type(com.sun.tools.javac.code.Type) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 77 with Nullable

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

the class TreePathUtil method enclosingOfClass.

/**
 * Gets the first (innermost) enclosing tree in path, of the specified class.
 *
 * @param <T> the type of {@code treeClass}
 * @param path the path defining the tree node
 * @param treeClass the class of the desired tree
 * @return the enclosing tree of the given type as given by the path, {@code null} otherwise
 */
@Nullable
public static <T extends Tree> T enclosingOfClass(final TreePath path, final Class<T> treeClass) {
    TreePath p = path;
    while (p != null) {
        Tree leaf = p.getLeaf();
        if (treeClass.isInstance(leaf)) {
            return treeClass.cast(leaf);
        }
        p = p.getParentPath();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) BlockTree(com.sun.source.tree.BlockTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 78 with Nullable

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

the class Resolver method findField.

/**
 * Finds the field with name {@code name} in {@code type} or a superclass or superinterface of
 * {@code type}.
 *
 * <p>The method adheres to all the rules of Java's scoping (while also considering the imports)
 * for name resolution.
 *
 * @param name the name of the field
 * @param type the type of the receiver (i.e., the type in which to look for the field)
 * @param path the tree path to the local scope
 * @return the element for the field, {@code null} otherwise
 */
@Nullable
public VariableElement findField(String name, TypeMirror type, TreePath path) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        Env<AttrContext> env = getEnvForPath(path);
        Element res = wrapInvocationOnResolveInstance(FIND_IDENT_IN_TYPE, env, type, names.fromString(name), Kinds.KindSelector.VAR);
        if (res.getKind().isField()) {
            return (VariableElement) res;
        } else if (res.getKind() == ElementKind.OTHER && ACCESSERROR.isInstance(res)) {
            // Return the inaccessible field that was found
            return (VariableElement) wrapInvocation(res, ACCESSERROR_ACCESS, null, null);
        } else {
            // Most likely didn't find the field and the Element is a SymbolNotFoundError
            return null;
        }
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) AttrContext(com.sun.tools.javac.comp.AttrContext) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 79 with Nullable

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

the class Resolver method findClassInPackage.

/**
 * Finds the class with name {@code name} in a given package.
 *
 * @param name the name of the class
 * @param pck the PackageSymbol for the package
 * @param path the tree path to the local scope
 * @return the {@code ClassSymbol} for the class if it is found, {@code null} otherwise
 */
@Nullable
public ClassSymbol findClassInPackage(String name, PackageSymbol pck, TreePath path) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        Env<AttrContext> env = getEnvForPath(path);
        Element res = wrapInvocationOnResolveInstance(FIND_IDENT_IN_PACKAGE, env, pck, names.fromString(name), Kinds.KindSelector.TYP);
        if (ElementUtils.isTypeElement(res)) {
            return (ClassSymbol) res;
        } else {
            return null;
        }
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) AttrContext(com.sun.tools.javac.comp.AttrContext) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 80 with Nullable

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

the class Resolver method findMethod.

/**
 * Finds the method element for a given name and list of expected parameter types.
 *
 * <p>The method adheres to all the rules of Java's scoping (while also considering the imports)
 * for name resolution.
 *
 * <p>(This method takes into account autoboxing.)
 *
 * @param methodName name of the method to find
 * @param receiverType type of the receiver of the method
 * @param path tree path
 * @param argumentTypes types of arguments passed to the method call
 * @return the method element (if found)
 */
@Nullable
public ExecutableElement findMethod(String methodName, TypeMirror receiverType, TreePath path, java.util.List<TypeMirror> argumentTypes) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        Env<AttrContext> env = getEnvForPath(path);
        Type site = (Type) receiverType;
        Name name = names.fromString(methodName);
        List<Type> argtypes = List.nil();
        for (TypeMirror a : argumentTypes) {
            argtypes = argtypes.append((Type) a);
        }
        List<Type> typeargtypes = List.nil();
        boolean allowBoxing = true;
        boolean useVarargs = false;
        try {
            // For some reason we have to set our own method context, which is rather ugly.
            // TODO: find a nicer way to do this.
            Object methodContext = buildMethodContext();
            Object oldContext = getField(resolve, "currentResolutionContext");
            setField(resolve, "currentResolutionContext", methodContext);
            Element result = wrapInvocationOnResolveInstance(FIND_METHOD, env, site, name, argtypes, typeargtypes, allowBoxing, useVarargs);
            setField(resolve, "currentResolutionContext", oldContext);
            if (result.getKind() == ElementKind.METHOD || result.getKind() == ElementKind.CONSTRUCTOR) {
                return (ExecutableElement) result;
            }
            return null;
        } catch (Throwable t) {
            Error err = new AssertionError(String.format("Unexpected reflection error in findMethod(%s, %s, ..., %s)", methodName, receiverType, // path
            argumentTypes));
            err.initCause(t);
            throw err;
        }
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) AttrContext(com.sun.tools.javac.comp.AttrContext) Name(com.sun.tools.javac.util.Name) Type(com.sun.tools.javac.code.Type) TypeMirror(javax.lang.model.type.TypeMirror) 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