Search in sources :

Example 31 with BugInCF

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

the class ForwardAnalysisImpl method performAnalysis.

@Override
public void performAnalysis(ControlFlowGraph cfg) {
    if (isRunning) {
        throw new BugInCF("ForwardAnalysisImpl::performAnalysis() shouldn't be called when the analysis is" + " running.");
    }
    isRunning = true;
    try {
        init(cfg);
        while (!worklist.isEmpty()) {
            Block b = worklist.poll();
            performAnalysisBlock(b);
        }
    } finally {
        assert isRunning;
        // In case performAnalysisBlock crashed, reset isRunning to false.
        isRunning = false;
    }
}
Also used : ExceptionBlock(org.checkerframework.dataflow.cfg.block.ExceptionBlock) SpecialBlock(org.checkerframework.dataflow.cfg.block.SpecialBlock) Block(org.checkerframework.dataflow.cfg.block.Block) RegularBlock(org.checkerframework.dataflow.cfg.block.RegularBlock) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 32 with BugInCF

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

the class AnnotatedTypeFactory method getCheckerNames.

/**
 * Returns the names of the annotation processors that are being run.
 *
 * @return the names of the annotation processors that are being run
 */
// ClassLoader.getResources returns an Enumeration
@SuppressWarnings("JdkObsolete")
public String[] getCheckerNames() {
    com.sun.tools.javac.util.Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    String processorArg = Options.instance(context).get("-processor");
    if (processorArg != null) {
        return processorArg.split(",");
    }
    try {
        String filename = "META-INF/services/javax.annotation.processing.Processor";
        List<String> lines = new ArrayList<>();
        Enumeration<URL> urls = getClass().getClassLoader().getResources(filename);
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            lines.addAll(in.lines().collect(Collectors.toList()));
        }
        String[] result = lines.toArray(new String[0]);
        return result;
    } catch (IOException e) {
        throw new BugInCF(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BugInCF(org.checkerframework.javacutil.BugInCF) URL(java.net.URL) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) BufferedReader(java.io.BufferedReader)

Example 33 with BugInCF

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

the class AnnotatedTypeFactory method methodFromUse.

/**
 * Determines the type of the invoked method based on the passed expression tree, executable
 * element, and receiver type.
 *
 * @param tree either a MethodInvocationTree or a MemberReferenceTree
 * @param methodElt the element of the referenced method
 * @param receiverType the type of the receiver
 * @return the method type being invoked with tree and the (inferred) type arguments
 * @see #methodFromUse(MethodInvocationTree)
 */
public ParameterizedExecutableType methodFromUse(ExpressionTree tree, ExecutableElement methodElt, AnnotatedTypeMirror receiverType) {
    AnnotatedExecutableType memberTypeWithoutOverrides = // get unsubstituted type
    getAnnotatedType(methodElt);
    AnnotatedExecutableType memberTypeWithOverrides = applyFakeOverrides(receiverType, methodElt, memberTypeWithoutOverrides);
    memberTypeWithOverrides = applyRecordTypesToAccessors(methodElt, memberTypeWithOverrides);
    methodFromUsePreSubstitution(tree, memberTypeWithOverrides);
    AnnotatedExecutableType methodType = AnnotatedTypes.asMemberOf(types, this, receiverType, methodElt, memberTypeWithOverrides);
    List<AnnotatedTypeMirror> typeargs = new ArrayList<>(methodType.getTypeVariables().size());
    Map<TypeVariable, AnnotatedTypeMirror> typeParamToTypeArg = AnnotatedTypes.findTypeArguments(processingEnv, this, tree, methodElt, methodType);
    if (!typeParamToTypeArg.isEmpty()) {
        typeParamToTypeArg = captureMethodTypeArgs(typeParamToTypeArg, memberTypeWithOverrides.getTypeVariables());
        for (AnnotatedTypeVariable tv : methodType.getTypeVariables()) {
            if (typeParamToTypeArg.get(tv.getUnderlyingType()) == null) {
                throw new BugInCF("AnnotatedTypeFactory.methodFromUse:mismatch between declared method type variables" + " and the inferred method type arguments. Method type variables: " + methodType.getTypeVariables() + "; " + "Inferred method type arguments: " + typeParamToTypeArg);
            }
            typeargs.add(typeParamToTypeArg.get(tv.getUnderlyingType()));
        }
        methodType = (AnnotatedExecutableType) typeVarSubstitutor.substitute(typeParamToTypeArg, methodType);
    }
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION && TreeUtils.isMethodInvocation(tree, objectGetClass, processingEnv)) {
        adaptGetClassReturnTypeToReceiver(methodType, receiverType, tree);
    }
    return new ParameterizedExecutableType(methodType, typeargs);
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 34 with BugInCF

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

the class AnnotationFileParser method sameType.

/**
 * Returns true if the two types are the same.
 *
 * @param javacType type in javac form
 * @param javaParserType type in JavaParser form
 * @return true if the two types are the same
 */
private boolean sameType(TypeMirror javacType, Type javaParserType) {
    switch(javacType.getKind()) {
        case BOOLEAN:
            return javaParserType.equals(PrimitiveType.booleanType());
        case BYTE:
            return javaParserType.equals(PrimitiveType.byteType());
        case CHAR:
            return javaParserType.equals(PrimitiveType.charType());
        case DOUBLE:
            return javaParserType.equals(PrimitiveType.doubleType());
        case FLOAT:
            return javaParserType.equals(PrimitiveType.floatType());
        case INT:
            return javaParserType.equals(PrimitiveType.intType());
        case LONG:
            return javaParserType.equals(PrimitiveType.longType());
        case SHORT:
            return javaParserType.equals(PrimitiveType.shortType());
        case DECLARED:
        case TYPEVAR:
            if (!(javaParserType instanceof ClassOrInterfaceType)) {
                return false;
            }
            com.sun.tools.javac.code.Type javacTypeInternal = (com.sun.tools.javac.code.Type) javacType;
            ClassOrInterfaceType javaParserClassType = (ClassOrInterfaceType) javaParserType;
            // Use asString() because toString() includes annotations.
            String javaParserString = javaParserClassType.asString();
            Element javacElement = javacTypeInternal.asElement();
            // Check both fully-qualified name and simple name.
            return javacElement.toString().equals(javaParserString) || javacElement.getSimpleName().contentEquals(javaParserString);
        case ARRAY:
            return javaParserType.isArrayType() && sameType(((ArrayType) javacType).getComponentType(), javaParserType.asArrayType().getComponentType());
        default:
            throw new BugInCF("Unhandled type %s of kind %s", javacType, javacType.getKind());
    }
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) ArrayType(javax.lang.model.type.ArrayType) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) Type(com.github.javaparser.ast.type.Type) ArrayType(javax.lang.model.type.ArrayType) AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotationFileType(org.checkerframework.framework.stub.AnnotationFileUtil.AnnotationFileType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(com.github.javaparser.ast.type.WildcardType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReferenceType(com.github.javaparser.ast.type.ReferenceType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 35 with BugInCF

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

the class SourceChecker method shouldSuppressWarnings.

/**
 * Determines whether all the warnings pertaining to a given tree should be suppressed. Returns
 * true if the tree is within the scope of a @SuppressWarnings annotation, one of whose values
 * suppresses the checker's warnings. Also, returns true if the {@code errKey} matches a string in
 * {@code -AsuppressWarnings}.
 *
 * @param tree the tree that might be a source of a warning
 * @param errKey the error key the checker is emitting
 * @return true if no warning should be emitted for the given tree because it is contained by a
 *     declaration with an appropriately-valued {@literal @}SuppressWarnings annotation; false
 *     otherwise
 */
public boolean shouldSuppressWarnings(Tree tree, String errKey) {
    Collection<String> prefixes = getSuppressWarningsPrefixes();
    if (prefixes.isEmpty() || (prefixes.contains(SUPPRESS_ALL_PREFIX) && prefixes.size() == 1)) {
        throw new BugInCF("Checker must provide a SuppressWarnings prefix." + " SourceChecker#getSuppressWarningsPrefixes was not overridden correctly.");
    }
    if (shouldSuppress(getSuppressWarningsStringsFromOption(), errKey)) {
        return true;
    }
    if (shouldSuppress(getSuppressWarningsStringsFromOption(), errKey)) {
        // the warning.
        return true;
    }
    // trees.getPath might be slow, but this is only used in error reporting
    @Nullable TreePath path = trees.getPath(this.currentRoot, tree);
    @Nullable VariableTree var = TreePathUtil.enclosingVariable(path);
    if (var != null && shouldSuppressWarnings(TreeUtils.elementFromTree(var), errKey)) {
        return true;
    }
    @Nullable MethodTree method = TreePathUtil.enclosingMethod(path);
    if (method != null) {
        @Nullable Element elt = TreeUtils.elementFromTree(method);
        if (shouldSuppressWarnings(elt, errKey)) {
            return true;
        }
        if (isAnnotatedForThisCheckerOrUpstreamChecker(elt)) {
            // because they may not have an @AnnotatedFor.
            return false;
        }
    }
    @Nullable ClassTree cls = TreePathUtil.enclosingClass(path);
    if (cls != null) {
        @Nullable Element elt = TreeUtils.elementFromTree(cls);
        if (shouldSuppressWarnings(elt, errKey)) {
            return true;
        }
        if (isAnnotatedForThisCheckerOrUpstreamChecker(elt)) {
            // because they may not have an @AnnotatedFor.
            return false;
        }
    }
    if (useConservativeDefault("source")) {
        // false, we DO suppress the warning.
        return true;
    }
    return false;
}
Also used : TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableTree(com.sun.source.tree.VariableTree) ClassTree(com.sun.source.tree.ClassTree) BugInCF(org.checkerframework.javacutil.BugInCF) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

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