Search in sources :

Example 1 with ArrayType

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

the class StandaloneArrayTypeTest method testToString.

@Test
public void testToString() throws IOException {
    initCompiler();
    ArrayType stringArray = types.getArrayType(elements.getTypeElement("java.lang.String").asType());
    assertEquals("java.lang.String[]", stringArray.toString());
}
Also used : ArrayType(javax.lang.model.type.ArrayType) Test(org.junit.Test)

Example 2 with ArrayType

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

the class TypeResolverTest method testMultiDimArrayTypeResolves.

@Test
public void testMultiDimArrayTypeResolves() 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);
    ArrayType stringArrayArrayType = types.getArrayType(stringArrayType);
    DeclaredType expectedSuperclass = types.getDeclaredType(listElement, stringArrayArrayType);
    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 3 with ArrayType

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

the class TreeBackedTypesTest method testIsNotSameTypeDifferentTypes.

@Test
public void testIsNotSameTypeDifferentTypes() throws IOException {
    initCompiler();
    PrimitiveType intType = types.getPrimitiveType(TypeKind.INT);
    ArrayType intArrayType = types.getArrayType(intType);
    assertNotSameType(intType, intArrayType);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) PrimitiveType(javax.lang.model.type.PrimitiveType) Test(org.junit.Test)

Example 4 with ArrayType

use of javax.lang.model.type.ArrayType in project androidannotations by androidannotations.

the class RestAnnotationHelper method checkIfParameterizedTypeReferenceShouldBeUsed.

private boolean checkIfParameterizedTypeReferenceShouldBeUsed(TypeMirror returnType) {
    switch(returnType.getKind()) {
        case DECLARED:
            return !((DeclaredType) returnType).getTypeArguments().isEmpty();
        case ARRAY:
            ArrayType arrayType = (ArrayType) returnType;
            TypeMirror componentType = arrayType.getComponentType();
            return checkIfParameterizedTypeReferenceShouldBeUsed(componentType);
    }
    return false;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 5 with ArrayType

use of javax.lang.model.type.ArrayType in project androidannotations by androidannotations.

the class RestAnnotationHelper method resolveResponseClass.

/**
	 * Resolve the expected class for the input type according to the following
	 * rules :
	 * <ul>
	 * <li>The type is a primitive : Directly return the JClass as usual</li>
	 * <li>The type is NOT a generics : Directly return the JClass as usual</li>
	 * <li>The type is a generics and enclosing type is a class C&lt;T&gt; :
	 * Generate a subclass of C&lt;T&gt; and return it</li>
	 * <li>The type is a generics and enclosing type is an interface I&lt;T&gt;
	 * : Looking the inheritance tree, then</li>
	 * <ol>
	 * <li>One of the parent is a {@link java.util.Map Map} : Generate a
	 * subclass of {@link LinkedHashMap}&lt;T&gt; one and return it</li>
	 * <li>One of the parent is a {@link Set} : Generate a subclass of
	 * {@link TreeSet}&lt;T&gt; one and return it</li>
	 * <li>One of the parent is a {@link java.util.Collection Collection} :
	 * Generate a subclass of {@link ArrayList}&lt;T&gt; one and return it</li>
	 * <li>Return {@link Object} definition</li>
	 * </ol>
	 * </ul>
	 *
	 */
private AbstractJClass resolveResponseClass(TypeMirror expectedType, RestHolder holder, boolean useTypeReference) {
    // is a class or an interface
    if (expectedType.getKind() == TypeKind.DECLARED) {
        DeclaredType declaredType = (DeclaredType) expectedType;
        List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
        // is NOT a generics, return directly
        if (typeArguments.isEmpty()) {
            return codeModelHelper.typeMirrorToJClass(declaredType);
        }
        // is a generics, must generate a new super class
        TypeElement declaredElement = (TypeElement) declaredType.asElement();
        if (useTypeReference && getElementUtils().getTypeElement(RestSpringClasses.PARAMETERIZED_TYPE_REFERENCE) != null) {
            return codeModelHelper.typeMirrorToJClass(declaredType);
        }
        AbstractJClass baseClass = codeModelHelper.typeMirrorToJClass(declaredType).erasure();
        AbstractJClass decoratedExpectedClass = retrieveDecoratedResponseClass(declaredType, declaredElement, holder);
        if (decoratedExpectedClass == null) {
            decoratedExpectedClass = baseClass;
        }
        return decoratedExpectedClass;
    } else if (expectedType.getKind() == TypeKind.ARRAY) {
        ArrayType arrayType = (ArrayType) expectedType;
        TypeMirror componentType = arrayType.getComponentType();
        return resolveResponseClass(componentType, holder, false).array();
    }
    // is not a class nor an interface, return directly
    return codeModelHelper.typeMirrorToJClass(expectedType);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AbstractJClass(com.helger.jcodemodel.AbstractJClass) 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