Search in sources :

Example 1 with SourceChecker

use of org.checkerframework.framework.source.SourceChecker in project checker-framework by typetools.

the class FactoryTestChecker method initChecker.

@Override
public void initChecker() {
    super.initChecker();
    // Find factory constructor
    String checkerClassName = getOption("checker");
    try {
        if (checkerClassName != null) {
            Class<?> checkerClass = Class.forName(checkerClassName);
            Constructor<?> constructor = checkerClass.getConstructor();
            Object o = constructor.newInstance();
            if (o instanceof SourceChecker) {
                checker = (SourceChecker) o;
            }
        }
    } catch (Exception e) {
        errorAbort("Couldn't load " + checkerClassName + " class.");
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) SourceChecker(org.checkerframework.framework.source.SourceChecker) IOException(java.io.IOException)

Example 2 with SourceChecker

use of org.checkerframework.framework.source.SourceChecker in project checker-framework by typetools.

the class DependentTypesHelper method reportErrors.

protected void reportErrors(Tree errorTree, List<DependentTypesError> errors) {
    if (errors.isEmpty()) {
        return;
    }
    SourceChecker checker = factory.getContext().getChecker();
    String error = PluginUtil.join("\n", errors);
    checker.report(Result.failure("expression.unparsable.type.invalid", error), errorTree);
}
Also used : SourceChecker(org.checkerframework.framework.source.SourceChecker)

Example 3 with SourceChecker

use of org.checkerframework.framework.source.SourceChecker in project checker-framework by typetools.

the class DefaultTypeArgumentInference method inferTypeArgs.

@Override
public Map<TypeVariable, AnnotatedTypeMirror> inferTypeArgs(AnnotatedTypeFactory typeFactory, ExpressionTree expressionTree, ExecutableElement methodElem, AnnotatedExecutableType methodType) {
    final List<AnnotatedTypeMirror> argTypes = TypeArgInferenceUtil.getArgumentTypes(expressionTree, typeFactory);
    final TreePath pathToExpression = typeFactory.getPath(expressionTree);
    assert pathToExpression != null;
    final AnnotatedTypeMirror assignedTo = TypeArgInferenceUtil.assignedTo(typeFactory, pathToExpression);
    SourceChecker checker = typeFactory.getContext().getChecker();
    if (showInferenceSteps) {
        checker.message(Kind.NOTE, "DTAI: expression: %s\n  argTypes: %s\n  assignedTo: %s\n", expressionTree.toString().replace("\n", " "), argTypes, assignedTo);
    }
    final Set<TypeVariable> targets = TypeArgInferenceUtil.methodTypeToTargets(methodType);
    if (assignedTo == null && TreeUtils.getAssignmentContext(pathToExpression) != null) {
        // If the type of the assignment context isn't found, but the expression is assigned,
        // then don't attempt to infere type arguments, because the Java type inferred will be
        // incorrect.  The assignment type is null when it includes uninferred type arguments.
        // For example:
        // <T> T outMethod()
        // <U> void inMethod(U u);
        // inMethod(outMethod())
        // would require solving the constraints for both type argument inferences
        // simultaneously
        Map<TypeVariable, AnnotatedTypeMirror> inferredArgs = new LinkedHashMap<>();
        handleUninferredTypeVariables(typeFactory, methodType, targets, inferredArgs);
        return inferredArgs;
    }
    Map<TypeVariable, AnnotatedTypeMirror> inferredArgs;
    try {
        inferredArgs = infer(typeFactory, argTypes, assignedTo, methodElem, methodType, targets, true);
        if (showInferenceSteps) {
            checker.message(Kind.NOTE, "  after infer: %s\n", inferredArgs);
        }
        handleNullTypeArguments(typeFactory, methodElem, methodType, argTypes, assignedTo, targets, inferredArgs);
        if (showInferenceSteps) {
            checker.message(Kind.NOTE, "  after handleNull: %s\n", inferredArgs);
        }
    } catch (Exception ex) {
        // Catch any errors thrown by inference.
        inferredArgs = new LinkedHashMap<>();
        if (showInferenceSteps) {
            checker.message(Kind.NOTE, "  exception: %s\n", ex.getLocalizedMessage());
        }
    }
    handleUninferredTypeVariables(typeFactory, methodType, targets, inferredArgs);
    if (showInferenceSteps) {
        checker.message(Kind.NOTE, "  results: %s\n", inferredArgs);
    }
    return inferredArgs;
}
Also used : TreePath(com.sun.source.util.TreePath) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) SourceChecker(org.checkerframework.framework.source.SourceChecker) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with SourceChecker

use of org.checkerframework.framework.source.SourceChecker in project checker-framework by typetools.

the class SignaturePrinter method init.

// /////// Initialization /////////////
private void init(ProcessingEnvironment env, String checkerName) {
    if (checkerName != null) {
        try {
            Class<?> checkerClass = Class.forName(checkerName);
            Constructor<?> cons = checkerClass.getConstructor();
            checker = (SourceChecker) cons.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        checker = new SourceChecker() {

            @Override
            protected SourceVisitor<?, ?> createSourceVisitor() {
                return null;
            }

            @Override
            public AnnotationProvider getAnnotationProvider() {
                throw new UnsupportedOperationException("getAnnotationProvider is not implemented for this class.");
            }
        };
    }
    checker.init(env);
}
Also used : SourceVisitor(org.checkerframework.framework.source.SourceVisitor) AnnotationProvider(org.checkerframework.javacutil.AnnotationProvider) SourceChecker(org.checkerframework.framework.source.SourceChecker)

Example 5 with SourceChecker

use of org.checkerframework.framework.source.SourceChecker in project checker-framework by typetools.

the class DependentTypesHelper method checkAnnotation.

/**
 * Checks every Java expression element of the annotation to see if the expression is an error
 * string as specified by DependentTypesError#isExpressionError. If any expression is an error,
 * then an error is reported at {@code errorTree}.
 *
 * @param annotation annotation to check
 * @param errorTree location at which to issue errors
 */
public void checkAnnotation(AnnotationMirror annotation, Tree errorTree) {
    List<DependentTypesError> errors = checkForError(annotation);
    if (errors.isEmpty()) {
        return;
    }
    SourceChecker checker = factory.getContext().getChecker();
    String error = PluginUtil.join("\n", errors);
    checker.report(Result.failure("flowexpr.parse.error", error), errorTree);
}
Also used : SourceChecker(org.checkerframework.framework.source.SourceChecker)

Aggregations

SourceChecker (org.checkerframework.framework.source.SourceChecker)5 TreePath (com.sun.source.util.TreePath)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 TypeVariable (javax.lang.model.type.TypeVariable)1 JavaFileObject (javax.tools.JavaFileObject)1 SourceVisitor (org.checkerframework.framework.source.SourceVisitor)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)1 AnnotationProvider (org.checkerframework.javacutil.AnnotationProvider)1