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;
}
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations