1515 */
1616package org .androidannotations .internal .core .handler ;
1717
18- import static com .helger .jcodemodel .JExpr .assign ;
1918import static com .helger .jcodemodel .JExpr .cast ;
20- import static com .helger .jcodemodel .JExpr .ref ;
2119
2220import javax .lang .model .element .Element ;
2321import javax .lang .model .element .TypeElement ;
2725import org .androidannotations .ElementValidation ;
2826import org .androidannotations .annotations .SystemService ;
2927import org .androidannotations .handler .BaseAnnotationHandler ;
28+ import org .androidannotations .handler .MethodInjectionHandler ;
3029import org .androidannotations .helper .CanonicalNameConstants ;
30+ import org .androidannotations .helper .InjectHelper ;
3131import org .androidannotations .holder .EComponentHolder ;
3232import org .androidannotations .internal .core .model .AndroidSystemServices ;
3333
3434import com .helger .jcodemodel .AbstractJClass ;
35+ import com .helger .jcodemodel .IJAssignmentTarget ;
3536import com .helger .jcodemodel .IJExpression ;
36- import com .helger .jcodemodel .IJStatement ;
3737import com .helger .jcodemodel .JBlock ;
38- import com .helger .jcodemodel .JConditional ;
3938import com .helger .jcodemodel .JFieldRef ;
4039import com .helger .jcodemodel .JInvocation ;
40+ import com .helger .jcodemodel .JOp ;
4141
42- public class SystemServiceHandler extends BaseAnnotationHandler <EComponentHolder > {
42+ public class SystemServiceHandler extends BaseAnnotationHandler <EComponentHolder >implements MethodInjectionHandler <EComponentHolder > {
43+
44+ private final InjectHelper <EComponentHolder > injectHelper ;
4345
4446 public SystemServiceHandler (AndroidAnnotationsEnvironment environment ) {
4547 super (SystemService .class , environment );
48+ injectHelper = new InjectHelper <>(validatorHelper , this );
4649 }
4750
4851 @ Override
4952 public void validate (Element element , ElementValidation validation ) {
50- validatorHelper . enclosingElementHasEnhancedComponentAnnotation ( element , validation );
53+ injectHelper . validate ( SystemService . class , element , validation );
5154
5255 validatorHelper .androidService (element , validation );
5356
@@ -56,46 +59,49 @@ public void validate(Element element, ElementValidation validation) {
5659
5760 @ Override
5861 public void process (Element element , EComponentHolder holder ) {
59- String fieldName = element .getSimpleName ().toString ();
62+ injectHelper .process (element , holder );
63+ }
64+
65+ @ Override
66+ public JBlock getInvocationBlock (EComponentHolder holder ) {
67+ return holder .getInitBodyInjectionBlock ();
68+ }
6069
61- TypeMirror serviceType = element .asType ();
70+ @ Override
71+ public void assignValue (JBlock targetBlock , IJAssignmentTarget fieldRef , EComponentHolder holder , Element element , Element param ) {
72+ TypeMirror serviceType = param .asType ();
6273 String fieldTypeQualifiedName = serviceType .toString ();
6374
6475 JFieldRef serviceRef = new AndroidSystemServices (getEnvironment ()).getServiceConstantRef (serviceType );
65-
66- JBlock methodBody = holder .getInitBodyInjectionBlock ();
67-
6876 if (CanonicalNameConstants .APP_WIDGET_MANAGER .equals (fieldTypeQualifiedName )) {
69- createSpecialInjection (holder , fieldName , fieldTypeQualifiedName , serviceRef , methodBody , 21 , "LOLLIPOP" , getClasses ().APP_WIDGET_MANAGER , "getInstance" , true );
77+ targetBlock . add ( fieldRef . assign ( createSpecialInjection (holder , fieldTypeQualifiedName , serviceRef , 21 , "LOLLIPOP" , getClasses ().APP_WIDGET_MANAGER , "getInstance" , true )) );
7078 } else {
71- methodBody .add (createNormalInjection (holder , fieldName , fieldTypeQualifiedName , serviceRef , methodBody ));
79+ targetBlock .add (fieldRef . assign ( createNormalInjection (holder , fieldTypeQualifiedName , serviceRef ) ));
7280 }
7381 }
7482
7583 @ SuppressWarnings ("checkstyle:parameternumber" )
76- private void createSpecialInjection (EComponentHolder holder , String fieldName , String fieldTypeQualifiedName , JFieldRef serviceRef , JBlock methodBody , int apiLevel , String apiLevelName ,
77- AbstractJClass serviceClass , String injectionMethodName , boolean contextNeeded ) {
84+ private IJExpression createSpecialInjection (EComponentHolder holder , String fieldTypeQualifiedName , JFieldRef serviceRef , int apiLevel , String apiLevelName , AbstractJClass serviceClass ,
85+ String injectionMethodName , boolean invocationRequiresContext ) {
7886 if (getEnvironment ().getAndroidManifest ().getMinSdkVersion () >= apiLevel ) {
79- methodBody . add ( createNormalInjection (holder , fieldName , fieldTypeQualifiedName , serviceRef , methodBody ) );
87+ return createNormalInjection (holder , fieldTypeQualifiedName , serviceRef );
8088 } else {
81- JInvocation injectionMethodInvokation = serviceClass .staticInvoke (injectionMethodName );
82- if (contextNeeded ) {
83- injectionMethodInvokation .arg (holder .getContextRef ());
89+ JInvocation serviceClassInvocation = serviceClass .staticInvoke (injectionMethodName );
90+ if (invocationRequiresContext ) {
91+ serviceClassInvocation .arg (holder .getContextRef ());
8492 }
85- IJStatement oldInjection = assign (ref (fieldName ), injectionMethodInvokation );
86-
8793 if (isApiOnClasspath (apiLevelName )) {
88- JConditional conditional = methodBody . _if ( getClasses ().BUILD_VERSION .staticRef ("SDK_INT" ).gte (getClasses ().BUILD_VERSION_CODES .staticRef (apiLevelName ) ));
89- conditional . _then (). add ( createNormalInjection (holder , fieldName , fieldTypeQualifiedName , serviceRef , methodBody ) );
90- conditional . _else (). add ( oldInjection );
94+ IJExpression condition = getClasses ().BUILD_VERSION .staticRef ("SDK_INT" ).gte (getClasses ().BUILD_VERSION_CODES .staticRef (apiLevelName ));
95+ IJExpression normalInjection = createNormalInjection (holder , fieldTypeQualifiedName , serviceRef );
96+ return JOp . cond ( condition , normalInjection , serviceClassInvocation );
9197 } else {
92- methodBody . add ( oldInjection ) ;
98+ return serviceClassInvocation ;
9399 }
94100 }
95101 }
96102
97- private IJStatement createNormalInjection (EComponentHolder holder , String fieldName , String fieldTypeQualifiedName , JFieldRef serviceRef , JBlock methodBody ) {
98- return assign ( ref ( fieldName ), cast (getJClass (fieldTypeQualifiedName ), getAppropriateContextRef (holder , fieldTypeQualifiedName ).invoke ("getSystemService" ).arg (serviceRef ) ));
103+ private IJExpression createNormalInjection (EComponentHolder holder , String fieldTypeQualifiedName , JFieldRef serviceRef ) {
104+ return cast (getJClass (fieldTypeQualifiedName ), getAppropriateContextRef (holder , fieldTypeQualifiedName ).invoke ("getSystemService" ).arg (serviceRef ));
99105 }
100106
101107 private IJExpression getAppropriateContextRef (EComponentHolder holder , String fieldTypeQualifiedName ) {
@@ -115,4 +121,9 @@ private boolean isApiOnClasspath(String apiName) {
115121 }
116122 return false ;
117123 }
124+
125+ @ Override
126+ public void validateEnclosingElement (Element element , ElementValidation valid ) {
127+ validatorHelper .enclosingElementHasEnhancedComponentAnnotation (element , valid );
128+ }
118129}
0 commit comments