2222import static com .helger .jcodemodel .JMod .STATIC ;
2323
2424import javax .lang .model .element .Element ;
25+ import javax .lang .model .element .ElementKind ;
26+ import javax .lang .model .type .DeclaredType ;
2527import javax .lang .model .type .TypeMirror ;
2628
2729import org .androidannotations .AndroidAnnotationsEnvironment ;
2830import org .androidannotations .ElementValidation ;
2931import org .androidannotations .annotations .Extra ;
3032import org .androidannotations .handler .BaseAnnotationHandler ;
33+ import org .androidannotations .handler .MethodInjectionHandler ;
3134import org .androidannotations .helper .BundleHelper ;
3235import org .androidannotations .helper .CaseHelper ;
36+ import org .androidannotations .helper .InjectHelper ;
3337import org .androidannotations .holder .HasExtras ;
3438import org .androidannotations .holder .HasIntentBuilder ;
3539
3640import com .helger .jcodemodel .AbstractJClass ;
41+ import com .helger .jcodemodel .IJAssignmentTarget ;
3742import com .helger .jcodemodel .IJExpression ;
3843import com .helger .jcodemodel .JBlock ;
3944import com .helger .jcodemodel .JExpr ;
40- import com .helger .jcodemodel .JFieldRef ;
4145import com .helger .jcodemodel .JFieldVar ;
4246import com .helger .jcodemodel .JMethod ;
4347import com .helger .jcodemodel .JVar ;
4448
45- public class ExtraHandler extends BaseAnnotationHandler <HasExtras > {
49+ public class ExtraHandler extends BaseAnnotationHandler <HasExtras >implements MethodInjectionHandler <HasExtras > {
50+
51+ private final InjectHelper <HasExtras > injectHelper ;
4652
4753 public ExtraHandler (AndroidAnnotationsEnvironment environment ) {
4854 super (Extra .class , environment );
55+ injectHelper = new InjectHelper <>(validatorHelper , this );
4956 }
5057
5158 @ Override
@@ -55,15 +62,25 @@ public void validate(Element element, ElementValidation validation) {
5562 * setIntent() method can be overridden
5663 */
5764
58- validatorHelper . enclosingElementHasEActivity ( element , validation );
65+ injectHelper . validate ( Extra . class , element , validation );
5966
6067 validatorHelper .isNotPrivate (element , validation );
6168
62- validatorHelper .canBePutInABundle (element , validation );
69+ Element param = injectHelper .getParam (element );
70+ validatorHelper .canBePutInABundle (param , validation );
6371 }
6472
6573 @ Override
6674 public void process (Element element , HasExtras holder ) {
75+ injectHelper .process (element , holder );
76+ }
77+
78+ @ Override
79+ public JBlock getInvocationBlock (HasExtras holder ) {
80+ return holder .getInjectExtrasBlock ();
81+ }
82+
83+ public void assignValue (JBlock targetBlock , IJAssignmentTarget fieldRef , HasExtras holder , Element element , Element param ) {
6784 Extra annotation = element .getAnnotation (Extra .class );
6885 String extraKey = annotation .value ();
6986 String fieldName = element .getSimpleName ().toString ();
@@ -72,12 +89,34 @@ public void process(Element element, HasExtras holder) {
7289 }
7390
7491 JFieldVar extraKeyStaticField = createStaticExtraField (holder , extraKey , fieldName );
75- injectExtraInComponent (element , holder , extraKeyStaticField , fieldName );
76-
7792 if (holder instanceof HasIntentBuilder ) {
7893 String docComment = getProcessingEnvironment ().getElementUtils ().getDocComment (element );
79- createIntentInjectionMethod (element , (HasIntentBuilder ) holder , extraKeyStaticField , fieldName , docComment );
94+ createIntentInjectionMethod (param , (HasIntentBuilder ) holder , extraKeyStaticField , fieldName , docComment );
8095 }
96+
97+ JMethod injectExtrasMethod = holder .getInjectExtrasMethod ();
98+ JVar extras = holder .getInjectExtras ();
99+
100+ DeclaredType enclosingClassType ;
101+ if (param .getKind () == ElementKind .PARAMETER ) {
102+ enclosingClassType = (DeclaredType ) param .getEnclosingElement ().getEnclosingElement ().asType ();
103+ } else {
104+ enclosingClassType = (DeclaredType ) param .getEnclosingElement ().asType ();
105+ }
106+ TypeMirror type = codeModelHelper .getActualType (param , enclosingClassType , holder );
107+ AbstractJClass elementClass = codeModelHelper .typeMirrorToJClass (param .asType ());
108+ BundleHelper bundleHelper = new BundleHelper (getEnvironment (), type );
109+
110+ IJExpression intent = invoke ("getIntent" );
111+ IJExpression restoreMethodCall = bundleHelper .getExpressionToRestoreFromIntentOrBundle (elementClass , intent , extras , extraKeyStaticField , injectExtrasMethod );
112+
113+ JBlock ifContainsKey = targetBlock ._if (JExpr .invoke (extras , "containsKey" ).arg (extraKeyStaticField ))._then ();
114+ ifContainsKey .assign (fieldRef , restoreMethodCall );
115+ }
116+
117+ @ Override
118+ public void validateEnclosingElement (Element element , ElementValidation valid ) {
119+ validatorHelper .enclosingElementHasEActivity (element , valid );
81120 }
82121
83122 private JFieldVar createStaticExtraField (HasExtras holder , String extraKey , String fieldName ) {
@@ -89,23 +128,6 @@ private JFieldVar createStaticExtraField(HasExtras holder, String extraKey, Stri
89128 return staticExtraField ;
90129 }
91130
92- private void injectExtraInComponent (Element element , HasExtras hasExtras , JFieldVar extraKeyStaticField , String fieldName ) {
93- JMethod injectExtrasMethod = hasExtras .getInjectExtrasMethod ();
94- JVar extras = hasExtras .getInjectExtras ();
95- JBlock injectExtrasBlock = hasExtras .getInjectExtrasBlock ();
96-
97- TypeMirror type = codeModelHelper .getActualType (element , hasExtras );
98- AbstractJClass elementClass = codeModelHelper .typeMirrorToJClass (element .asType ());
99- BundleHelper bundleHelper = new BundleHelper (getEnvironment (), type );
100-
101- JFieldRef extraField = JExpr .ref (fieldName );
102- IJExpression intent = invoke ("getIntent" );
103- JBlock ifContainsKey = injectExtrasBlock ._if (JExpr .invoke (extras , "containsKey" ).arg (extraKeyStaticField ))._then ();
104-
105- IJExpression restoreMethodCall = bundleHelper .getExpressionToRestoreFromIntentOrBundle (elementClass , intent , extras , extraKeyStaticField , injectExtrasMethod );
106- ifContainsKey .assign (extraField , restoreMethodCall );
107- }
108-
109131 private void createIntentInjectionMethod (Element element , HasIntentBuilder holder , JFieldVar extraKeyStaticField , String fieldName , String docComment ) {
110132 holder .getIntentBuilder ().getPutExtraMethod (element .asType (), fieldName , extraKeyStaticField , docComment );
111133 }
0 commit comments