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();
}
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);
}
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;
}
}
}
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);
}
}
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();
}
Aggregations