use of javax.lang.model.type.TypeVariable in project buck by facebook.
the class StandaloneTypeVariableTest method testAsElement.
@Test
public void testAsElement() throws IOException {
compile("class Foo<T> { }");
TypeParameterElement tElement = elements.getTypeElement("Foo").getTypeParameters().get(0);
TypeVariable tVar = (TypeVariable) tElement.asType();
assertSame(tElement, tVar.asElement());
}
use of javax.lang.model.type.TypeVariable in project buck by facebook.
the class StandaloneTypeVariableTest method testGetUpperBoundMultipleBounds.
@Test
public void testGetUpperBoundMultipleBounds() throws IOException {
compile("class Foo<T extends java.lang.CharSequence & java.lang.Runnable> { }");
TypeMirror charSequenceType = elements.getTypeElement("java.lang.CharSequence").asType();
TypeMirror runnableType = elements.getTypeElement("java.lang.Runnable").asType();
TypeVariable tVar = (TypeVariable) elements.getTypeElement("Foo").getTypeParameters().get(0).asType();
IntersectionType upperBound = (IntersectionType) tVar.getUpperBound();
List<? extends TypeMirror> bounds = upperBound.getBounds();
assertSame(2, bounds.size());
assertSameType(charSequenceType, bounds.get(0));
assertSameType(runnableType, bounds.get(1));
}
use of javax.lang.model.type.TypeVariable in project buck by facebook.
the class StandaloneTypeVariableTest method testGetUpperBoundUnbounded.
@Test
public void testGetUpperBoundUnbounded() throws IOException {
compile("class Foo<T> { }");
TypeMirror objectType = elements.getTypeElement("java.lang.Object").asType();
TypeVariable tVar = (TypeVariable) elements.getTypeElement("Foo").getTypeParameters().get(0).asType();
assertSameType(objectType, tVar.getUpperBound());
}
use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.
the class CFTreeBuilder method createAnnotatedType.
private Tree createAnnotatedType(AnnotatedTypeMirror annotatedType) {
// Implementation based on com.sun.tools.javac.tree.TreeMaker.Type
// Convert the annotations from a set of AnnotationMirrors
// to a list of AnnotationTrees.
Set<AnnotationMirror> annotations = annotatedType.getAnnotations();
List<JCTree.JCAnnotation> annotationTrees = List.nil();
for (AnnotationMirror am : annotations) {
// TODO: what TypeAnnotationPosition should be used?
Attribute.TypeCompound typeCompound = TypeAnnotationUtils.createTypeCompoundFromAnnotationMirror(am, TypeAnnotationUtils.unknownTAPosition(), env);
JCTree.JCAnnotation annotationTree = maker.Annotation(typeCompound);
JCTree.JCAnnotation typeAnnotationTree = maker.TypeAnnotation(annotationTree.getAnnotationType(), annotationTree.getArguments());
typeAnnotationTree.attribute = typeCompound;
annotationTrees = annotationTrees.append(typeAnnotationTree);
}
// Convert the underlying type from a TypeMirror to an
// ExpressionTree and combine with the AnnotationTrees
// to form a ClassTree of kind ANNOTATION_TYPE.
Tree underlyingTypeTree;
switch(annotatedType.getKind()) {
case BYTE:
underlyingTypeTree = maker.TypeIdent(TypeTag.BYTE);
break;
case CHAR:
underlyingTypeTree = maker.TypeIdent(TypeTag.BYTE);
break;
case SHORT:
underlyingTypeTree = maker.TypeIdent(TypeTag.SHORT);
break;
case INT:
underlyingTypeTree = maker.TypeIdent(TypeTag.INT);
break;
case LONG:
underlyingTypeTree = maker.TypeIdent(TypeTag.LONG);
break;
case FLOAT:
underlyingTypeTree = maker.TypeIdent(TypeTag.FLOAT);
break;
case DOUBLE:
underlyingTypeTree = maker.TypeIdent(TypeTag.DOUBLE);
break;
case BOOLEAN:
underlyingTypeTree = maker.TypeIdent(TypeTag.BOOLEAN);
break;
case VOID:
underlyingTypeTree = maker.TypeIdent(TypeTag.VOID);
break;
case TYPEVAR:
{
// No recursive annotations.
AnnotatedTypeMirror.AnnotatedTypeVariable variable = (AnnotatedTypeMirror.AnnotatedTypeVariable) annotatedType;
TypeVariable underlyingTypeVar = variable.getUnderlyingType();
underlyingTypeTree = maker.Ident((Symbol.TypeSymbol) (underlyingTypeVar).asElement());
break;
}
case WILDCARD:
{
AnnotatedTypeMirror.AnnotatedWildcardType wildcard = (AnnotatedTypeMirror.AnnotatedWildcardType) annotatedType;
WildcardType wildcardType = wildcard.getUnderlyingType();
if (wildcardType.getExtendsBound() != null) {
Tree annotatedExtendsBound = createAnnotatedType(wildcard.getExtendsBound());
underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.EXTENDS), (JCTree) annotatedExtendsBound);
} else if (wildcardType.getSuperBound() != null) {
Tree annotatedSuperBound = createAnnotatedType(wildcard.getSuperBound());
underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.SUPER), (JCTree) annotatedSuperBound);
} else {
underlyingTypeTree = maker.Wildcard(maker.TypeBoundKind(BoundKind.UNBOUND), null);
}
break;
}
case DECLARED:
{
underlyingTypeTree = maker.Type((Type) annotatedType.getUnderlyingType());
if (underlyingTypeTree instanceof JCTree.JCTypeApply) {
// Replace the type parameters with annotated versions.
AnnotatedTypeMirror.AnnotatedDeclaredType annotatedDeclaredType = (AnnotatedTypeMirror.AnnotatedDeclaredType) annotatedType;
List<JCTree.JCExpression> typeArgTrees = List.nil();
for (AnnotatedTypeMirror arg : annotatedDeclaredType.getTypeArguments()) {
typeArgTrees = typeArgTrees.append((JCTree.JCExpression) createAnnotatedType(arg));
}
JCTree.JCExpression clazz = (JCTree.JCExpression) ((JCTree.JCTypeApply) underlyingTypeTree).getType();
underlyingTypeTree = maker.TypeApply(clazz, typeArgTrees);
}
break;
}
case ARRAY:
{
AnnotatedTypeMirror.AnnotatedArrayType annotatedArrayType = (AnnotatedTypeMirror.AnnotatedArrayType) annotatedType;
Tree annotatedComponentTree = createAnnotatedType(annotatedArrayType.getComponentType());
underlyingTypeTree = maker.TypeArray((JCTree.JCExpression) annotatedComponentTree);
break;
}
case ERROR:
underlyingTypeTree = maker.TypeIdent(TypeTag.ERROR);
break;
default:
assert false : "unexpected type: " + annotatedType;
underlyingTypeTree = null;
break;
}
((JCTree) underlyingTypeTree).setType((Type) annotatedType.getUnderlyingType());
if (annotationTrees.isEmpty()) {
return underlyingTypeTree;
}
JCTree.JCAnnotatedType annotatedTypeTree = maker.AnnotatedType(annotationTrees, (JCTree.JCExpression) underlyingTypeTree);
annotatedTypeTree.setType((Type) annotatedType.getUnderlyingType());
return annotatedTypeTree;
}
use of javax.lang.model.type.TypeVariable in project checker-framework by typetools.
the class DefaultTypeHierarchy method visitWildcardSubtype.
protected boolean visitWildcardSubtype(AnnotatedWildcardType subtype, AnnotatedTypeMirror supertype, VisitHistory visited) {
if (subtype.isUninferredTypeArgument()) {
return subtype.atypeFactory.ignoreUninferredTypeArguments;
}
TypeMirror superTypeMirror = supertype.getUnderlyingType();
if (supertype.getKind() == TypeKind.TYPEVAR) {
TypeVariable atv = (TypeVariable) supertype.getUnderlyingType();
if (TypesUtils.isCaptured(atv)) {
superTypeMirror = TypesUtils.getCapturedWildcard(atv);
}
}
if (superTypeMirror.getKind() == TypeKind.WILDCARD) {
// This can happen at a method invocation where a type variable in the method
// declaration is substituted with a wildcard.
// For example:
// <T> void method(Gen<T> t) {}
// Gen<?> x;
// method(x); // this method is called when checking this method call
// And also when checking lambdas
boolean subtypeHasAnno = subtype.getAnnotationInHierarchy(currentTop) != null;
boolean supertypeHasAnno = supertype.getAnnotationInHierarchy(currentTop) != null;
if (subtypeHasAnno && supertypeHasAnno) {
// as the bounds are the same
return isPrimarySubtype(subtype, supertype, true);
} else if (!subtypeHasAnno && !supertypeHasAnno && areEqualInHierarchy(subtype, supertype, currentTop)) {
// Two unannotated uses of wildcard types are the same type
return true;
}
}
return isSubtype(subtype.getExtendsBound(), supertype, visited);
}
Aggregations