Skip to content

Commit cb7aaef

Browse files
committed
Create blocks for injection and after injection
That way we can make sure that after injection code will always happen after injection, regardless of the annotation handler calling order.
1 parent ce5cd5c commit cb7aaef

28 files changed

Lines changed: 115 additions & 55 deletions

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EActivityHolder.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private void setInjectExtras() {
443443
injectExtrasBlock = injectExtrasBody._if(injectExtras.ne(_null()))._then();
444444

445445
getSetIntent().body().invoke(injectExtrasMethod);
446-
getInitBody().invoke(injectExtrasMethod);
446+
getInitBodyInjectionBlock().invoke(injectExtrasMethod);
447447
}
448448

449449
public JMethod getOnNewIntent() {
@@ -554,7 +554,7 @@ public JVar getInitNonConfigurationInstance() throws JClassAlreadyExistsExceptio
554554
}
555555

556556
private void setInitNonConfigurationInstance() throws JClassAlreadyExistsException {
557-
JBlock initBody = getInitBody();
557+
JBlock initBody = getInitBodyInjectionBlock();
558558
JDefinedClass ncHolderClass = getNonConfigurationHolder().getGeneratedClass();
559559
initNonConfigurationInstance = initBody.decl(ncHolderClass, "nonConfigurationInstance", cast(ncHolderClass, _super().invoke(getGetLastNonConfigurationInstance())));
560560
initIfNonConfigurationNotNullBlock = initBody._if(initNonConfigurationInstance.ne(_null()))._then();
@@ -730,7 +730,7 @@ public JFieldVar getIntentFilterField(IntentFilterData intentFilterData) {
730730

731731
@Override
732732
public JBlock getIntentFilterInitializationBlock(IntentFilterData intentFilterData) {
733-
return getInitBody();
733+
return getInitBodyInjectionBlock();
734734
}
735735

736736
@Override
@@ -739,8 +739,13 @@ public JBlock getPreferenceScreenInitializationBlock() {
739739
}
740740

741741
@Override
742-
public JBlock getAddPreferencesFromResourceBlock() {
743-
return preferencesHolder.getAddPreferencesFromResourceBlock();
742+
public JBlock getAddPreferencesFromResourceInjectionBlock() {
743+
return preferencesHolder.getAddPreferencesFromResourceInjectionBlock();
744+
}
745+
746+
@Override
747+
public JBlock getAddPreferencesFromResourceAfterInjectionBlock() {
748+
return preferencesHolder.getAddPreferencesFromResourceAfterInjectionBlock();
744749
}
745750

746751
@Override

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EComponentHolder.java‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public abstract class EComponentHolder extends BaseGeneratedClassHolder {
3434

3535
protected IJExpression contextRef;
3636
protected JMethod init;
37+
private JBlock initBodyInjectionBlock;
38+
private JBlock initBodyAfterInjectionBlock;
3739
private JVar resourcesRef;
3840
private JFieldVar powerManagerRef;
3941

@@ -63,6 +65,27 @@ public JBlock getInitBody() {
6365
return getInit().body();
6466
}
6567

68+
public JBlock getInitBodyInjectionBlock() {
69+
if (initBodyInjectionBlock == null) {
70+
setInitBodyBlocks();
71+
}
72+
73+
return initBodyInjectionBlock;
74+
}
75+
76+
public JBlock getInitBodyAfterInjectionBlock() {
77+
if (initBodyAfterInjectionBlock == null) {
78+
setInitBodyBlocks();
79+
}
80+
81+
return initBodyAfterInjectionBlock;
82+
}
83+
84+
private void setInitBodyBlocks() {
85+
initBodyInjectionBlock = getInitBody().blockSimple();
86+
initBodyAfterInjectionBlock = getInitBody().blockSimple();
87+
}
88+
6689
public JVar getResourcesRef() {
6790
if (resourcesRef == null) {
6891
setResourcesRef();
@@ -71,7 +94,7 @@ public JVar getResourcesRef() {
7194
}
7295

7396
private void setResourcesRef() {
74-
resourcesRef = getInitBody().decl(getClasses().RESOURCES, "resources" + generationSuffix(), getContextRef().invoke("getResources"));
97+
resourcesRef = getInitBodyInjectionBlock().decl(getClasses().RESOURCES, "resources" + generationSuffix(), getContextRef().invoke("getResources"));
7598
}
7699

77100
public JFieldVar getPowerManagerRef() {
@@ -83,7 +106,7 @@ public JFieldVar getPowerManagerRef() {
83106
}
84107

85108
private void setPowerManagerRef() {
86-
JBlock methodBody = getInitBody();
109+
JBlock methodBody = getInitBodyInjectionBlock();
87110

88111
JFieldRef serviceRef = getClasses().CONTEXT.staticRef("POWER_SERVICE");
89112
powerManagerRef = getGeneratedClass().field(PRIVATE, getClasses().POWER_MANAGER, "powerManager" + generationSuffix());

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public abstract class EComponentWithViewSupportHolder extends EComponentHolder i
5252
protected ViewNotifierHelper viewNotifierHelper;
5353
private JMethod onViewChanged;
5454
private JBlock onViewChangedBody;
55-
private JBlock onViewChangedBodyBeforeFindViews;
55+
private JBlock onViewChangedBodyInjectionBlock;
56+
private JBlock onViewChangedBodyAfterInjectionBlock;
57+
private JBlock onViewChangedBodyBeforeInjectionBlock;
5658
private JVar onViewChangedHasViewsParam;
5759
protected Map<String, FoundHolder> foundHolders = new HashMap<>();
5860
protected JMethod findNativeFragmentById;
@@ -76,11 +78,25 @@ public JBlock getOnViewChangedBody() {
7678
return onViewChangedBody;
7779
}
7880

79-
public JBlock getOnViewChangedBodyBeforeFindViews() {
80-
if (onViewChangedBodyBeforeFindViews == null) {
81+
public JBlock getOnViewChangedBodyBeforeInjectionBlock() {
82+
if (onViewChangedBodyBeforeInjectionBlock == null) {
8183
setOnViewChanged();
8284
}
83-
return onViewChangedBodyBeforeFindViews;
85+
return onViewChangedBodyBeforeInjectionBlock;
86+
}
87+
88+
public JBlock getOnViewChangedBodyInjectionBlock() {
89+
if (onViewChangedBodyInjectionBlock == null) {
90+
setOnViewChanged();
91+
}
92+
return onViewChangedBodyInjectionBlock;
93+
}
94+
95+
public JBlock getOnViewChangedBodyAfterInjectionBlock() {
96+
if (onViewChangedBodyAfterInjectionBlock == null) {
97+
setOnViewChanged();
98+
}
99+
return onViewChangedBodyAfterInjectionBlock;
84100
}
85101

86102
public JVar getOnViewChangedHasViewsParam() {
@@ -95,10 +111,12 @@ protected void setOnViewChanged() {
95111
onViewChanged = getGeneratedClass().method(PUBLIC, getCodeModel().VOID, "onViewChanged");
96112
onViewChanged.annotate(Override.class);
97113
onViewChangedBody = onViewChanged.body();
98-
onViewChangedBodyBeforeFindViews = onViewChangedBody.blockSimple();
114+
onViewChangedBodyBeforeInjectionBlock = onViewChangedBody.blockSimple();
115+
onViewChangedBodyInjectionBlock = onViewChangedBody.blockSimple();
116+
onViewChangedBodyAfterInjectionBlock = onViewChangedBody.blockSimple();
99117
onViewChangedHasViewsParam = onViewChanged.param(HasViews.class, "hasViews");
100118
AbstractJClass notifierClass = getJClass(OnViewChangedNotifier.class);
101-
getInitBody().staticInvoke(notifierClass, "registerOnViewChangedListener").arg(_this());
119+
getInitBodyInjectionBlock().staticInvoke(notifierClass, "registerOnViewChangedListener").arg(_this());
102120
}
103121

104122
public JInvocation findViewById(JFieldRef idRef) {
@@ -115,7 +133,7 @@ public void assignFindViewById(JFieldRef idRef, AbstractJClass viewClass, JField
115133
String idRefString = idRef.name();
116134
FoundViewHolder foundViewHolder = (FoundViewHolder) foundHolders.get(idRefString);
117135

118-
JBlock block = getOnViewChangedBody();
136+
JBlock block = getOnViewChangedBodyInjectionBlock();
119137
IJExpression assignExpression;
120138

121139
if (foundViewHolder != null) {
@@ -147,7 +165,7 @@ public FoundViewHolder getFoundViewHolder(JFieldRef idRef, AbstractJClass viewCl
147165

148166
protected FoundViewHolder createFoundViewAndIfNotNullBlock(JFieldRef idRef, AbstractJClass viewClass) {
149167
IJExpression findViewExpression = findViewById(idRef);
150-
JBlock block = getOnViewChangedBody().blockSimple();
168+
JBlock block = getOnViewChangedBodyInjectionBlock().blockSimple();
151169

152170
if (viewClass == null) {
153171
viewClass = getClasses().VIEW;
@@ -256,7 +274,7 @@ private TextWatcherHolder createTextWatcherHolder(JFieldRef idRef, TypeMirror vi
256274
viewClass = getJClass(viewParameterType.toString());
257275
}
258276

259-
JBlock onViewChangedBody = getOnViewChangedBody().blockSimple();
277+
JBlock onViewChangedBody = getOnViewChangedBodyInjectionBlock().blockSimple();
260278
JVar viewVariable = onViewChangedBody.decl(FINAL, viewClass, "view", cast(viewClass, findViewById(idRef)));
261279
onViewChangedBody._if(viewVariable.ne(JExpr._null()))._then() //
262280
.invoke(viewVariable, "addTextChangedListener").arg(_new(onTextChangeListenerClass));

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ private void setInjectArgs() {
370370
injectBundleArgs = injectExtrasBody.decl(getClasses().BUNDLE, "args_", invoke("getArguments"));
371371
injectArgsBlock = injectExtrasBody._if(injectBundleArgs.ne(_null()))._then();
372372

373-
getInitBody().invoke(injectArgsMethod);
373+
getInitBodyInjectionBlock().invoke(injectArgsMethod);
374374
}
375375

376376
@Override
@@ -535,7 +535,7 @@ public JBlock getIntentFilterInitializationBlock(IntentFilterData intentFilterDa
535535
if (RegisterAt.OnAttachOnDetach.equals(intentFilterData.getRegisterAt())) {
536536
return getOnAttachAfterSuperBlock();
537537
}
538-
return getInitBody();
538+
return getInitBodyInjectionBlock();
539539
}
540540

541541
@Override
@@ -544,8 +544,13 @@ public JBlock getPreferenceScreenInitializationBlock() {
544544
}
545545

546546
@Override
547-
public JBlock getAddPreferencesFromResourceBlock() {
548-
return preferencesDelegate.getAddPreferencesFromResourceBlock();
547+
public JBlock getAddPreferencesFromResourceInjectionBlock() {
548+
return preferencesDelegate.getAddPreferencesFromResourceInjectionBlock();
549+
}
550+
551+
@Override
552+
public JBlock getAddPreferencesFromResourceAfterInjectionBlock() {
553+
return preferencesDelegate.getAddPreferencesFromResourceAfterInjectionBlock();
549554
}
550555

551556
@Override

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EServiceHolder.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public JFieldVar getIntentFilterField(IntentFilterData intentFilterData) {
9797

9898
@Override
9999
public JBlock getIntentFilterInitializationBlock(IntentFilterData intentFilterData) {
100-
return getInitBody();
100+
return getInitBodyInjectionBlock();
101101
}
102102

103103
@Override

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/HasPreferences.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public interface HasPreferences extends GeneratedClassHolder {
2323

2424
JBlock getPreferenceScreenInitializationBlock();
2525

26-
JBlock getAddPreferencesFromResourceBlock();
26+
JBlock getAddPreferencesFromResourceInjectionBlock();
27+
28+
JBlock getAddPreferencesFromResourceAfterInjectionBlock();
2729

2830
void assignFindPreferenceByKey(JFieldRef idRef, AbstractJClass preferenceClass, JFieldRef fieldRef);
2931

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/PreferencesDelegate.java‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838

3939
public class PreferencesDelegate extends GeneratedClassHolderDelegate<EComponentWithViewSupportHolder> implements HasPreferences {
4040

41-
protected JBlock addPreferencesFromResourceBlock;
41+
protected JBlock addPreferencesFromResourceInjectionBlock;
42+
protected JBlock addPreferencesFromResourceAfterInjectionBlock;
4243

4344
private boolean usingSupportV7Preference = false;
4445
private AbstractJClass basePreferenceClass;
@@ -64,19 +65,28 @@ public PreferencesDelegate(EComponentWithViewSupportHolder holder) {
6465
}
6566

6667
@Override
67-
public JBlock getAddPreferencesFromResourceBlock() {
68-
if (addPreferencesFromResourceBlock == null) {
68+
public JBlock getAddPreferencesFromResourceInjectionBlock() {
69+
if (addPreferencesFromResourceInjectionBlock == null) {
6970
setAddPreferencesFromResourceBlock();
7071
}
71-
return addPreferencesFromResourceBlock;
72+
return addPreferencesFromResourceInjectionBlock;
73+
}
74+
75+
@Override
76+
public JBlock getAddPreferencesFromResourceAfterInjectionBlock() {
77+
if (addPreferencesFromResourceAfterInjectionBlock == null) {
78+
setAddPreferencesFromResourceBlock();
79+
}
80+
return addPreferencesFromResourceAfterInjectionBlock;
7281
}
7382

7483
private void setAddPreferencesFromResourceBlock() {
7584
JMethod method = getGeneratedClass().method(PUBLIC, codeModel().VOID, "addPreferencesFromResource");
7685
method.annotate(Override.class);
7786
JVar preferencesResIdParam = method.param(int.class, "preferencesResId");
7887
method.body().invoke(JExpr._super(), "addPreferencesFromResource").arg(preferencesResIdParam);
79-
addPreferencesFromResourceBlock = method.body();
88+
addPreferencesFromResourceInjectionBlock = method.body().blockSimple();
89+
addPreferencesFromResourceAfterInjectionBlock = method.body().blockSimple();
8090
}
8191

8292
private JInvocation findPreferenceByKey(JFieldRef idRef) {
@@ -90,7 +100,7 @@ public void assignFindPreferenceByKey(JFieldRef idRef, AbstractJClass preference
90100
String idRefString = idRef.name();
91101
FoundPreferenceHolder foundViewHolder = (FoundPreferenceHolder) holder.foundHolders.get(idRefString);
92102

93-
JBlock block = getAddPreferencesFromResourceBlock();
103+
JBlock block = getAddPreferencesFromResourceInjectionBlock();
94104
IJExpression assignExpression;
95105

96106
if (foundViewHolder != null) {
@@ -129,7 +139,7 @@ public AbstractJClass getBasePreferenceClass() {
129139

130140
private FoundPreferenceHolder createFoundPreferenceAndIfNotNullBlock(JFieldRef idRef, AbstractJClass preferenceClass) {
131141
IJExpression findPreferenceExpression = findPreferenceByKey(idRef);
132-
JBlock block = getAddPreferencesFromResourceBlock().blockSimple();
142+
JBlock block = getAddPreferencesFromResourceInjectionBlock().blockSimple();
133143

134144
if (preferenceClass == null) {
135145
preferenceClass = basePreferenceClass;

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/CorePlugin.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,16 @@ public List<AnnotationHandler<?>> getHandlers(AndroidAnnotationsEnvironment andr
195195
annotationHandlers.add(new OnActivityResultHandler(androidAnnotationEnv));
196196

197197
annotationHandlers.add(new IgnoredWhenDetachedHandler(androidAnnotationEnv));
198-
/* After injection methods must be after injections */
198+
199199
annotationHandlers.add(new AfterInjectHandler(androidAnnotationEnv));
200200
annotationHandlers.add(new AfterExtrasHandler(androidAnnotationEnv));
201201
annotationHandlers.add(new AfterViewsHandler(androidAnnotationEnv));
202202

203-
/* preference screen handler must be after injections */
204203
annotationHandlers.add(new PreferenceScreenHandler(androidAnnotationEnv));
205204
annotationHandlers.add(new PreferenceHeadersHandler(androidAnnotationEnv));
206-
/* Preference injections must be after preference screen handler */
207205
annotationHandlers.add(new PreferenceByKeyHandler(androidAnnotationEnv));
208206
annotationHandlers.add(new PreferenceChangeHandler(androidAnnotationEnv));
209207
annotationHandlers.add(new PreferenceClickHandler(androidAnnotationEnv));
210-
/* After preference injection methods must be after preference injections */
211208
annotationHandlers.add(new AfterPreferencesHandler(androidAnnotationEnv));
212209

213210
if (androidAnnotationEnv.getOptionBooleanValue(OPTION_TRACE)) {

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/AbstractFragmentByHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final void process(Element element, EComponentWithViewSupportHolder holde
6666
boolean isNativeFragment = nativeFragmentElement != null && annotationHelper.isSubtype(elementType, nativeFragmentElement.asType());
6767

6868
String fieldName = element.getSimpleName().toString();
69-
JBlock methodBody = holder.getOnViewChangedBody();
69+
JBlock methodBody = holder.getOnViewChangedBodyInjectionBlock();
7070

7171
if (holder instanceof EFragmentHolder) {
7272
boolean childFragment = annotationHelper.extractAnnotationParameter(element, "childFragment");

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/AbstractResHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void process(Element element, EComponentHolder holder) {
5959

6060
JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(element, resInnerClass, true);
6161

62-
JBlock methodBody = holder.getInitBody();
62+
JBlock methodBody = holder.getInitBodyInjectionBlock();
6363

6464
makeCall(fieldName, holder, methodBody, idRef);
6565
}

0 commit comments

Comments
 (0)