Skip to content

Commit cade871

Browse files
committed
allow to inject views into @ViewById annotated methods
1 parent 9d796cd commit cade871

4 files changed

Lines changed: 41 additions & 47 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/ViewById.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
* @see AfterViews
6262
*/
6363
@Retention(RetentionPolicy.CLASS)
64-
@Target(ElementType.FIELD)
64+
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
6565
public @interface ViewById {
6666

6767
/**

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -126,34 +126,6 @@ public JInvocation findViewById(JFieldRef idRef) {
126126
return findViewById;
127127
}
128128

129-
public void processViewById(JFieldRef idRef, AbstractJClass viewClass, JFieldRef fieldRef) {
130-
assignFindViewById(idRef, viewClass, fieldRef);
131-
}
132-
133-
public void assignFindViewById(JFieldRef idRef, AbstractJClass viewClass, JFieldRef fieldRef) {
134-
String idRefString = idRef.name();
135-
FoundViewHolder foundViewHolder = (FoundViewHolder) foundHolders.get(idRefString);
136-
137-
JBlock block = getOnViewChangedBodyInjectionBlock();
138-
IJExpression assignExpression;
139-
140-
if (foundViewHolder != null) {
141-
assignExpression = foundViewHolder.getOrCastRef(viewClass);
142-
} else {
143-
assignExpression = findViewById(idRef);
144-
if (viewClass != null && viewClass != getClasses().VIEW) {
145-
assignExpression = cast(viewClass, assignExpression);
146-
147-
if (viewClass.isParameterized()) {
148-
codeModelHelper.addSuppressWarnings(onViewChanged, "unchecked");
149-
}
150-
}
151-
foundHolders.put(idRefString, new FoundViewHolder(this, viewClass, fieldRef, block));
152-
}
153-
154-
block.assign(fieldRef, assignExpression);
155-
}
156-
157129
public FoundViewHolder getFoundViewHolder(JFieldRef idRef, AbstractJClass viewClass) {
158130
String idRefString = idRef.name();
159131
FoundViewHolder foundViewHolder = (FoundViewHolder) foundHolders.get(idRefString);
@@ -166,16 +138,19 @@ public FoundViewHolder getFoundViewHolder(JFieldRef idRef, AbstractJClass viewCl
166138

167139
protected FoundViewHolder createFoundViewAndIfNotNullBlock(JFieldRef idRef, AbstractJClass viewClass) {
168140
IJExpression findViewExpression = findViewById(idRef);
169-
JBlock block = getOnViewChangedBodyInjectionBlock().blockSimple();
141+
JBlock block = getOnViewChangedBodyBeforeInjectionBlock();
170142

171143
if (viewClass == null) {
172144
viewClass = getClasses().VIEW;
173145
} else if (viewClass != getClasses().VIEW) {
174146
findViewExpression = cast(viewClass, findViewExpression);
175147
}
176148

177-
JVar view = block.decl(viewClass, "view", findViewExpression);
178-
return new FoundViewHolder(this, viewClass, view, block);
149+
JVar view = block.decl(viewClass, "view_" + idRef.name(), findViewExpression);
150+
if (viewClass.isParameterized()) {
151+
codeModelHelper.addSuppressWarnings(view, "unchecked");
152+
}
153+
return new FoundViewHolder(this, viewClass, view, getOnViewChangedBodyInjectionBlock());
179154
}
180155

181156
public JMethod getFindNativeFragmentById() {

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,7 @@ private JBlock getOnDestroyViewAfterSuperBlock() {
263263
return onDestroyViewAfterSuperBlock;
264264
}
265265

266-
@Override
267-
public void processViewById(JFieldRef idRef, AbstractJClass viewClass, JFieldRef fieldRef) {
268-
super.processViewById(idRef, viewClass, fieldRef);
269-
clearInjectedView(fieldRef);
270-
}
271-
272-
private void clearInjectedView(JFieldRef fieldRef) {
266+
public void clearInjectedView(JFieldRef fieldRef) {
273267
JBlock block = getOnDestroyViewAfterSuperBlock();
274268
block.assign(fieldRef, _null());
275269
}

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,35 @@
2424
import org.androidannotations.ElementValidation;
2525
import org.androidannotations.annotations.ViewById;
2626
import org.androidannotations.handler.BaseAnnotationHandler;
27+
import org.androidannotations.handler.MethodInjectionHandler;
2728
import org.androidannotations.helper.IdValidatorHelper;
29+
import org.androidannotations.helper.InjectHelper;
2830
import org.androidannotations.holder.EComponentWithViewSupportHolder;
31+
import org.androidannotations.holder.EFragmentHolder;
2932
import org.androidannotations.rclass.IRClass;
3033

3134
import com.helger.jcodemodel.AbstractJClass;
35+
import com.helger.jcodemodel.IJAssignmentTarget;
36+
import com.helger.jcodemodel.JBlock;
3237
import com.helger.jcodemodel.JFieldRef;
3338

34-
public class ViewByIdHandler extends BaseAnnotationHandler<EComponentWithViewSupportHolder> {
39+
public class ViewByIdHandler extends BaseAnnotationHandler<EComponentWithViewSupportHolder>implements MethodInjectionHandler<EComponentWithViewSupportHolder> {
40+
41+
private final InjectHelper<EComponentWithViewSupportHolder> injectHelper;
3542

3643
public ViewByIdHandler(AndroidAnnotationsEnvironment environment) {
3744
super(ViewById.class, environment);
45+
injectHelper = new InjectHelper<>(validatorHelper, this);
3846
}
3947

4048
@Override
4149
public void validate(Element element, ElementValidation validation) {
42-
validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validation);
50+
injectHelper.validate(ViewById.class, element, validation);
4351

44-
validatorHelper.isDeclaredType(element, validation);
52+
Element param = injectHelper.getParam(element);
53+
validatorHelper.isDeclaredType(param, validation);
4554

46-
validatorHelper.extendsView(element, validation);
55+
validatorHelper.extendsView(param, validation);
4756

4857
validatorHelper.resIdsExist(element, IRClass.Res.ID, IdValidatorHelper.FallbackStrategy.USE_ELEMENT_NAME, validation);
4958

@@ -52,14 +61,30 @@ public void validate(Element element, ElementValidation validation) {
5261

5362
@Override
5463
public void process(Element element, EComponentWithViewSupportHolder holder) {
55-
String fieldName = element.getSimpleName().toString();
64+
injectHelper.process(element, holder);
65+
if (holder instanceof EFragmentHolder) {
66+
String fieldName = element.getSimpleName().toString();
67+
((EFragmentHolder) holder).clearInjectedView(ref(fieldName));
68+
}
69+
}
5670

57-
TypeMirror uiFieldTypeMirror = element.asType();
71+
@Override
72+
public JBlock getInvocationBlock(EComponentWithViewSupportHolder holder) {
73+
return holder.getOnViewChangedBodyInjectionBlock();
74+
}
75+
76+
@Override
77+
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentWithViewSupportHolder holder, Element element, Element param) {
78+
TypeMirror uiFieldTypeMirror = param.asType();
5879

5980
JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(element, IRClass.Res.ID, true);
6081
AbstractJClass viewClass = codeModelHelper.typeMirrorToJClass(uiFieldTypeMirror);
61-
JFieldRef fieldRef = ref(fieldName);
6282

63-
holder.processViewById(idRef, viewClass, fieldRef);
83+
targetBlock.add(fieldRef.assign(holder.getFoundViewHolder(idRef, viewClass).getOrCastRef(viewClass)));
84+
}
85+
86+
@Override
87+
public void validateEnclosingElement(Element element, ElementValidation valid) {
88+
validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, valid);
6489
}
6590
}

0 commit comments

Comments
 (0)