Search in sources :

Example 11 with ArrayType

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

the class TreeBackedTypesTest method testIsNotSameTypeArrayType.

@Test
public void testIsNotSameTypeArrayType() throws IOException {
    compile(Joiner.on('\n').join("class Foo { }", "class Bar { }"));
    TypeMirror fooType = elements.getTypeElement("Foo").asType();
    TypeMirror barType = elements.getTypeElement("Bar").asType();
    ArrayType fooArray = types.getArrayType(fooType);
    ArrayType barArray = types.getArrayType(barType);
    assertNotSameType(fooArray, barArray);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) Test(org.junit.Test)

Example 12 with ArrayType

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

the class TypeResolverTest method testPrimitiveArrayTypeResolves.

@Test
public void testPrimitiveArrayTypeResolves() throws IOException {
    compile("abstract class Foo extends java.util.ArrayList<int[]> { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    TypeElement listElement = elements.getTypeElement("java.util.ArrayList");
    TypeMirror intType = types.getPrimitiveType(TypeKind.INT);
    ArrayType intArrayType = types.getArrayType(intType);
    DeclaredType expectedSuperclass = types.getDeclaredType(listElement, intArrayType);
    assertSameType(expectedSuperclass, fooElement.getSuperclass());
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 13 with ArrayType

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

the class TypeResolverTest method testArrayTypeResolves.

@Test
public void testArrayTypeResolves() throws IOException {
    compile("abstract class Foo extends java.util.ArrayList<java.lang.String[]> { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    TypeElement listElement = elements.getTypeElement("java.util.ArrayList");
    TypeMirror stringType = elements.getTypeElement("java.lang.String").asType();
    ArrayType stringArrayType = types.getArrayType(stringType);
    DeclaredType expectedSuperclass = types.getDeclaredType(listElement, stringArrayType);
    assertSameType(expectedSuperclass, fooElement.getSuperclass());
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType) Test(org.junit.Test)

Example 14 with ArrayType

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

the class TreeBackedTypesTest method testIsSameTypeArrayType.

@Test
public void testIsSameTypeArrayType() throws IOException {
    compile("class Foo { }");
    TypeElement fooElement = elements.getTypeElement("Foo");
    ArrayType fooArray = types.getArrayType(fooElement.asType());
    ArrayType fooArray2 = types.getArrayType(fooElement.asType());
    assertSameType(fooArray, fooArray2);
    assertNotSameType(fooArray, fooElement.asType());
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeElement(javax.lang.model.element.TypeElement) Test(org.junit.Test)

Example 15 with ArrayType

use of javax.lang.model.type.ArrayType in project auto by google.

the class AutoAnnotationProcessor method compatibleTypes.

/**
   * Returns true if {@code parameterType} can be used to provide the value of an annotation member
   * of type {@code memberType}. They must either be the same type, or the member type must be an
   * array and the parameter type must be a collection of a compatible type.
   */
private boolean compatibleTypes(TypeMirror parameterType, TypeMirror memberType) {
    if (typeUtils.isAssignable(parameterType, memberType)) {
        // is a subtype of that annotation interface (why would you do that?).
        return true;
    }
    // to Collection<Integer> (in this example).
    if (memberType.getKind() != TypeKind.ARRAY) {
        return false;
    }
    TypeMirror arrayElementType = ((ArrayType) memberType).getComponentType();
    TypeMirror wrappedArrayElementType = arrayElementType.getKind().isPrimitive() ? typeUtils.boxedClass((PrimitiveType) arrayElementType).asType() : arrayElementType;
    TypeElement javaUtilCollection = processingEnv.getElementUtils().getTypeElement(Collection.class.getCanonicalName());
    DeclaredType collectionOfElement = typeUtils.getDeclaredType(javaUtilCollection, wrappedArrayElementType);
    return typeUtils.isAssignable(parameterType, collectionOfElement);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Collection(java.util.Collection) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

ArrayType (javax.lang.model.type.ArrayType)24 TypeMirror (javax.lang.model.type.TypeMirror)18 DeclaredType (javax.lang.model.type.DeclaredType)11 TypeElement (javax.lang.model.element.TypeElement)9 Test (org.junit.Test)7 Expression (com.google.devtools.j2objc.ast.Expression)4 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 VariableElement (javax.lang.model.element.VariableElement)3 BindString (butterknife.BindString)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 Statement (com.google.devtools.j2objc.ast.Statement)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PrimitiveType (javax.lang.model.type.PrimitiveType)2