Search in sources :

Example 51 with AbstractJClass

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

the class ProcessHolder method refClass.

public AbstractJClass refClass(Class<?> clazz) {
    AbstractJClass referencedClass = codeModel.ref(clazz);
    loadedClasses.put(clazz.getCanonicalName(), referencedClass);
    return referencedClass;
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 52 with AbstractJClass

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

the class RInnerClass method extractIdStaticRef.

public static JFieldRef extractIdStaticRef(AndroidAnnotationsEnvironment environment, String layoutFieldQualifiedName) {
    if (layoutFieldQualifiedName != null) {
        int fieldSuffix = layoutFieldQualifiedName.lastIndexOf('.');
        String fieldName = layoutFieldQualifiedName.substring(fieldSuffix + 1);
        String rInnerClassName = layoutFieldQualifiedName.substring(0, fieldSuffix);
        int innerClassSuffix = rInnerClassName.lastIndexOf('.');
        String rClassQualifiedName = rInnerClassName.substring(0, innerClassSuffix);
        String innerClassSimpleName = rInnerClassName.substring(innerClassSuffix + 1);
        JDirectClass rClass = (JDirectClass) environment.getJClass(rClassQualifiedName);
        AbstractJClass innerClass = null;
        for (JDirectClass clazz : rClass.classes()) {
            if (clazz.name().equals(innerClassSimpleName)) {
                innerClass = clazz;
                break;
            }
        }
        if (innerClass == null) {
            try {
                innerClass = rClass._class(innerClassSimpleName);
            } catch (JClassAlreadyExistsException e) {
            // never happens, since we already checked the inner class does not exist
            }
        }
        return innerClass.staticRef(fieldName);
    } else {
        return null;
    }
}
Also used : JClassAlreadyExistsException(com.helger.jcodemodel.JClassAlreadyExistsException) JDirectClass(com.helger.jcodemodel.JDirectClass) AbstractJClass(com.helger.jcodemodel.AbstractJClass)

Example 53 with AbstractJClass

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

the class TraceHandler method getExitMessage.

private IJExpression getExitMessage(ExecutableElement element, JMethod method, JVar result, JVar duration) throws ClassNotFoundException {
    String methodName = getMethodName(element);
    List<JVar> params = method.params();
    StringBuilder paramStr = new StringBuilder();
    for (int i = 0; i < params.size(); i++) {
        if (i > 0) {
            paramStr.append(", ");
        }
        JVar var = params.get(i);
        paramStr.append(var.type().name());
    }
    methodName += "(" + paramStr.toString() + ")";
    JInvocation format = getJClass(String.class).staticInvoke("format");
    if (result == null) {
        format.arg("Exiting [" + methodName + "], duration in ms: %d");
    } else {
        format.arg("Exiting [" + methodName + " returning: %s], duration in ms: %d");
        if (method.type().isArray()) {
            AbstractJClass arraysClass = getJClass(Arrays.class);
            format.arg(arraysClass.staticInvoke("toString").arg(JExpr.cast(boxify(method.type()), result)));
        } else {
            format.arg(result);
        }
    }
    return format.arg(duration);
}
Also used : AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JVar(com.helger.jcodemodel.JVar)

Example 54 with AbstractJClass

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

the class TransactionalHandler method process.

@Override
public void process(Element element, EComponentHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    String returnTypeName = executableElement.getReturnType().toString();
    AbstractJClass returnType = getJClass(returnTypeName);
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    codeModelHelper.removeBody(method);
    JVar db = method.params().get(0);
    JBlock body = method.body();
    body.invoke(db, "beginTransaction");
    JTryBlock tryBlock = body._try();
    IJExpression activitySuper = holder.getGeneratedClass().staticRef("super");
    JInvocation superCall = JExpr.invoke(activitySuper, method);
    for (JVar param : method.params()) {
        superCall.arg(param);
    }
    JBlock tryBody = tryBlock.body();
    if (returnTypeName.equals("void")) {
        tryBody.add(superCall);
        tryBody.invoke(db, "setTransactionSuccessful");
        tryBody._return();
    } else {
        JVar result = tryBody.decl(returnType, "result_", superCall);
        tryBody.invoke(db, "setTransactionSuccessful");
        tryBody._return(result);
    }
    JCatchBlock catchBlock = tryBlock._catch(getJClass(RuntimeException.class));
    JVar exceptionParam = catchBlock.param("e");
    JBlock catchBody = catchBlock.body();
    JInvocation errorInvoke = catchBody.staticInvoke(getClasses().LOG, "e");
    errorInvoke.arg(logTagForClassHolder(holder));
    errorInvoke.arg("Error in transaction");
    errorInvoke.arg(exceptionParam);
    catchBody._throw(exceptionParam);
    tryBlock._finally().invoke(db, "endTransaction");
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Example 55 with AbstractJClass

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

the class UiThreadHandler method addUIThreadCheck.

/**
	 * Add the pre-check to see if we are already in the UI thread.
	 *
	 * @param delegatingMethod
	 * @param holder
	 * @throws JClassAlreadyExistsException
	 */
private void addUIThreadCheck(JMethod delegatingMethod, JBlock previousBody, EComponentHolder holder) throws JClassAlreadyExistsException {
    // Get the Thread and Looper class.
    AbstractJClass tClass = getClasses().THREAD;
    AbstractJClass lClass = getClasses().LOOPER;
    // invoke the methods.
    IJExpression lhs = tClass.staticInvoke(METHOD_CUR_THREAD);
    IJExpression rhs = lClass.staticInvoke(METHOD_MAIN_LOOPER).invoke(METHOD_GET_THREAD);
    // create the conditional and the block.
    JConditional con = delegatingMethod.body()._if(JOp.eq(lhs, rhs));
    JBlock thenBlock = con._then().add(previousBody);
    thenBlock._return();
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional)

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