Skip to content

Commit e2fa526

Browse files
vincentjames501Yoann Delouis
authored andcommitted
Adding an @IgnoredWhenDetached for @efragment's
1 parent 4ec8a02 commit e2fa526

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* When used standalone in an {@link EFragment} or in conjunction with the {@link UiThread}
25+
* or {@link Background} annotations, the annotated method will be wrapped in an 'if attached'
26+
* block such that no code will be executed if the {@link EFragment} is no longer bound to
27+
* its parent activity.
28+
* <p/>
29+
* Should be used on method that must meet the following criteria
30+
* <p/>
31+
* 1) Can only be used in conjunction with classes annotated with {@link EFragment}
32+
* <p/>
33+
* 2) The annotated method MUST return void and MAY contain parameters.
34+
*
35+
* @see org.androidannotations.annotations.EFragment
36+
* @see org.androidannotations.annotations.UiThread
37+
* @see org.androidannotations.annotations.Background
38+
* @see android.os.Handler
39+
*/
40+
@Retention(RetentionPolicy.CLASS)
41+
@Target(ElementType.METHOD)
42+
public @interface IgnoredWhenDetached {
43+
44+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ public AnnotationHandlers(ProcessingEnvironment processingEnvironment) {
116116
/* UIThreadHandler and BackgroundHandler must be after TraceHandler */
117117
add(new UiThreadHandler(processingEnvironment));
118118
add(new BackgroundHandler(processingEnvironment));
119+
120+
/* Should come after everything so it can wrap the entire body accordingly */
121+
add(new IgnoredWhenDetachedHandler(processingEnvironment));
119122
}
120123

121124
private void add(AnnotationHandler<? extends GeneratedClassHolder> annotationHandler) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.handler;
17+
18+
import com.sun.codemodel.JBlock;
19+
import com.sun.codemodel.JMethod;
20+
import org.androidannotations.annotations.IgnoredWhenDetached;
21+
import org.androidannotations.helper.APTCodeModelHelper;
22+
import org.androidannotations.holder.EComponentHolder;
23+
import org.androidannotations.model.AnnotationElements;
24+
import org.androidannotations.process.IsValid;
25+
26+
import javax.annotation.processing.ProcessingEnvironment;
27+
import javax.lang.model.element.Element;
28+
import javax.lang.model.element.ExecutableElement;
29+
30+
import static com.sun.codemodel.JExpr._null;
31+
import static com.sun.codemodel.JExpr._this;
32+
import static com.sun.codemodel.JExpr.invoke;
33+
34+
public class IgnoredWhenDetachedHandler extends BaseAnnotationHandler<EComponentHolder> {
35+
36+
private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();
37+
38+
public IgnoredWhenDetachedHandler(ProcessingEnvironment processingEnvironment) {
39+
super(IgnoredWhenDetached.class, processingEnvironment);
40+
}
41+
42+
@Override
43+
public void validate(Element element, AnnotationElements validatedElements, IsValid valid) {
44+
validatorHelper.enclosingElementHasEFragment(element, validatedElements, valid);
45+
}
46+
47+
@Override
48+
public void process(Element element, EComponentHolder holder) throws Exception {
49+
ExecutableElement executableElement = (ExecutableElement) element;
50+
JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
51+
JBlock previousMethodBody = codeModelHelper.removeBody(delegatingMethod);
52+
53+
delegatingMethod.body()._if(invoke(_this(), "getActivity").ne(_null()))._then().add(previousMethodBody);
54+
}
55+
}

0 commit comments

Comments
 (0)