Skip to content

Commit 1c2575c

Browse files
committed
use the view field for FoundViewHolder
1 parent a2f3bfd commit 1c2575c

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.androidannotations.internal.helper.ViewNotifierHelper;
3838

3939
import com.helger.jcodemodel.AbstractJClass;
40+
import com.helger.jcodemodel.IJAssignmentTarget;
4041
import com.helger.jcodemodel.IJExpression;
4142
import com.helger.jcodemodel.JBlock;
4243
import com.helger.jcodemodel.JDefinedClass;
@@ -127,16 +128,20 @@ public JInvocation findViewById(JFieldRef idRef) {
127128
}
128129

129130
public FoundViewHolder getFoundViewHolder(JFieldRef idRef, AbstractJClass viewClass) {
131+
return getFoundViewHolder(idRef, viewClass, null);
132+
}
133+
134+
public FoundViewHolder getFoundViewHolder(JFieldRef idRef, AbstractJClass viewClass, IJAssignmentTarget fieldRef) {
130135
String idRefString = idRef.name();
131136
FoundViewHolder foundViewHolder = (FoundViewHolder) foundHolders.get(idRefString);
132137
if (foundViewHolder == null) {
133-
foundViewHolder = createFoundViewAndIfNotNullBlock(idRef, viewClass);
138+
foundViewHolder = createFoundViewAndIfNotNullBlock(idRef, viewClass, fieldRef);
134139
foundHolders.put(idRefString, foundViewHolder);
135140
}
136141
return foundViewHolder;
137142
}
138143

139-
protected FoundViewHolder createFoundViewAndIfNotNullBlock(JFieldRef idRef, AbstractJClass viewClass) {
144+
protected FoundViewHolder createFoundViewAndIfNotNullBlock(JFieldRef idRef, AbstractJClass viewClass, IJAssignmentTarget fieldRef) {
140145
IJExpression findViewExpression = findViewById(idRef);
141146
JBlock block = getOnViewChangedBodyBeforeInjectionBlock();
142147

@@ -146,11 +151,17 @@ protected FoundViewHolder createFoundViewAndIfNotNullBlock(JFieldRef idRef, Abst
146151
findViewExpression = cast(viewClass, findViewExpression);
147152
}
148153

149-
JVar view = block.decl(viewClass, "view_" + idRef.name(), findViewExpression);
150-
if (viewClass.isParameterized()) {
151-
codeModelHelper.addSuppressWarnings(view, "unchecked");
154+
IJAssignmentTarget foundView = fieldRef;
155+
if (foundView == null) {
156+
JVar view = block.decl(viewClass, "view_" + idRef.name(), findViewExpression);
157+
if (viewClass.isParameterized()) {
158+
codeModelHelper.addSuppressWarnings(view, "unchecked");
159+
}
160+
foundView = view;
161+
} else {
162+
block.add(foundView.assign(findViewExpression));
152163
}
153-
return new FoundViewHolder(this, viewClass, view, getOnViewChangedBodyInjectionBlock());
164+
return new FoundViewHolder(this, viewClass, foundView, getOnViewChangedBodyInjectionBlock());
154165
}
155166

156167
public JMethod getFindNativeFragmentById() {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.helger.jcodemodel.JExpr.ref;
1919

2020
import javax.lang.model.element.Element;
21+
import javax.lang.model.element.ElementKind;
2122
import javax.lang.model.type.TypeMirror;
2223

2324
import org.androidannotations.AndroidAnnotationsEnvironment;
@@ -29,6 +30,7 @@
2930
import org.androidannotations.helper.InjectHelper;
3031
import org.androidannotations.holder.EComponentWithViewSupportHolder;
3132
import org.androidannotations.holder.EFragmentHolder;
33+
import org.androidannotations.holder.FoundViewHolder;
3234
import org.androidannotations.rclass.IRClass;
3335

3436
import com.helger.jcodemodel.AbstractJClass;
@@ -80,7 +82,14 @@ public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, ECompon
8082
JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(element, IRClass.Res.ID, true);
8183
AbstractJClass viewClass = codeModelHelper.typeMirrorToJClass(uiFieldTypeMirror);
8284

83-
targetBlock.add(fieldRef.assign(holder.getFoundViewHolder(idRef, viewClass).getOrCastRef(viewClass)));
85+
IJAssignmentTarget viewHolderTarget = null;
86+
if (element.getKind() == ElementKind.FIELD) {
87+
viewHolderTarget = fieldRef;
88+
}
89+
FoundViewHolder viewHolder = holder.getFoundViewHolder(idRef, viewClass, viewHolderTarget);
90+
if (!viewHolder.getRef().equals(viewHolderTarget)) {
91+
targetBlock.add(fieldRef.assign(viewHolder.getOrCastRef(viewClass)));
92+
}
8493
}
8594

8695
@Override

0 commit comments

Comments
 (0)