This repository was archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
added check for UI Thread #605
Merged
DayS
merged 7 commits into
androidannotations:develop
from
rockytriton:604_check_currentThread_in_UiThread
Jul 12, 2013
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50ecf50
added check for UI Thread
rockytriton 46263f9
got rid of the direct() call
rockytriton 6b7d716
Fixed an issue when the method had parameters
rockytriton acd3972
added useDirect variable to UiThread annotation
rockytriton a5c4b7f
added Propagation enum instead of boolean
rockytriton a4816de
Removed clone body for a better method.
rockytriton f48bdd9
Changed to only reuse thread if delay == 0
rockytriton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,18 +22,29 @@ | |
| import javax.lang.model.element.ExecutableElement; | ||
|
|
||
| import org.androidannotations.annotations.UiThread; | ||
| import org.androidannotations.annotations.UiThread.Propagation; | ||
| import org.androidannotations.helper.APTCodeModelHelper; | ||
|
|
||
| import android.os.Looper; | ||
|
|
||
| import com.sun.codemodel.JBlock; | ||
| import com.sun.codemodel.JClass; | ||
| import com.sun.codemodel.JClassAlreadyExistsException; | ||
| import com.sun.codemodel.JCodeModel; | ||
| import com.sun.codemodel.JConditional; | ||
| import com.sun.codemodel.JDefinedClass; | ||
| import com.sun.codemodel.JExpr; | ||
| 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 | ||
|
|
@@ -45,15 +56,15 @@ public String getTarget() { | |
| public void process(Element element, JCodeModel codeModel, EBeanHolder holder) throws JClassAlreadyExistsException { | ||
|
|
||
| ExecutableElement executableElement = (ExecutableElement) element; | ||
| UiThread annotation = element.getAnnotation(UiThread.class); | ||
| Propagation propagation = annotation.propagation(); | ||
|
|
||
| JMethod delegatingMethod = helper.overrideAnnotatedMethod(executableElement, holder); | ||
|
|
||
| JDefinedClass anonymousRunnableClass = helper.createDelegatingAnonymousRunnableClass(holder, delegatingMethod); | ||
|
|
||
| { | ||
| // Execute Runnable | ||
|
|
||
| UiThread annotation = element.getAnnotation(UiThread.class); | ||
| long delay = annotation.delay(); | ||
|
|
||
| if (holder.handler == null) { | ||
|
|
@@ -62,6 +73,11 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t | |
| } | ||
|
|
||
| if (delay == 0) { | ||
| if (propagation == Propagation.REUSE) { | ||
| // Put in the check for the UI thread. | ||
| addUIThreadCheck(delegatingMethod, codeModel, holder); | ||
| } | ||
|
|
||
| delegatingMethod.body().invoke(holder.handler, "post").arg(_new(anonymousRunnableClass)); | ||
| } else { | ||
| delegatingMethod.body().invoke(holder.handler, "postDelayed").arg(_new(anonymousRunnableClass)).arg(lit(delay)); | ||
|
|
@@ -70,4 +86,29 @@ 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 | ||
| * @param holder | ||
| * @throws JClassAlreadyExistsException | ||
| */ | ||
| private void addUIThreadCheck(JMethod delegatingMethod, JCodeModel codeModel, EBeanHolder holder) throws JClassAlreadyExistsException { | ||
| // Get the Thread and Looper class. | ||
| JClass tClass = codeModel.ref(Thread.class); | ||
| JClass lClass = codeModel.ref(Looper.class); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 thenBlock = con._then(); | ||
|
|
||
| helper.callSuperMethod(delegatingMethod, holder, thenBlock); | ||
|
|
||
| thenBlock._return(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.