Search in sources :

Example 36 with AbstractJClass

use of com.helger.jcodemodel.AbstractJClass 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 37 with AbstractJClass

use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.

the class OrmLiteHolder method setDatabaseHelperRef.

private JFieldVar setDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
    AbstractJClass databaseHelperClass = getJClass(databaseHelperTypeMirror.toString());
    String fieldName = CaseHelper.lowerCaseFirst(databaseHelperClass.name()) + ModelConstants.generationSuffix();
    JFieldVar databaseHelperRef = getGeneratedClass().field(PRIVATE, databaseHelperClass, fieldName);
    databaseHelperRefs.put(databaseHelperTypeMirror, databaseHelperRef);
    IJExpression dbHelperClass = databaseHelperClass.dotclass();
    //
    holder().getInitBodyInjectionBlock().assign(//
    databaseHelperRef, getJClass(OrmLiteClasses.OPEN_HELPER_MANAGER).staticInvoke("getHelper").arg(holder().getContextRef()).arg(dbHelperClass));
    return databaseHelperRef;
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 38 with AbstractJClass

use of com.helger.jcodemodel.AbstractJClass 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 39 with AbstractJClass

use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.

the class RestAnnotationHelper method plainName.

protected String plainName(AbstractJClass jClass) {
    String plainName = jClass.erasure().name();
    List<? extends AbstractJClass> typeParameters = jClass.getTypeParameters();
    if (typeParameters.size() > 0) {
        plainName += "_";
        for (AbstractJClass typeParameter : typeParameters) {
            plainName += plainName(typeParameter);
        }
    }
    return plainName;
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 40 with AbstractJClass

use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.

the class RestHolder method setAvailableCookiesField.

private void setAvailableCookiesField() {
    AbstractJClass stringClass = getClasses().STRING;
    AbstractJClass mapClass = getClasses().HASH_MAP.narrow(stringClass, stringClass);
    availableCookiesField = getGeneratedClass().field(JMod.PRIVATE, mapClass, "availableCookies");
    getInit().body().assign(availableCookiesField, _new(mapClass));
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Aggregations

AbstractJClass (com.helger.jcodemodel.AbstractJClass)125 JVar (com.helger.jcodemodel.JVar)48 JMethod (com.helger.jcodemodel.JMethod)36 JBlock (com.helger.jcodemodel.JBlock)35 JInvocation (com.helger.jcodemodel.JInvocation)31 TypeMirror (javax.lang.model.type.TypeMirror)24 IJExpression (com.helger.jcodemodel.IJExpression)23 JDefinedClass (com.helger.jcodemodel.JDefinedClass)19 VariableElement (javax.lang.model.element.VariableElement)14 JFieldVar (com.helger.jcodemodel.JFieldVar)13 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)9 ExecutableElement (javax.lang.model.element.ExecutableElement)9 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)8 JConditional (com.helger.jcodemodel.JConditional)8 JTypeVar (com.helger.jcodemodel.JTypeVar)8 DeclaredType (javax.lang.model.type.DeclaredType)8 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)6 JFieldRef (com.helger.jcodemodel.JFieldRef)6 ArrayList (java.util.ArrayList)6 BundleHelper (org.androidannotations.helper.BundleHelper)6