Search in sources :

Example 76 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class BaseTypeChecker method instantiateSubcheckers.

/*
   * Performs a depth first search for all checkers this checker depends on.
   * The depth first search ensures that the collection has the correct order the checkers need to be run in.
   *
   * Modifies the alreadyInitializedSubcheckerMap map by adding all recursively newly instantiated subcheckers' class objects and instances.
   * A LinkedHashMap is used because, unlike HashMap, it preserves the order in which entries were inserted.
   *
   * Returns the unmodifiable list of immediate subcheckers of this checker.
   */
private List<BaseTypeChecker> instantiateSubcheckers(LinkedHashMap<Class<? extends BaseTypeChecker>, BaseTypeChecker> alreadyInitializedSubcheckerMap) {
    LinkedHashSet<Class<? extends BaseTypeChecker>> classesOfImmediateSubcheckers = getImmediateSubcheckerClasses();
    if (classesOfImmediateSubcheckers.isEmpty()) {
        return Collections.emptyList();
    }
    ArrayList<BaseTypeChecker> immediateSubcheckers = new ArrayList<>(classesOfImmediateSubcheckers.size());
    for (Class<? extends BaseTypeChecker> subcheckerClass : classesOfImmediateSubcheckers) {
        BaseTypeChecker subchecker = alreadyInitializedSubcheckerMap.get(subcheckerClass);
        if (subchecker != null) {
            // Add the already initialized subchecker to the list of immediate subcheckers so
            // that this checker can refer to it.
            immediateSubcheckers.add(subchecker);
            continue;
        }
        BaseTypeChecker instance;
        try {
            instance = subcheckerClass.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new BugInCF("Could not create an instance of " + subcheckerClass);
        }
        instance.setProcessingEnvironment(this.processingEnv);
        instance.treePathCacher = this.getTreePathCacher();
        // Prevent the new checker from storing non-immediate subcheckers
        instance.subcheckers = Collections.emptyList();
        immediateSubcheckers.add(instance);
        instance.immediateSubcheckers = instance.instantiateSubcheckers(alreadyInitializedSubcheckerMap);
        instance.setParentChecker(this);
        alreadyInitializedSubcheckerMap.put(subcheckerClass, instance);
    }
    return Collections.unmodifiableList(immediateSubcheckers);
}
Also used : ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 77 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class AccumulationAnnotatedTypeFactory method convertToPredicate.

/**
 * Converts the given annotation mirror to a predicate.
 *
 * @param anno an annotation
 * @return the predicate, as a String, that is equivalent to that annotation. May return the empty
 *     string.
 */
protected String convertToPredicate(AnnotationMirror anno) {
    if (AnnotationUtils.areSame(anno, bottom)) {
        return "false";
    } else if (isPredicate(anno)) {
        String result = AnnotationUtils.getElementValueOrNull(anno, "value", String.class, false);
        return result == null ? "" : result;
    } else if (isAccumulatorAnnotation(anno)) {
        List<String> values = getAccumulatedValues(anno);
        StringJoiner sj = new StringJoiner(" && ");
        for (String value : values) {
            sj.add(value);
        }
        return sj.toString();
    } else {
        throw new BugInCF("annotation is not bottom, a predicate, or an accumulator: " + anno);
    }
}
Also used : BugInCF(org.checkerframework.javacutil.BugInCF) StringJoiner(java.util.StringJoiner)

Example 78 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class BaseTypeValidator method reportInvalidBounds.

/**
 * Most errors reported by this class are of the form type.invalid. This method reports when the
 * bounds of a wildcard or type variable don't make sense. Bounds make sense when the effective
 * annotations on the upper bound are supertypes of those on the lower bounds for all hierarchies.
 * To ensure that this subtlety is not lost on users, we report "bound" and print the bounds along
 * with the invalid type rather than a "type.invalid".
 *
 * @param type the type with invalid bounds
 * @param tree where to report the error
 */
protected void reportInvalidBounds(final AnnotatedTypeMirror type, final Tree tree) {
    final String label;
    final AnnotatedTypeMirror upperBound;
    final AnnotatedTypeMirror lowerBound;
    switch(type.getKind()) {
        case TYPEVAR:
            label = "type parameter";
            upperBound = ((AnnotatedTypeVariable) type).getUpperBound();
            lowerBound = ((AnnotatedTypeVariable) type).getLowerBound();
            break;
        case WILDCARD:
            label = "wildcard";
            upperBound = ((AnnotatedWildcardType) type).getExtendsBound();
            lowerBound = ((AnnotatedWildcardType) type).getSuperBound();
            break;
        default:
            throw new BugInCF("Type is not bounded.%ntype=%s%ntree=%s", type, tree);
    }
    checker.reportError(tree, "bound", label, type.toString(), upperBound.toString(true), lowerBound.toString(true));
    isValid = false;
}
Also used : BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 79 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class PerFileSuite method getParametersList.

/**
 * Returns a list of one-element arrays, each containing a Java File.
 */
@SuppressWarnings({ "unchecked", // JUnit needs to be annotated
"nullness" })
private List<Object[]> getParametersList(TestClass klass) throws Throwable {
    FrameworkMethod method = getParametersMethod(klass);
    List<File> javaFiles;
    // which returns List<Object []> or getParametersMethod would fail
    if (method == null) {
        throw new BugInCF("no method annotated with @Parameters");
    }
    if (method.getReturnType().isArray()) {
        String[] dirs = (String[]) method.invokeExplosively(null);
        javaFiles = TestUtilities.findNestedJavaTestFiles(dirs);
    } else {
        javaFiles = (List<File>) method.invokeExplosively(null);
    }
    List<Object[]> argumentLists = CollectionsPlume.mapList((File javaFile) -> new Object[] { javaFile }, javaFiles);
    return argumentLists;
}
Also used : BugInCF(org.checkerframework.javacutil.BugInCF) File(java.io.File) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 80 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class PerFileSuite method getParametersMethod.

/**
 * Returns method annotated @Parameters, typically the getTestDirs or getTestFiles method.
 */
private FrameworkMethod getParametersMethod(TestClass testClass) {
    final List<FrameworkMethod> parameterMethods = testClass.getAnnotatedMethods(Parameters.class);
    if (parameterMethods.size() != 1) {
        // Construct error message
        String methods;
        if (parameterMethods.isEmpty()) {
            methods = "[No methods specified]";
        } else {
            StringJoiner sj = new StringJoiner(", ");
            for (FrameworkMethod method : parameterMethods) {
                sj.add(method.getName());
            }
            methods = sj.toString();
        }
        throw new BugInCF(requiredFormsMessage, testClass.getName(), methods);
    }
    // else
    FrameworkMethod method = parameterMethods.get(0);
    Class<?> returnType = method.getReturnType();
    String methodName = method.getName();
    switch(methodName) {
        case "getTestDirs":
            if (returnType.isArray()) {
                if (returnType.getComponentType() != String.class) {
                    throw new RuntimeException("Component type of getTestDirs must be java.lang.String, found " + returnType.getComponentType().getCanonicalName());
                }
            }
            break;
        case "getTestFiles":
            // subtype thereof is not easy.
            if (!List.class.getCanonicalName().equals(returnType.getCanonicalName())) {
                throw new RuntimeException("getTestFiles must return a List<File>, found " + returnType);
            }
            break;
        default:
            throw new BugInCF(requiredFormsMessage, testClass.getName(), method);
    }
    int modifiers = method.getMethod().getModifiers();
    if (!Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
        throw new RuntimeException("Parameter method (" + method.getName() + ") must be public and static");
    }
    return method;
}
Also used : BugInCF(org.checkerframework.javacutil.BugInCF) FrameworkMethod(org.junit.runners.model.FrameworkMethod) StringJoiner(java.util.StringJoiner)

Aggregations

BugInCF (org.checkerframework.javacutil.BugInCF)127 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)29 ArrayList (java.util.ArrayList)28 AnnotationMirror (javax.lang.model.element.AnnotationMirror)26 TypeElement (javax.lang.model.element.TypeElement)26 TypeMirror (javax.lang.model.type.TypeMirror)25 ExecutableElement (javax.lang.model.element.ExecutableElement)24 MethodTree (com.sun.source.tree.MethodTree)20 ExpressionTree (com.sun.source.tree.ExpressionTree)18 VariableTree (com.sun.source.tree.VariableTree)18 Element (javax.lang.model.element.Element)18 ClassTree (com.sun.source.tree.ClassTree)17 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)17 NewClassTree (com.sun.source.tree.NewClassTree)17 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)16 IOException (java.io.IOException)16 Tree (com.sun.source.tree.Tree)15 Map (java.util.Map)15 List (java.util.List)14 VariableElement (javax.lang.model.element.VariableElement)14