Search in sources :

Example 16 with BugInCF

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

the class TypeVarUseApplier method getParameterAnnos.

/**
 * Currently, the metadata for storing annotations (i.e. the Attribute.TypeCompounds) is null for
 * binary-only parameters and type parameters. However, it is present on the method. So in order
 * to ensure that we correctly retrieve the annotations we need to index from the method and
 * retrieve the annotations from its metadata.
 *
 * @return a list of annotations that were found on METHOD_FORMAL_PARAMETERS that match the
 *     parameter index of the input element in the parent methods formal parameter list
 */
private static List<Attribute.TypeCompound> getParameterAnnos(final Element paramElem) {
    final Element enclosingElement = paramElem.getEnclosingElement();
    if (!(enclosingElement instanceof ExecutableElement)) {
        throw new BugInCF("Bad element passed to TypeFromElement.getTypeParameterAnnotationAttributes: " + "element: " + paramElem + " not found in enclosing executable: " + enclosingElement);
    }
    final MethodSymbol enclosingMethod = (MethodSymbol) enclosingElement;
    if (enclosingMethod.getKind() != ElementKind.CONSTRUCTOR && enclosingMethod.getKind() != ElementKind.METHOD) {
        // Initializer blocks don't have parameters, so there is nothing to do.
        return Collections.emptyList();
    }
    // TODO: for the parameter in a lambda expression, the enclosingMethod isn't
    // the lambda expression. Does this read the correct annotations?
    final int paramIndex = enclosingMethod.getParameters().indexOf(paramElem);
    final List<Attribute.TypeCompound> annotations = enclosingMethod.getRawTypeAttributes();
    final List<Attribute.TypeCompound> result = new ArrayList<>();
    for (final Attribute.TypeCompound typeAnno : annotations) {
        if (typeAnno.position.type == TargetType.METHOD_FORMAL_PARAMETER) {
            if (typeAnno.position.parameter_index == paramIndex) {
                result.add(typeAnno);
            }
        }
    }
    return result;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Attribute(com.sun.tools.javac.code.Attribute) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 17 with BugInCF

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

the class TypeVarUseApplier method getReturnAnnos.

/**
 * Returns the annotations on the return type of the input ExecutableElement.
 *
 * @param methodElem the method whose return type annotations to return
 * @return the annotations on the return type of the input ExecutableElement
 */
private static List<Attribute.TypeCompound> getReturnAnnos(final Element methodElem) {
    if (!(methodElem instanceof ExecutableElement)) {
        throw new BugInCF("Bad element passed to TypeVarUseApplier.getReturnAnnos:" + methodElem);
    }
    final MethodSymbol enclosingMethod = (MethodSymbol) methodElem;
    final List<Attribute.TypeCompound> annotations = enclosingMethod.getRawTypeAttributes();
    final List<Attribute.TypeCompound> result = new ArrayList<>();
    for (final Attribute.TypeCompound typeAnno : annotations) {
        if (typeAnno.position.type == TargetType.METHOD_RETURN) {
            result.add(typeAnno);
        }
    }
    return result;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Attribute(com.sun.tools.javac.code.Attribute) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 18 with BugInCF

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

the class SceneToStubWriter method printClassDefinitions.

/**
 * Print the hierarchy of outer classes up to and including the given class, and return the number
 * of curly braces to close with. The classes are printed with appropriate opening curly braces,
 * in standard Java style.
 *
 * <p>In an AScene, an inner class name is a binary name like "Outer$Inner". In a stub file, inner
 * classes must be nested, as in Java source code.
 *
 * @param basename the binary name of the class without the package part
 * @param aClass the AClass for {@code basename}
 * @param printWriter the writer where the class definition should be printed
 * @param checker the type-checker whose annotations are being written
 * @return the number of outer classes within which this class is nested
 */
private static int printClassDefinitions(String basename, AClass aClass, PrintWriter printWriter, BaseTypeChecker checker) {
    String[] classNames = basename.split("\\$");
    TypeElement innermostTypeElt = aClass.getTypeElement();
    if (innermostTypeElt == null) {
        throw new BugInCF("typeElement was unexpectedly null in this aClass: " + aClass);
    }
    TypeElement[] typeElements = getTypeElementsForClasses(innermostTypeElt, classNames);
    for (int i = 0; i < classNames.length; i++) {
        String nameToPrint = classNames[i];
        if (i == classNames.length - 1) {
            printWriter.print(indents(i));
            printWriter.println("@AnnotatedFor(\"" + checker.getClass().getCanonicalName() + "\")");
        }
        printWriter.print(indents(i));
        if (aClass.isEnum(nameToPrint)) {
            printWriter.print("enum ");
        } else {
            printWriter.print("class ");
        }
        if (i == classNames.length - 1) {
            // Only print class annotations on the innermost class, which corresponds to aClass.
            // If there should be class annotations on another class, it will have its own stub
            // file, which will eventually be merged with this one.
            printWriter.print(formatAnnotations(aClass.getAnnotations()));
        }
        printWriter.print(nameToPrint);
        printTypeParameters(typeElements[i], printWriter);
        printWriter.println(" {");
        if (aClass.isEnum(nameToPrint) && i != classNames.length - 1) {
            // Print a blank set of enum constants if this is an outer enum.
            printWriter.println(indents(i + 1) + "/* omitted enum constants */ ;");
        }
        printWriter.println();
    }
    return classNames.length;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ATypeElement(scenelib.annotations.el.ATypeElement) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 19 with BugInCF

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

the class WholeProgramInferenceImplementation method updateFromFieldAssignment.

@Override
public void updateFromFieldAssignment(Node lhs, Node rhs) {
    Element element;
    String fieldName;
    if (lhs instanceof FieldAccessNode) {
        element = ((FieldAccessNode) lhs).getElement();
        fieldName = ((FieldAccessNode) lhs).getFieldName();
    } else if (lhs instanceof LocalVariableNode) {
        element = ((LocalVariableNode) lhs).getElement();
        fieldName = ((LocalVariableNode) lhs).getName();
    } else {
        throw new BugInCF("updateFromFieldAssignment received an unexpected node type: " + lhs.getClass());
    }
    // TODO: For a primitive such as long, this is yielding just @GuardedBy rather than
    // @GuardedBy({}).
    AnnotatedTypeMirror rhsATM = atypeFactory.getAnnotatedType(rhs.getTree());
    atypeFactory.wpiAdjustForUpdateField(lhs.getTree(), element, fieldName, rhsATM);
    updateFieldFromType(lhs.getTree(), element, fieldName, rhsATM);
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) BugInCF(org.checkerframework.javacutil.BugInCF) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 20 with BugInCF

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

the class WholeProgramInferenceScenesStorage method writeResultsToFile.

@Override
public void writeResultsToFile(WholeProgramInference.OutputFormat outputFormat, BaseTypeChecker checker) {
    if (outputFormat == OutputFormat.AJAVA) {
        throw new BugInCF("WholeProgramInferenceScenes used with format " + outputFormat);
    }
    for (String file : modifiedScenes) {
        ASceneWrapper scene = scenes.get(file);
        prepareSceneForWriting(scene.getAScene());
    }
    writeScenes(outputFormat, checker);
}
Also used : ASceneWrapper(org.checkerframework.common.wholeprograminference.scenelib.ASceneWrapper) BugInCF(org.checkerframework.javacutil.BugInCF)

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