Skip to content

Commit 81ec0fc

Browse files
committed
Changing processing order, ancestors first androidannotations#296
1 parent a4803f2 commit 81ec0fc

4 files changed

Lines changed: 91 additions & 15 deletions

File tree

  • AndroidAnnotations

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/ModelProcessor.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ public ProcessResult process(AnnotationElements validatedModel) throws Exception
8888
for (DecoratingElementProcessor processor : enclosedProcessors) {
8989
Class<? extends Annotation> target = processor.getTarget();
9090

91+
/*
92+
* For ancestors, the processor manipulates the annotated elements,
93+
* but uses the holder for the root element
94+
*/
95+
Set<AnnotatedAndRootElements> ancestorAnnotatedElements = validatedModel.getAncestorAnnotatedElements(target.getName());
96+
for (AnnotatedAndRootElements elements : ancestorAnnotatedElements) {
97+
EBeanHolder holder = eBeansHolder.getEBeanHolder(elements.rootTypeElement);
98+
/*
99+
* Annotations coming from ancestors may be applied to root
100+
* elements that are not validated, and therefore not available.
101+
*/
102+
if (holder != null) {
103+
processor.process(elements.annotatedElement, codeModel, holder);
104+
}
105+
}
106+
91107
Set<? extends Element> rootAnnotatedElements = validatedModel.getRootAnnotatedElements(target.getName());
92108

93109
for (Element annotatedElement : rootAnnotatedElements) {
@@ -109,21 +125,6 @@ public ProcessResult process(AnnotationElements validatedModel) throws Exception
109125
}
110126
}
111127

112-
/*
113-
* For ancestors, the processor manipulates the annotated elements,
114-
* but uses the holder for the root element
115-
*/
116-
Set<AnnotatedAndRootElements> ancestorAnnotatedElements = validatedModel.getAncestorAnnotatedElements(target.getName());
117-
for (AnnotatedAndRootElements elements : ancestorAnnotatedElements) {
118-
EBeanHolder holder = eBeansHolder.getEBeanHolder(elements.rootTypeElement);
119-
/*
120-
* Annotations coming from ancestors may be applied to root
121-
* elements that are not validated, and therefore not available.
122-
*/
123-
if (holder != null) {
124-
processor.process(elements.annotatedElement, codeModel, holder);
125-
}
126-
}
127128
}
128129

129130
return new ProcessResult(//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.androidannotations.test15.inheritance;
2+
3+
import static org.fest.assertions.Assertions.assertThat;
4+
import static org.mockito.Mockito.mock;
5+
6+
import org.androidannotations.test15.AndroidAnnotationsTestRunner;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import android.app.Activity;
11+
import android.content.Context;
12+
13+
@RunWith(AndroidAnnotationsTestRunner.class)
14+
public class InheritanceTest {
15+
16+
@Test
17+
public void after_inject_mother_calls_first() {
18+
Child child = Child_.getInstance_(mock(Context.class));
19+
assertThat(child.motherInitWasCalled).isTrue();
20+
}
21+
22+
@Test
23+
public void after_views_mother_calls_first() {
24+
Child_ child = Child_.getInstance_(mock(Activity.class));
25+
child.afterSetContentView_();
26+
assertThat(child.motherInitViewsWasCalled).isTrue();
27+
}
28+
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.androidannotations.test15.inheritance;
2+
3+
import org.androidannotations.annotations.AfterInject;
4+
import org.androidannotations.annotations.AfterViews;
5+
import org.androidannotations.annotations.EBean;
6+
7+
@EBean
8+
public class Child extends Mother {
9+
10+
public boolean motherInitWasCalled;
11+
public boolean motherInitViewsWasCalled;
12+
13+
@AfterInject
14+
void initChild() {
15+
motherInitWasCalled = motherInitCalled;
16+
}
17+
18+
@AfterViews
19+
void initViewsChild() {
20+
motherInitViewsWasCalled = motherInitViewsCalled;
21+
}
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.androidannotations.test15.inheritance;
2+
3+
import org.androidannotations.annotations.AfterInject;
4+
import org.androidannotations.annotations.AfterViews;
5+
import org.androidannotations.annotations.EBean;
6+
7+
@EBean
8+
public abstract class Mother {
9+
10+
protected boolean motherInitCalled = false;
11+
protected boolean motherInitViewsCalled = false;
12+
13+
@AfterInject
14+
void initMother() {
15+
motherInitCalled = true;
16+
}
17+
18+
@AfterViews
19+
void initViewsMother() {
20+
motherInitViewsCalled = true;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)