Search in sources :

Example 6 with TypeAnnotationPosition

use of com.sun.tools.javac.code.TypeAnnotationPosition in project checker-framework by typetools.

the class MethodApplier method applyThrowsAnnotations.

/**
 * For each thrown type, collect all the annotations for that type and apply them
 */
private void applyThrowsAnnotations(final List<Attribute.TypeCompound> annos) {
    final List<AnnotatedTypeMirror> thrown = methodType.getThrownTypes();
    if (thrown.isEmpty()) {
        return;
    }
    Map<AnnotatedTypeMirror, List<TypeCompound>> typeToAnnos = new LinkedHashMap<>();
    for (final AnnotatedTypeMirror thrownType : thrown) {
        typeToAnnos.put(thrownType, new ArrayList<>());
    }
    for (TypeCompound anno : annos) {
        final TypeAnnotationPosition annoPos = anno.position;
        if (annoPos.type_index >= 0 && annoPos.type_index < thrown.size()) {
            final AnnotatedTypeMirror thrownType = thrown.get(annoPos.type_index);
            typeToAnnos.get(thrownType).add(anno);
        } else {
            ErrorReporter.errorAbort("MethodApplier.applyThrowsAnnotation: " + "invalid throws index " + annoPos.type_index + " for annotation: " + anno + " for element: " + ElementUtils.getVerboseName(element));
        }
    }
    for (final Entry<AnnotatedTypeMirror, List<TypeCompound>> typeToAnno : typeToAnnos.entrySet()) {
        ElementAnnotationUtil.annotateViaTypeAnnoPosition(typeToAnno.getKey(), typeToAnno.getValue());
    }
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) ArrayList(java.util.ArrayList) List(java.util.List) TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with TypeAnnotationPosition

use of com.sun.tools.javac.code.TypeAnnotationPosition in project checker-framework by typetools.

the class TypeVarUseApplier method getVariableAnnos.

/**
 * @return annotations on an element that apply to variable declarations
 */
private static List<Attribute.TypeCompound> getVariableAnnos(final Element variableElem) {
    final VarSymbol varSymbol = (VarSymbol) variableElem;
    final List<Attribute.TypeCompound> annotations = new ArrayList<>();
    for (Attribute.TypeCompound anno : varSymbol.getRawTypeAttributes()) {
        TypeAnnotationPosition pos = anno.position;
        switch(pos.type) {
            case FIELD:
            case LOCAL_VARIABLE:
            case RESOURCE_VARIABLE:
            case EXCEPTION_PARAMETER:
                annotations.add(anno);
                break;
            default:
        }
    }
    return annotations;
}
Also used : Attribute(com.sun.tools.javac.code.Attribute) ArrayList(java.util.ArrayList) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition)

Example 8 with TypeAnnotationPosition

use of com.sun.tools.javac.code.TypeAnnotationPosition in project checker-framework by typetools.

the class TypesIntoElements method storeVariable.

private static void storeVariable(ProcessingEnvironment processingEnv, Types types, AnnotatedTypeFactory atypeFactory, VariableTree var) {
    VarSymbol sym = (VarSymbol) TreeUtils.elementFromDeclaration(var);
    AnnotatedTypeMirror type;
    if (atypeFactory instanceof GenericAnnotatedTypeFactory) {
        // TODO: this is rather ugly: we do not want refinement from the
        // initializer of the field. We need a general way to get
        // the "defaulted" type of a variable.
        type = ((GenericAnnotatedTypeFactory<?, ?, ?, ?>) atypeFactory).getAnnotatedTypeLhs(var);
    } else {
        type = atypeFactory.getAnnotatedType(var);
    }
    TypeAnnotationPosition tapos = TypeAnnotationUtils.fieldTAPosition(((JCTree) var).pos);
    List<Attribute.TypeCompound> tcs;
    tcs = generateTypeCompounds(processingEnv, type, tapos);
    addUniqueTypeCompounds(types, sym, tcs);
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition)

Example 9 with TypeAnnotationPosition

use of com.sun.tools.javac.code.TypeAnnotationPosition in project checker-framework by typetools.

the class TypesIntoElements method storeTypeParameters.

private static void storeTypeParameters(ProcessingEnvironment processingEnv, Types types, AnnotatedTypeFactory atypeFactory, java.util.List<? extends TypeParameterTree> tps, Symbol sym) {
    boolean isClassOrInterface = sym.getKind().isClass() || sym.getKind().isInterface();
    List<Attribute.TypeCompound> tcs = List.nil();
    int tpidx = 0;
    for (TypeParameterTree tp : tps) {
        AnnotatedTypeVariable typeVar = (AnnotatedTypeVariable) atypeFactory.getAnnotatedTypeFromTypeTree(tp);
        // System.out.println("The Type for type parameter " + tp + " is " + type);
        TypeAnnotationPosition tapos;
        // the bounds may not be explicit and we couldn't look up separate pos.
        if (isClassOrInterface) {
            tapos = TypeAnnotationUtils.typeParameterTAPosition(tpidx, ((JCTree) tp).pos);
        } else {
            tapos = TypeAnnotationUtils.methodTypeParameterTAPosition(tpidx, ((JCTree) tp).pos);
        }
        {
            // This block is essentially direct annotations, perhaps we should refactor that
            // method out
            List<Attribute.TypeCompound> res = List.nil();
            for (AnnotationMirror am : typeVar.getLowerBound().getAnnotations()) {
                Attribute.TypeCompound tc = TypeAnnotationUtils.createTypeCompoundFromAnnotationMirror(am, tapos, processingEnv);
                res = res.prepend(tc);
            }
            tcs = tcs.appendList(res);
        }
        AnnotatedTypeMirror tpbound = typeVar.getUpperBound();
        java.util.List<? extends AnnotatedTypeMirror> bounds;
        if (tpbound.getKind() == TypeKind.INTERSECTION) {
            bounds = ((AnnotatedTypeMirror.AnnotatedIntersectionType) tpbound).directSuperTypes();
        } else {
            bounds = List.of(tpbound);
        }
        int bndidx = 0;
        for (AnnotatedTypeMirror bound : bounds) {
            if (bndidx == 0 && ((Type) bound.getUnderlyingType()).isInterface()) {
                // If the first bound is an interface, there is an implicit java.lang.Object
                ++bndidx;
            }
            if (isClassOrInterface) {
                tapos = TypeAnnotationUtils.typeParameterBoundTAPosition(tpidx, bndidx, ((JCTree) tp).pos);
            } else {
                tapos = TypeAnnotationUtils.methodTypeParameterBoundTAPosition(tpidx, bndidx, ((JCTree) tp).pos);
            }
            tcs = tcs.appendList(generateTypeCompounds(processingEnv, bound, tapos));
            ++bndidx;
        }
        ++tpidx;
    }
    // System.out.println("Adding " + tcs + " to " + sym);
    addUniqueTypeCompounds(types, sym, tcs);
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) TypeParameterTree(com.sun.source.tree.TypeParameterTree) Attribute(com.sun.tools.javac.code.Attribute) JCTree(com.sun.tools.javac.tree.JCTree) AnnotationMirror(javax.lang.model.element.AnnotationMirror) List(com.sun.tools.javac.util.List) TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 10 with TypeAnnotationPosition

use of com.sun.tools.javac.code.TypeAnnotationPosition in project bazel by bazelbuild.

the class TypeAnnotationUtils method copyTAPosition8.

private static TypeAnnotationPosition copyTAPosition8(TypeAnnotationPosition tapos) throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
    TypeAnnotationPosition res = TypeAnnotationPosition.class.newInstance();
    res.isValidOffset = tapos.isValidOffset;
    TypeAnnotationPosition.class.getField("bound_index").set(res, tapos.bound_index);
    res.exception_index = tapos.exception_index;
    res.location = List.from(tapos.location);
    if (tapos.lvarIndex != null)
        res.lvarIndex = Arrays.copyOf(tapos.lvarIndex, tapos.lvarIndex.length);
    if (tapos.lvarLength != null)
        res.lvarLength = Arrays.copyOf(tapos.lvarLength, tapos.lvarLength.length);
    if (tapos.lvarOffset != null)
        res.lvarOffset = Arrays.copyOf(tapos.lvarOffset, tapos.lvarOffset.length);
    res.offset = tapos.offset;
    TypeAnnotationPosition.class.getField("onLambda").set(res, tapos.onLambda);
    TypeAnnotationPosition.class.getField("parameter_index").set(res, tapos.parameter_index);
    TypeAnnotationPosition.class.getField("pos").set(res, tapos.pos);
    TypeAnnotationPosition.class.getField("type").set(res, tapos.type);
    TypeAnnotationPosition.class.getField("type_index").set(res, tapos.type_index);
    return res;
}
Also used : TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition)

Aggregations

TypeAnnotationPosition (com.sun.tools.javac.code.TypeAnnotationPosition)18 TypeCompound (com.sun.tools.javac.code.Attribute.TypeCompound)5 Attribute (com.sun.tools.javac.code.Attribute)2 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)2 JCTree (com.sun.tools.javac.tree.JCTree)2 List (com.sun.tools.javac.util.List)2 ArrayList (java.util.ArrayList)2 TypeParameterTree (com.sun.source.tree.TypeParameterTree)1 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)1 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)1