Skip to content

Commit c5d5a69

Browse files
yDelouisYoann Delouis
authored andcommitted
Fix IgnoredWhenDetached with UiThread(Reuse)
1 parent 03130e8 commit c5d5a69

3 files changed

Lines changed: 19 additions & 33 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/UiThreadHandler.java‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,17 @@
1515
*/
1616
package org.androidannotations.handler;
1717

18-
import static com.sun.codemodel.JExpr._new;
19-
import static com.sun.codemodel.JExpr.lit;
18+
import com.sun.codemodel.*;
19+
import org.androidannotations.annotations.UiThread;
20+
import org.androidannotations.helper.APTCodeModelHelper;
21+
import org.androidannotations.holder.EComponentHolder;
2022

2123
import javax.annotation.processing.ProcessingEnvironment;
2224
import javax.lang.model.element.Element;
2325
import javax.lang.model.element.ExecutableElement;
2426

25-
import org.androidannotations.annotations.UiThread;
26-
import org.androidannotations.helper.APTCodeModelHelper;
27-
import org.androidannotations.holder.EComponentHolder;
28-
29-
import com.sun.codemodel.JBlock;
30-
import com.sun.codemodel.JClass;
31-
import com.sun.codemodel.JClassAlreadyExistsException;
32-
import com.sun.codemodel.JConditional;
33-
import com.sun.codemodel.JDefinedClass;
34-
import com.sun.codemodel.JExpression;
35-
import com.sun.codemodel.JMethod;
36-
import com.sun.codemodel.JOp;
27+
import static com.sun.codemodel.JExpr._new;
28+
import static com.sun.codemodel.JExpr.lit;
3729

3830
public class UiThreadHandler extends AbstractRunnableHandler {
3931

@@ -51,7 +43,8 @@ public UiThreadHandler(ProcessingEnvironment processingEnvironment) {
5143
public void process(Element element, EComponentHolder holder) throws Exception {
5244
ExecutableElement executableElement = (ExecutableElement) element;
5345
JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
54-
JDefinedClass anonymousRunnableClass = codeModelHelper.createDelegatingAnonymousRunnableClass(holder, delegatingMethod);
46+
JBlock previousBody = codeModelHelper.removeBody(delegatingMethod);
47+
JDefinedClass anonymousRunnableClass = codeModelHelper.createDelegatingAnonymousRunnableClass(holder, previousBody);
5548

5649
UiThread annotation = element.getAnnotation(UiThread.class);
5750
long delay = annotation.delay();
@@ -60,7 +53,7 @@ public void process(Element element, EComponentHolder holder) throws Exception {
6053
if (delay == 0) {
6154
if (propagation == UiThread.Propagation.REUSE) {
6255
// Put in the check for the UI thread.
63-
addUIThreadCheck(delegatingMethod, holder);
56+
addUIThreadCheck(delegatingMethod, previousBody, holder);
6457
}
6558

6659
delegatingMethod.body().invoke(holder.getHandler(), "post").arg(_new(anonymousRunnableClass));
@@ -76,7 +69,7 @@ public void process(Element element, EComponentHolder holder) throws Exception {
7669
* @param holder
7770
* @throws JClassAlreadyExistsException
7871
*/
79-
private void addUIThreadCheck(JMethod delegatingMethod, EComponentHolder holder) throws JClassAlreadyExistsException {
72+
private void addUIThreadCheck(JMethod delegatingMethod, JBlock previousBody, EComponentHolder holder) throws JClassAlreadyExistsException {
8073
// Get the Thread and Looper class.
8174
JClass tClass = holder.classes().THREAD;
8275
JClass lClass = holder.classes().LOOPER;
@@ -87,10 +80,7 @@ private void addUIThreadCheck(JMethod delegatingMethod, EComponentHolder holder)
8780

8881
// create the conditional and the block.
8982
JConditional con = delegatingMethod.body()._if(JOp.eq(lhs, rhs));
90-
JBlock thenBlock = con._then();
91-
92-
codeModelHelper.callSuperMethod(delegatingMethod, holder, thenBlock);
93-
83+
JBlock thenBlock = con._then().add(previousBody);
9484
thenBlock._return();
9585
}
9686
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import static com.sun.codemodel.JExpr.lit;
2020
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
2121

22+
import org.androidannotations.holder.EComponentHolder;
23+
import org.androidannotations.holder.GeneratedClassHolder;
24+
25+
import javax.lang.model.util.ElementFilter;
2226
import java.io.StringWriter;
2327
import java.lang.annotation.Annotation;
2428
import java.lang.reflect.Field;
@@ -41,13 +45,10 @@
4145
import javax.lang.model.type.TypeKind;
4246
import javax.lang.model.type.TypeMirror;
4347
import javax.lang.model.type.WildcardType;
44-
import javax.lang.model.util.ElementFilter;
4548
import javax.lang.model.util.Types;
4649

4750
import org.androidannotations.annotations.EBean;
4851
import org.androidannotations.holder.EBeanHolder;
49-
import org.androidannotations.holder.EComponentHolder;
50-
import org.androidannotations.holder.GeneratedClassHolder;
5152

5253
import com.sun.codemodel.JAnnotatable;
5354
import com.sun.codemodel.JAnnotationUse;
@@ -376,19 +377,14 @@ public String getIdStringFromIdFieldRef(JFieldRef idRef) {
376377
throw new IllegalStateException("Unable to extract target name from JFieldRef");
377378
}
378379

379-
public JDefinedClass createDelegatingAnonymousRunnableClass(EComponentHolder holder, JMethod delegatedMethod) {
380-
380+
public JDefinedClass createDelegatingAnonymousRunnableClass(EComponentHolder holder, JBlock previousBody) {
381381
JCodeModel codeModel = holder.codeModel();
382382

383-
JDefinedClass anonymousRunnableClass;
384-
JBlock previousMethodBody = removeBody(delegatedMethod);
385-
386-
anonymousRunnableClass = codeModel.anonymousClass(Runnable.class);
383+
JDefinedClass anonymousRunnableClass = codeModel.anonymousClass(Runnable.class);
387384

388385
JMethod runMethod = anonymousRunnableClass.method(JMod.PUBLIC, codeModel.VOID, "run");
389386
runMethod.annotate(Override.class);
390-
391-
runMethod.body().add(previousMethodBody);
387+
runMethod.body().add(previousBody);
392388

393389
return anonymousRunnableClass;
394390
}

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/efragment/MyListFragment.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void uiThread() {
5555
didExecute = true;
5656
}
5757

58-
@UiThread
58+
@UiThread(propagation = UiThread.Propagation.REUSE)
5959
@IgnoredWhenDetached
6060
void uiThreadIgnored() {
6161
didExecute = true;

0 commit comments

Comments
 (0)