Skip to content

Commit c46b0e7

Browse files
committed
Merge pull request androidannotations#677 from tbruyelle/646_PropagateThreadExceptions
Propagate exceptions on @Background/@UIThread annotated methods
2 parents 6b70c32 + c4e8e3d commit c46b0e7

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public String getIdStringFromIdFieldRef(JFieldRef idRef) {
228228
throw new IllegalStateException("Unable to extract target name from JFieldRef");
229229
}
230230

231-
public JTryBlock surroundWithTryCatch(EBeanHolder holder, JBlock block, JBlock content, String exceptionMessage) {
231+
public JTryBlock surroundWithTryCatch(EBeanHolder holder, JBlock block, JBlock content, String exceptionMessage, boolean propagate) {
232232
Classes classes = holder.classes();
233233
JTryBlock tryBlock = block._try();
234234
tryBlock.body().add(content);
@@ -239,6 +239,9 @@ public JTryBlock surroundWithTryCatch(EBeanHolder holder, JBlock block, JBlock c
239239
errorInvoke.arg(exceptionMessage);
240240
errorInvoke.arg(exceptionParam);
241241
catchBlock.body().add(errorInvoke);
242+
if (propagate) {
243+
catchBlock.body()._throw(exceptionParam);
244+
}
242245
return tryBlock;
243246
}
244247

@@ -257,7 +260,7 @@ public JDefinedClass createDelegatingAnonymousRunnableClass(EBeanHolder holder,
257260

258261
JBlock runMethodBody = runMethod.body();
259262

260-
surroundWithTryCatch(holder, runMethodBody, previousMethodBody, "A runtime exception was thrown while executing code in a runnable");
263+
surroundWithTryCatch(holder, runMethodBody, previousMethodBody, "A runtime exception was thrown while executing code in a runnable", true);
261264

262265
return anonymousRunnableClass;
263266
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/BackgroundProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
6161
executeMethod.annotate(Override.class);
6262

6363
JBlock runMethodBody = executeMethod.body();
64-
helper.surroundWithTryCatch(holder, runMethodBody, previousMethodBody, "A runtime exception was thrown while executing code in a background task");
64+
helper.surroundWithTryCatch(holder, runMethodBody, previousMethodBody, "A runtime exception was thrown while executing code in a background task", true);
6565

6666
Background annotation = element.getAnnotation(Background.class);
6767
String id = annotation.id();

‎AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/ThreadActivityTest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,26 @@ public void cancellableSerializedBackgroundTasks() {
263263
}
264264
}
265265

266+
@Test
267+
public void propagateException() {
268+
BackgroundExecutor.setExecutor(new Executor() {
269+
@Override
270+
public void execute(Runnable command) {
271+
command.run();
272+
}
273+
});
274+
try {
275+
activity.backgroundThrowException();
276+
Assert.fail("Exception should be propagated in @Background annotated methods");
277+
} catch (RuntimeException e) {
278+
// good
279+
}
280+
try {
281+
activity.uiThreadThrowException();
282+
Assert.fail("Exception should be propagated in @UIThread annotated methods");
283+
} catch (RuntimeException e) {
284+
// good
285+
}
286+
}
287+
266288
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,14 @@ void backgrounddUsingArrayParamtersMethod(MySerializableBean[] array) {
139139
@Background
140140
void backgroundUsingArrayParamtersMethod(MySerializableBean[][] array) {
141141
}
142+
143+
@Background
144+
void backgroundThrowException() {
145+
throw new RuntimeException();
146+
}
147+
148+
@UiThread
149+
void uiThreadThrowException() {
150+
throw new RuntimeException();
151+
}
142152
}

0 commit comments

Comments
 (0)