Search in sources :

Example 1 with CanonicalNameOrEmpty

use of org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty in project checker-framework by typetools.

the class ReflectiveEvaluator method getMethodObject.

/**
 * Method for reflectively obtaining a method object so it can (potentially) be statically
 * executed by the checker for constant propagation.
 *
 * @param tree a method invocation tree
 * @return the Method object corresponding to the method invocation tree
 */
private Method getMethodObject(MethodInvocationTree tree) {
    final ExecutableElement ele = TreeUtils.elementFromUse(tree);
    List<Class<?>> paramClasses = null;
    try {
        @CanonicalNameOrEmpty String className = TypesUtils.getQualifiedName((DeclaredType) ele.getEnclosingElement().asType());
        paramClasses = getParameterClasses(ele);
        // https://tinyurl.com/cfissue/658 for Class.toString
        @SuppressWarnings("signature") Class<?> clazz = Class.forName(className.toString());
        Method method = clazz.getMethod(ele.getSimpleName().toString(), paramClasses.toArray(new Class<?>[0]));
        // TODO: find alternative
        @SuppressWarnings("deprecation") boolean acc = method.isAccessible();
        if (!acc) {
            method.setAccessible(true);
        }
        return method;
    } catch (ClassNotFoundException | UnsupportedClassVersionError | NoClassDefFoundError e) {
        if (reportWarnings) {
            checker.reportWarning(tree, "class.find.failed", ele.getEnclosingElement());
        }
        return null;
    } catch (Throwable e) {
        // The class we attempted to getMethod from inside the
        // call to getMethodObject.
        Element classElem = ele.getEnclosingElement();
        if (classElem == null) {
            if (reportWarnings) {
                checker.reportWarning(tree, "method.find.failed", ele.getSimpleName(), paramClasses);
            }
        } else {
            if (reportWarnings) {
                checker.reportWarning(tree, "method.find.failed.in.class", ele.getSimpleName(), paramClasses, classElem);
            }
        }
        return null;
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) Method(java.lang.reflect.Method) CanonicalNameOrEmpty(org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty)

Example 2 with CanonicalNameOrEmpty

use of org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty in project checker-framework by typetools.

the class AnnotationFileElementTypes method getOutermostEnclosingClass.

/**
 * Returns the fully qualified name of the outermost enclosing class of {@code e} or {@code null}
 * if no such class exists for {@code e}.
 *
 * @param e an element whose outermost enclosing class to return
 * @return the canonical name of the outermost enclosing class of {@code e} or {@code null} if no
 *     class encloses {@code e}
 */
@CanonicalNameOrEmpty
private String getOutermostEnclosingClass(Element e) {
    TypeElement enclosingClass = ElementUtils.enclosingTypeElement(e);
    if (enclosingClass == null) {
        return null;
    }
    while (true) {
        Element element = enclosingClass.getEnclosingElement();
        if (element == null || element.getKind() == ElementKind.PACKAGE) {
            break;
        }
        TypeElement t = ElementUtils.enclosingTypeElement(element);
        if (t == null) {
            break;
        }
        enclosingClass = t;
    }
    @// https://tinyurl.com/cfissue/658:
    SuppressWarnings(// https://tinyurl.com/cfissue/658:
    "signature:assignment") @CanonicalNameOrEmpty String result = enclosingClass.getQualifiedName().toString();
    return result;
}
Also used : CanonicalNameOrEmpty(org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) CanonicalNameOrEmpty(org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty)

Example 3 with CanonicalNameOrEmpty

use of org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty in project checker-framework by typetools.

the class TypesUtils method getQualifiedName.

// / Getters
/**
 * Gets the fully qualified name for a provided type. It returns an empty name if type is an
 * anonymous type.
 *
 * @param type the declared type
 * @return the name corresponding to that type
 */
// todo: add fake override of Name.toString.
@SuppressWarnings("signature:return")
@CanonicalNameOrEmpty
public static String getQualifiedName(DeclaredType type) {
    TypeElement element = (TypeElement) type.asElement();
    @CanonicalNameOrEmpty Name name = element.getQualifiedName();
    return name.toString();
}
Also used : CanonicalNameOrEmpty(org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty) TypeElement(javax.lang.model.element.TypeElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) Name(javax.lang.model.element.Name) CanonicalNameOrEmpty(org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty)

Aggregations

CanonicalNameOrEmpty (org.checkerframework.checker.signature.qual.CanonicalNameOrEmpty)3 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 Method (java.lang.reflect.Method)1 Name (javax.lang.model.element.Name)1 BinaryName (org.checkerframework.checker.signature.qual.BinaryName)1 FullyQualifiedName (org.checkerframework.checker.signature.qual.FullyQualifiedName)1