From 81ec0fc612ac867fdfcf5cb2d3f27597fa34b629 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Thu, 6 Dec 2012 11:56:43 +0100 Subject: [PATCH] Changing processing order, ancestors first #296 --- .../processing/ModelProcessor.java | 31 ++++++++++--------- .../test15/inheritance/InheritanceTest.java | 29 +++++++++++++++++ .../test15/inheritance/Child.java | 23 ++++++++++++++ .../test15/inheritance/Mother.java | 23 ++++++++++++++ 4 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/inheritance/InheritanceTest.java create mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Child.java create mode 100644 AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Mother.java diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/ModelProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/ModelProcessor.java index 687d9cf6db..23689ff650 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/ModelProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/ModelProcessor.java @@ -88,6 +88,22 @@ public ProcessResult process(AnnotationElements validatedModel) throws Exception for (DecoratingElementProcessor processor : enclosedProcessors) { Class target = processor.getTarget(); + /* + * For ancestors, the processor manipulates the annotated elements, + * but uses the holder for the root element + */ + Set ancestorAnnotatedElements = validatedModel.getAncestorAnnotatedElements(target.getName()); + for (AnnotatedAndRootElements elements : ancestorAnnotatedElements) { + EBeanHolder holder = eBeansHolder.getEBeanHolder(elements.rootTypeElement); + /* + * Annotations coming from ancestors may be applied to root + * elements that are not validated, and therefore not available. + */ + if (holder != null) { + processor.process(elements.annotatedElement, codeModel, holder); + } + } + Set rootAnnotatedElements = validatedModel.getRootAnnotatedElements(target.getName()); for (Element annotatedElement : rootAnnotatedElements) { @@ -109,21 +125,6 @@ public ProcessResult process(AnnotationElements validatedModel) throws Exception } } - /* - * For ancestors, the processor manipulates the annotated elements, - * but uses the holder for the root element - */ - Set ancestorAnnotatedElements = validatedModel.getAncestorAnnotatedElements(target.getName()); - for (AnnotatedAndRootElements elements : ancestorAnnotatedElements) { - EBeanHolder holder = eBeansHolder.getEBeanHolder(elements.rootTypeElement); - /* - * Annotations coming from ancestors may be applied to root - * elements that are not validated, and therefore not available. - */ - if (holder != null) { - processor.process(elements.annotatedElement, codeModel, holder); - } - } } return new ProcessResult(// diff --git a/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/inheritance/InheritanceTest.java b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/inheritance/InheritanceTest.java new file mode 100644 index 0000000000..56487b340d --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5-tests/src/test/java/org/androidannotations/test15/inheritance/InheritanceTest.java @@ -0,0 +1,29 @@ +package org.androidannotations.test15.inheritance; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +import org.androidannotations.test15.AndroidAnnotationsTestRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import android.app.Activity; +import android.content.Context; + +@RunWith(AndroidAnnotationsTestRunner.class) +public class InheritanceTest { + + @Test + public void after_inject_mother_calls_first() { + Child child = Child_.getInstance_(mock(Context.class)); + assertThat(child.motherInitWasCalled).isTrue(); + } + + @Test + public void after_views_mother_calls_first() { + Child_ child = Child_.getInstance_(mock(Activity.class)); + child.afterSetContentView_(); + assertThat(child.motherInitViewsWasCalled).isTrue(); + } + +} diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Child.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Child.java new file mode 100644 index 0000000000..36bdb269e4 --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Child.java @@ -0,0 +1,23 @@ +package org.androidannotations.test15.inheritance; + +import org.androidannotations.annotations.AfterInject; +import org.androidannotations.annotations.AfterViews; +import org.androidannotations.annotations.EBean; + +@EBean +public class Child extends Mother { + + public boolean motherInitWasCalled; + public boolean motherInitViewsWasCalled; + + @AfterInject + void initChild() { + motherInitWasCalled = motherInitCalled; + } + + @AfterViews + void initViewsChild() { + motherInitViewsWasCalled = motherInitViewsCalled; + } + +} diff --git a/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Mother.java b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Mother.java new file mode 100644 index 0000000000..a9dc13e286 --- /dev/null +++ b/AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/inheritance/Mother.java @@ -0,0 +1,23 @@ +package org.androidannotations.test15.inheritance; + +import org.androidannotations.annotations.AfterInject; +import org.androidannotations.annotations.AfterViews; +import org.androidannotations.annotations.EBean; + +@EBean +public abstract class Mother { + + protected boolean motherInitCalled = false; + protected boolean motherInitViewsCalled = false; + + @AfterInject + void initMother() { + motherInitCalled = true; + } + + @AfterViews + void initViewsMother() { + motherInitViewsCalled = true; + } + +}