Search in sources :

Example 6 with UserError

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

the class SourceChecker method useConservativeDefault.

/**
 * Should conservative defaults be used for the kind of unchecked code indicated by the parameter?
 *
 * @param kindOfCode source or bytecode
 * @return whether conservative defaults should be used
 */
public boolean useConservativeDefault(String kindOfCode) {
    final boolean useUncheckedDefaultsForSource = false;
    final boolean useUncheckedDefaultsForByteCode = false;
    String option = this.getOption("useConservativeDefaultsForUncheckedCode");
    // Temporary, for backward compatibility.
    if (option == null) {
        this.getOption("useDefaultsForUncheckedCode");
    }
    String[] args = option != null ? option.split(",") : new String[0];
    for (String arg : args) {
        boolean value = arg.indexOf("-") != 0;
        arg = value ? arg : arg.substring(1);
        if (arg.equals(kindOfCode)) {
            return value;
        }
    }
    if (kindOfCode.equals("source")) {
        return useUncheckedDefaultsForSource;
    } else if (kindOfCode.equals("bytecode")) {
        return useUncheckedDefaultsForByteCode;
    } else {
        throw new UserError("SourceChecker: unexpected argument to useConservativeDefault: " + kindOfCode);
    }
}
Also used : UserError(org.checkerframework.javacutil.UserError)

Example 7 with UserError

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

the class DOTCFGVisualizer method shutdown.

/**
 * Write a file {@code methods.txt} that contains a mapping from source code location to generated
 * dot file.
 */
@Override
public void shutdown() {
    try {
        // Open for append, in case of multiple sub-checkers.
        FileWriter fstream = new FileWriter(outDir + "/methods.txt", true);
        BufferedWriter out = new BufferedWriter(fstream);
        for (Map.Entry<String, String> kv : generated.entrySet()) {
            out.write(kv.getKey());
            out.append("\t");
            out.write(kv.getValue());
            out.append(lineSeparator);
        }
        out.close();
    } catch (IOException e) {
        throw new UserError("Error creating methods.txt file in: " + outDir + "; ensure the path is valid", e);
    }
}
Also used : UserError(org.checkerframework.javacutil.UserError) FileWriter(java.io.FileWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) BufferedWriter(java.io.BufferedWriter)

Example 8 with UserError

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

the class FormatterVisitor method forwardsArguments.

/**
 * Returns true if {@code invocationTree}'s arguments are {@code enclosingMethod}'s formal
 * parameters. In other words, {@code invocationTree} forwards {@code enclosingMethod}'s
 * arguments.
 *
 * <p>Only arguments from the first String formal parameter onward count. Returns false if there
 * is no String formal parameter.
 *
 * @param invocationTree an invocation of a method
 * @param enclosingMethod the method that contains the call
 * @return true if {@code invocationTree} is a call to a method that forwards its containing
 *     method's arguments
 */
private boolean forwardsArguments(MethodInvocationTree invocationTree, @Nullable MethodTree enclosingMethod) {
    if (enclosingMethod == null) {
        return false;
    }
    ExecutableElement enclosingMethodElement = TreeUtils.elementFromDeclaration(enclosingMethod);
    int paramIndex = formatStringIndex(enclosingMethodElement);
    if (paramIndex == -1) {
        return false;
    }
    ExecutableElement calledMethodElement = TreeUtils.elementFromUse(invocationTree);
    int callIndex = formatStringIndex(calledMethodElement);
    if (callIndex == -1) {
        throw new UserError("Method " + calledMethodElement + " is annotated @FormatMethod but has no String formal parameter");
    }
    List<? extends ExpressionTree> args = invocationTree.getArguments();
    List<? extends VariableTree> params = enclosingMethod.getParameters();
    if (params.size() - paramIndex != args.size() - callIndex) {
        return false;
    }
    while (paramIndex < params.size()) {
        ExpressionTree argTree = args.get(callIndex);
        if (argTree.getKind() != Tree.Kind.IDENTIFIER) {
            return false;
        }
        VariableTree param = params.get(paramIndex);
        if (param.getName() != ((IdentifierTree) argTree).getName()) {
            return false;
        }
        paramIndex++;
        callIndex++;
    }
    return true;
}
Also used : UserError(org.checkerframework.javacutil.UserError) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableTree(com.sun.source.tree.VariableTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 9 with UserError

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

the class CalledMethodsAnnotatedTypeFactory method enableFrameworks.

/**
 * Enables support for the default builder-generation frameworks, except those listed in the
 * disabled builder frameworks parsed from the -AdisableBuilderFrameworkSupport option's
 * arguments. Throws a UserError if the user included an unsupported framework in the list of
 * frameworks to be disabled.
 *
 * @param disabledFrameworks the disabled builder frameworks
 */
private void enableFrameworks(String[] disabledFrameworks) {
    boolean enableAutoValueSupport = true;
    boolean enableLombokSupport = true;
    for (String framework : disabledFrameworks) {
        switch(framework) {
            case "autovalue":
                enableAutoValueSupport = false;
                break;
            case "lombok":
                enableLombokSupport = false;
                break;
            default:
                throw new UserError("Unsupported builder framework in -AdisableBuilderFrameworkSupports: " + framework);
        }
    }
    if (enableAutoValueSupport) {
        builderFrameworkSupports.add(new AutoValueSupport(this));
    }
    if (enableLombokSupport) {
        builderFrameworkSupports.add(new LombokSupport(this));
    }
}
Also used : UserError(org.checkerframework.javacutil.UserError) AutoValueSupport(org.checkerframework.checker.calledmethods.builder.AutoValueSupport) LombokSupport(org.checkerframework.checker.calledmethods.builder.LombokSupport)

Example 10 with UserError

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

the class SourceChecker method init.

@Override
public final synchronized void init(ProcessingEnvironment env) {
    ProcessingEnvironment unwrappedEnv = unwrapProcessingEnvironment(env);
    super.init(unwrappedEnv);
    // The processingEnvironment field will be set by the superclass's init method.
    // This is used to trigger AggregateChecker's setProcessingEnvironment.
    setProcessingEnvironment(unwrappedEnv);
    // Keep in sync with check in checker-framework/build.gradle and text in installation
    // section of manual.
    int jreVersion = SystemUtil.getJreVersion();
    if (jreVersion != 8 && jreVersion != 11 && jreVersion != 17) {
        message((hasOption("permitUnsupportedJdkVersion") ? Kind.NOTE : Kind.WARNING), "Use JDK 8, 11, or 17 to run the Checker Framework.  You are using version %d.", jreVersion);
    }
    if (!hasOption("warnUnneededSuppressionsExceptions")) {
        warnUnneededSuppressionsExceptions = null;
    } else {
        String warnUnneededSuppressionsExceptionsString = getOption("warnUnneededSuppressionsExceptions");
        if (warnUnneededSuppressionsExceptionsString == null) {
            throw new UserError("Must supply an argument to -AwarnUnneededSuppressionsExceptions");
        }
        try {
            warnUnneededSuppressionsExceptions = Pattern.compile(warnUnneededSuppressionsExceptionsString);
        } catch (PatternSyntaxException e) {
            throw new UserError("Argument to -AwarnUnneededSuppressionsExceptions is not a regular expression: " + e.getMessage());
        }
    }
    if (hasOption("printGitProperties")) {
        printGitProperties();
    }
}
Also used : UserError(org.checkerframework.javacutil.UserError) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

UserError (org.checkerframework.javacutil.UserError)16 IOException (java.io.IOException)4 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)3 FileWriter (java.io.FileWriter)3 Map (java.util.Map)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Context (com.sun.tools.javac.util.Context)2 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 BugInCF (org.checkerframework.javacutil.BugInCF)2 TypeSystemError (org.checkerframework.javacutil.TypeSystemError)2 AScene (scenelib.annotations.el.AScene)2 ExpressionTree (com.sun.source.tree.ExpressionTree)1 VariableTree (com.sun.source.tree.VariableTree)1 Source (com.sun.tools.javac.code.Source)1 DiagnosticSource (com.sun.tools.javac.util.DiagnosticSource)1 Log (com.sun.tools.javac.util.Log)1