Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
got rid of the direct() call
  • Loading branch information
rockytriton committed May 27, 2013
commit 46263f97e4f3bdcf22cd7701db434f1227e6f336
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.androidannotations.annotations.UiThread;
import org.androidannotations.helper.APTCodeModelHelper;

import android.os.Looper;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
Expand All @@ -34,9 +36,14 @@
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JOp;

public class UiThreadProcessor implements DecoratingElementProcessor {

private static final String METHOD_CUR_THREAD = "currentThread";
private static final String METHOD_MAIN_LOOPER = "getMainLooper";
private static final String METHOD_GET_THREAD = "getThread";

private final APTCodeModelHelper helper = new APTCodeModelHelper();

@Override
Expand Down Expand Up @@ -64,12 +71,7 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
holder.handler = holder.generatedClass.field(JMod.PRIVATE, handlerClass, "handler_", JExpr._new(handlerClass));
}

JExpression jx = JExpr.direct("java.lang.Thread.currentThread() == android.os.Looper.getMainLooper().getThread()");

JConditional con = delegatingMethod.body()._if(jx);
JBlock block = con._then();
block.add(JExpr._super().invoke(delegatingMethod));
block._return();
addUIThreadCheck(delegatingMethod, codeModel);

if (delay == 0) {
delegatingMethod.body().invoke(holder.handler, "post").arg(_new(anonymousRunnableClass));
Expand All @@ -80,4 +82,26 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t

}

/**
* Add the pre-check to see if we are already in the UI thread.
*
* @param delegatingMethod
* @param codeModel
* @throws JClassAlreadyExistsException
*/
private void addUIThreadCheck(JMethod delegatingMethod, JCodeModel codeModel) throws JClassAlreadyExistsException {
// Get the Thread and Looper class.
JClass tClass = codeModel.ref(Thread.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't codeModel.ref(<a class>) because it messed up sometimes.
Have a look to EBeansHolder class instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EBeansHolder.refClass(Class) just calls codeModel.ref(). Should I be using EBeansHolder.refClass(String) instead and use the String name of the class? Do you know what situations that the ref() method doesn't work in? It works in my application at least. Just want to make sure I am using it correctly.

JClass lClass = codeModel.ref(Looper.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will look into this.


// invoke the methods.
JExpression lhs = tClass.staticInvoke(METHOD_CUR_THREAD);
JExpression 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 block = con._then();
block.add(JExpr._super().invoke(delegatingMethod));
block._return();
}
}