Search in sources :

Example 26 with DeclaredType

use of javax.lang.model.type.DeclaredType 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 27 with DeclaredType

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

the class RestAnnotationHelper method getResponseClass.

public IJExpression getResponseClass(Element element, RestHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    IJExpression responseClassExpr = nullCastedToNarrowedClass(holder);
    TypeMirror returnType = executableElement.getReturnType();
    if (returnType.getKind() != TypeKind.VOID) {
        if (getElementUtils().getTypeElement(RestSpringClasses.PARAMETERIZED_TYPE_REFERENCE) != null) {
            if (returnType.toString().startsWith(RestSpringClasses.RESPONSE_ENTITY)) {
                List<? extends TypeMirror> typeArguments = ((DeclaredType) returnType).getTypeArguments();
                if (!typeArguments.isEmpty()) {
                    returnType = typeArguments.get(0);
                }
            }
            if (checkIfParameterizedTypeReferenceShouldBeUsed(returnType)) {
                return createParameterizedTypeReferenceAnonymousSubclassInstance(returnType);
            }
        }
        AbstractJClass responseClass = retrieveResponseClass(returnType, holder);
        if (responseClass != null) {
            responseClassExpr = responseClass.dotclass();
        }
    }
    return responseClassExpr;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) DeclaredType(javax.lang.model.type.DeclaredType)

Example 28 with DeclaredType

use of javax.lang.model.type.DeclaredType 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)

Example 29 with DeclaredType

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

the class RestSpringValidatorHelper method hasSetOfHttpMethodReturnType.

public void hasSetOfHttpMethodReturnType(ExecutableElement element, ElementValidation valid) {
    TypeMirror returnType = element.getReturnType();
    String returnTypeString = returnType.toString();
    if (!returnTypeString.equals("java.util.Set<org.springframework.http.HttpMethod>")) {
        valid.addError("%s annotated methods can only return a Set of HttpMethod, not " + returnTypeString);
    } else {
        DeclaredType declaredType = (DeclaredType) returnType;
        List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
        if (typeArguments.size() != 1) {
            valid.addError("%s annotated methods can only return a parameterized Set (with HttpMethod)");
        } else {
            TypeMirror typeArgument = typeArguments.get(0);
            if (!typeArgument.toString().equals("org.springframework.http.HttpMethod")) {
                valid.addError("%s annotated methods can only return a parameterized Set of HttpMethod, not " + typeArgument.toString());
            }
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 30 with DeclaredType

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

the class RestHandler method setResponseErrorHandler.

private void setResponseErrorHandler(Element element, RestHolder holder) {
    DeclaredType responseErrorHandler = annotationHelper.extractAnnotationClassParameter(element, getTarget(), "responseErrorHandler");
    if (responseErrorHandler != null) {
        JInvocation errorHandler = codeModelHelper.newBeanOrEBean(responseErrorHandler, holder.getInitContextParam());
        holder.getInit().body().add(invoke(holder.getRestTemplateField(), "setErrorHandler").arg(errorHandler));
    }
}
Also used : JInvocation(com.helger.jcodemodel.JInvocation) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DeclaredType (javax.lang.model.type.DeclaredType)136 TypeElement (javax.lang.model.element.TypeElement)81 TypeMirror (javax.lang.model.type.TypeMirror)74 ExecutableElement (javax.lang.model.element.ExecutableElement)38 Element (javax.lang.model.element.Element)28 VariableElement (javax.lang.model.element.VariableElement)26 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)14 ArrayType (javax.lang.model.type.ArrayType)14 List (java.util.List)10 Map (java.util.Map)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 HashSet (java.util.HashSet)8 AnnotationValue (javax.lang.model.element.AnnotationValue)8 HashMap (java.util.HashMap)6 Types (javax.lang.model.util.Types)6 Name (javax.lang.model.element.Name)5 PackageElement (javax.lang.model.element.PackageElement)5 TypeParameterElement (javax.lang.model.element.TypeParameterElement)5