Search in sources :

Example 6 with TypeVariable

use of javax.lang.model.type.TypeVariable in project buck by facebook.

the class CompilerTreeApiTest method getTypeParameterUpperBound.

protected TypeMirror getTypeParameterUpperBound(String typeName, int typeParameterIndex) {
    TypeParameterElement typeParameter = elements.getTypeElement(typeName).getTypeParameters().get(typeParameterIndex);
    TypeVariable typeVariable = (TypeVariable) typeParameter.asType();
    return typeVariable.getUpperBound();
}
Also used : TypeVariable(javax.lang.model.type.TypeVariable) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 7 with TypeVariable

use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.

the class TypeArgInferenceUtil method containsUninferredTypeParameter.

/**
 * @return true if the type contains a use of a type variable from methodType
 */
private static boolean containsUninferredTypeParameter(AnnotatedTypeMirror type, AnnotatedExecutableType methodType) {
    final List<AnnotatedTypeVariable> annotatedTypeVars = methodType.getTypeVariables();
    final List<TypeVariable> typeVars = new ArrayList<>(annotatedTypeVars.size());
    for (AnnotatedTypeVariable annotatedTypeVar : annotatedTypeVars) {
        typeVars.add((TypeVariable) TypeAnnotationUtils.unannotatedType(annotatedTypeVar.getUnderlyingType()));
    }
    return containsTypeParameter(type, typeVars);
}
Also used : AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) ArrayList(java.util.ArrayList) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)

Example 8 with TypeVariable

use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.

the class TypeFromTypeTreeVisitor method forTypeVariable.

private AnnotatedTypeMirror forTypeVariable(AnnotatedTypeMirror type, AnnotatedTypeFactory f) {
    if (type.getKind() != TypeKind.TYPEVAR) {
        ErrorReporter.errorAbort("TypeFromTree.forTypeVariable: should only be called on type variables");
        // dead code
        return null;
    }
    TypeVariable typeVar = (TypeVariable) type.getUnderlyingType();
    TypeParameterElement tpe = (TypeParameterElement) typeVar.asElement();
    Element elt = tpe.getGenericElement();
    if (elt instanceof TypeElement) {
        TypeElement typeElt = (TypeElement) elt;
        int idx = typeElt.getTypeParameters().indexOf(tpe);
        ClassTree cls = (ClassTree) f.declarationFromElement(typeElt);
        if (cls != null) {
            // `forTypeVariable` is called for Identifier, MemberSelect and UnionType trees,
            // none of which are declarations.  But `cls.getTypeParameters()` returns a list
            // of type parameter declarations (`TypeParameterTree`), so this recursive call
            // to `visit` will return a declaration ATV.  So we must copy the result and set
            // its `isDeclaration` field to `false`.
            AnnotatedTypeMirror result = visit(cls.getTypeParameters().get(idx), f).shallowCopy();
            ((AnnotatedTypeVariable) result).setDeclaration(false);
            return result;
        } else {
            // We already have all info from the element -> nothing to do.
            return type;
        }
    } else if (elt instanceof ExecutableElement) {
        ExecutableElement exElt = (ExecutableElement) elt;
        int idx = exElt.getTypeParameters().indexOf(tpe);
        MethodTree meth = (MethodTree) f.declarationFromElement(exElt);
        if (meth != null) {
            // This works the same as the case above.  Even though `meth` itself is not a
            // type declaration tree, the elements of `meth.getTypeParameters()` still are.
            AnnotatedTypeMirror result = visit(meth.getTypeParameters().get(idx), f).shallowCopy();
            ((AnnotatedTypeVariable) result).setDeclaration(false);
            return result;
        } else {
            // " + elt);
            return type;
        }
    } else {
        // not an element at all, namely Symtab.noSymbol.
        if (TypesUtils.isCaptured(typeVar)) {
            return type;
        } else {
            ErrorReporter.errorAbort("TypeFromTree.forTypeVariable: not a supported element: " + elt);
            // dead code
            return null;
        }
    }
}
Also used : AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) MethodTree(com.sun.source.tree.MethodTree) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassTree(com.sun.source.tree.ClassTree) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 9 with TypeVariable

use of javax.lang.model.type.TypeVariable in project graal by oracle.

the class NodeIntrinsicVerifier method verify.

@Override
public void verify(Element element, AnnotationMirror annotation, PluginGenerator generator) {
    if (element.getKind() != ElementKind.METHOD) {
        assert false : "Element is guaranteed to be a method.";
        return;
    }
    ExecutableElement intrinsicMethod = (ExecutableElement) element;
    if (!intrinsicMethod.getModifiers().contains(Modifier.STATIC)) {
        env.getMessager().printMessage(Kind.ERROR, String.format("A @%s method must be static.", NodeIntrinsic.class.getSimpleName()), element, annotation);
    }
    if (!intrinsicMethod.getModifiers().contains(Modifier.NATIVE)) {
        env.getMessager().printMessage(Kind.ERROR, String.format("A @%s method must be native.", NodeIntrinsic.class.getSimpleName()), element, annotation);
    }
    TypeMirror nodeClassMirror = resolveAnnotationValue(TypeMirror.class, findAnnotationValue(annotation, NODE_CLASS_NAME));
    TypeElement nodeClass = (TypeElement) env.getTypeUtils().asElement(nodeClassMirror);
    if (nodeClass.getSimpleName().contentEquals(NodeIntrinsic.class.getSimpleName())) {
        // default value
        Element enclosingElement = intrinsicMethod.getEnclosingElement();
        while (enclosingElement != null && enclosingElement.getKind() != ElementKind.CLASS) {
            enclosingElement = enclosingElement.getEnclosingElement();
        }
        if (enclosingElement != null) {
            nodeClass = (TypeElement) enclosingElement;
        }
    }
    TypeMirror returnType = intrinsicMethod.getReturnType();
    if (returnType instanceof TypeVariable) {
        env.getMessager().printMessage(Kind.ERROR, "@NodeIntrinsic cannot have a generic return type.", element, annotation);
    }
    boolean injectedStampIsNonNull = intrinsicMethod.getAnnotation(NodeIntrinsic.class).injectedStampIsNonNull();
    if (returnType.getKind() == TypeKind.VOID) {
        for (VariableElement parameter : intrinsicMethod.getParameters()) {
            if (parameter.getAnnotation(InjectedNodeParameter.class) != null) {
                env.getMessager().printMessage(Kind.ERROR, "@NodeIntrinsic with an injected Stamp parameter cannot have a void return type.", element, annotation);
                break;
            }
        }
    }
    TypeMirror[] constructorSignature = constructorSignature(intrinsicMethod);
    Map<ExecutableElement, String> nonMatches = new HashMap<>();
    List<ExecutableElement> factories = findIntrinsifyFactoryMethod(nodeClass, constructorSignature, nonMatches, injectedStampIsNonNull);
    List<ExecutableElement> constructors = Collections.emptyList();
    if (nodeClass.getModifiers().contains(Modifier.ABSTRACT)) {
        if (factories.isEmpty()) {
            env.getMessager().printMessage(Kind.ERROR, String.format("Cannot make a node intrinsic for an abstract class %s.", nodeClass.getSimpleName()), element, annotation);
        }
    } else if (!isNodeType(nodeClass)) {
        if (factories.isEmpty()) {
            env.getMessager().printMessage(Kind.ERROR, String.format("%s is not a subclass of %s.", nodeClass.getSimpleName(), nodeType()), element, annotation);
        }
    } else {
        TypeMirror ret = returnType;
        if (env.getTypeUtils().isAssignable(ret, structuralInputType())) {
            checkInputType(nodeClass, ret, element, annotation);
        }
        constructors = findConstructors(nodeClass, constructorSignature, nonMatches, injectedStampIsNonNull);
    }
    Formatter msg = new Formatter();
    if (factories.size() > 1) {
        msg.format("Found more than one factory in %s matching node intrinsic:", nodeClass);
        for (ExecutableElement candidate : factories) {
            msg.format("%n  %s", candidate);
        }
        env.getMessager().printMessage(Kind.ERROR, msg.toString(), intrinsicMethod, annotation);
    } else if (constructors.size() > 1) {
        msg.format("Found more than one constructor in %s matching node intrinsic:", nodeClass);
        for (ExecutableElement candidate : constructors) {
            msg.format("%n  %s", candidate);
        }
        env.getMessager().printMessage(Kind.ERROR, msg.toString(), intrinsicMethod, annotation);
    } else if (factories.size() == 1) {
        generator.addPlugin(new GeneratedNodeIntrinsicPlugin.CustomFactoryPlugin(intrinsicMethod, factories.get(0), constructorSignature));
    } else if (constructors.size() == 1) {
        generator.addPlugin(new GeneratedNodeIntrinsicPlugin.ConstructorPlugin(intrinsicMethod, constructors.get(0), constructorSignature));
    } else {
        msg.format("Could not find any factories or constructors in %s matching node intrinsic", nodeClass);
        if (!nonMatches.isEmpty()) {
            msg.format("%nFactories and constructors that failed to match:");
            for (Map.Entry<ExecutableElement, String> e : nonMatches.entrySet()) {
                msg.format("%n  %s: %s", e.getKey(), e.getValue());
            }
        }
        env.getMessager().printMessage(Kind.ERROR, msg.toString(), intrinsicMethod, annotation);
    }
}
Also used : InjectedNodeParameter(org.graalvm.compiler.graph.Node.InjectedNodeParameter) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Formatter(java.util.Formatter) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) NodeIntrinsic(org.graalvm.compiler.graph.Node.NodeIntrinsic)

Example 10 with TypeVariable

use of javax.lang.model.type.TypeVariable in project mule by mulesoft.

the class IntrospectionUtils method getInterfaceGenerics.

public static List<TypeMirror> getInterfaceGenerics(TypeMirror type, TypeElement implementedInterface, ProcessingEnvironment processingEnvironment) {
    Types types = processingEnvironment.getTypeUtils();
    TypeMirror erasure = types.erasure(implementedInterface.asType());
    TypeMirror searchType = types.erasure(type);
    if (types.isSameType(erasure, searchType)) {
        if (type instanceof DeclaredType) {
            return (List<TypeMirror>) ((DeclaredType) type).getTypeArguments();
        }
    }
    for (TypeMirror interfaceType : ((TypeElement) types.asElement(type)).getInterfaces()) {
        if (types.isAssignable(types.erasure(interfaceType), erasure)) {
            List<TypeMirror> generics = new ArrayList<>();
            List<? extends TypeMirror> typeArguments = ((DeclaredType) interfaceType).getTypeArguments();
            for (TypeMirror typeArgument : typeArguments) {
                if (typeArgument instanceof DeclaredType) {
                    generics.add(typeArgument);
                } else if (typeArgument instanceof TypeVariable) {
                    DeclaredType declaredType = (DeclaredType) ((DeclaredType) type).asElement().asType();
                    List<? extends TypeMirror> typeTypeArguments = declaredType.getTypeArguments();
                    int i = typeTypeArguments.indexOf(typeArgument);
                    generics.add(((DeclaredType) type).getTypeArguments().get(i));
                }
            }
            return generics;
        }
    }
    if (type instanceof DeclaredType) {
        Element element = ((DeclaredType) type).asElement();
        if (element instanceof TypeElement) {
            TypeMirror superclass = ((TypeElement) element).getSuperclass();
            if (types.isAssignable(types.erasure(superclass), erasure)) {
                return getInterfaceGenerics(superclass, implementedInterface, processingEnvironment);
            }
        }
    }
    return emptyList();
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) TypeElement(javax.lang.model.element.TypeElement) FieldElement(org.mule.runtime.module.extension.api.loader.java.type.FieldElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) VariableElement(javax.lang.model.element.VariableElement) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) Collectors.toImmutableList(org.mule.runtime.api.util.collection.Collectors.toImmutableList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) Collectors.toList(java.util.stream.Collectors.toList) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

TypeVariable (javax.lang.model.type.TypeVariable)80 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)38 TypeMirror (javax.lang.model.type.TypeMirror)30 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)22 TypeElement (javax.lang.model.element.TypeElement)21 ArrayList (java.util.ArrayList)16 DeclaredType (javax.lang.model.type.DeclaredType)15 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)13 Map (java.util.Map)13 ExecutableElement (javax.lang.model.element.ExecutableElement)12 TypeParameterElement (javax.lang.model.element.TypeParameterElement)12 Test (org.junit.Test)10 ArrayType (javax.lang.model.type.ArrayType)9 WildcardType (javax.lang.model.type.WildcardType)9 AnnotationMirror (javax.lang.model.element.AnnotationMirror)8 Element (javax.lang.model.element.Element)8 AnnotationMirrorMap (org.checkerframework.framework.util.AnnotationMirrorMap)8 AnnotationMirrorSet (org.checkerframework.framework.util.AnnotationMirrorSet)8 VariableElement (javax.lang.model.element.VariableElement)7