2121import static com .helger .jcodemodel .JExpr .cast ;
2222import static com .helger .jcodemodel .JExpr .invoke ;
2323import static com .helger .jcodemodel .JExpr .lit ;
24- import static com .helger .jcodemodel .JExpr .ref ;
2524
2625import javax .lang .model .element .Element ;
2726
2827import org .androidannotations .AndroidAnnotationsEnvironment ;
2928import org .androidannotations .ElementValidation ;
3029import org .androidannotations .annotations .HttpsClient ;
3130import org .androidannotations .handler .BaseAnnotationHandler ;
31+ import org .androidannotations .handler .MethodInjectionHandler ;
32+ import org .androidannotations .helper .InjectHelper ;
3233import org .androidannotations .holder .EComponentHolder ;
3334import org .androidannotations .internal .process .ProcessHolder ;
3435import org .androidannotations .rclass .IRClass ;
3536import org .androidannotations .rclass .IRInnerClass ;
3637
38+ import com .helger .jcodemodel .IJAssignmentTarget ;
3739import com .helger .jcodemodel .IJExpression ;
3840import com .helger .jcodemodel .JBlock ;
3941import com .helger .jcodemodel .JCatchBlock ;
4547import com .helger .jcodemodel .JTryBlock ;
4648import com .helger .jcodemodel .JVar ;
4749
48- public class HttpsClientHandler extends BaseAnnotationHandler <EComponentHolder > {
50+ public class HttpsClientHandler extends BaseAnnotationHandler <EComponentHolder >implements MethodInjectionHandler <EComponentHolder > {
51+
52+ private final InjectHelper <EComponentHolder > injectHelper ;
4953
5054 public HttpsClientHandler (AndroidAnnotationsEnvironment environment ) {
5155 super (HttpsClient .class , environment );
56+ injectHelper = new InjectHelper <>(validatorHelper , this );
5257 }
5358
5459 @ Override
5560 public void validate (Element element , ElementValidation validation ) {
56- validatorHelper . enclosingElementHasEnhancedComponentAnnotation ( element , validation );
61+ injectHelper . validate ( HttpsClient . class , element , validation );
5762
5863 validatorHelper .annotationParameterIsOptionalValidResId (element , IRClass .Res .RAW , "keyStore" , validation );
5964 validatorHelper .annotationParameterIsOptionalValidResId (element , IRClass .Res .RAW , "trustStore" , validation );
@@ -63,6 +68,16 @@ public void validate(Element element, ElementValidation validation) {
6368
6469 @ Override
6570 public void process (Element element , EComponentHolder holder ) throws Exception {
71+ injectHelper .process (element , holder );
72+ }
73+
74+ @ Override
75+ public JBlock getInvocationBlock (EComponentHolder holder ) {
76+ return holder .getInitBodyInjectionBlock ();
77+ }
78+
79+ @ Override
80+ public void assignValue (JBlock targetBlock , IJAssignmentTarget fieldRef , EComponentHolder holder , Element element , Element param ) {
6681 IRInnerClass rInnerClass = getEnvironment ().getRClass ().get (IRClass .Res .RAW );
6782 HttpsClient annotation = element .getAnnotation (HttpsClient .class );
6883 JFieldRef trustStoreRawIdRef = annotationHelper .extractOneAnnotationFieldRef (element , getTarget (), rInnerClass , false , "trustStore" , "trustStoreResName" );
@@ -74,9 +89,6 @@ public void process(Element element, EComponentHolder holder) throws Exception {
7489 boolean useCustomTrustStore = trustStoreRawIdRef != null ;
7590 boolean useCustomKeyStore = keyStoreRawIdRef != null ;
7691
77- String fieldName = element .getSimpleName ().toString ();
78- JBlock methodBody = holder .getInitBodyInjectionBlock ();
79-
8092 ProcessHolder .Classes classes = getClasses ();
8193
8294 JDefinedClass jAnonClass = getCodeModel ().anonymousClass (classes .DEFAULT_HTTP_CLIENT );
@@ -177,6 +189,11 @@ public void process(Element element, EComponentHolder holder) throws Exception {
177189 jCatchBlock .body ().add (jVarExceptionParam .invoke ("printStackTrace" ));
178190 jCatchBlock .body ()._return (_super ().invoke ("createClientConnectionManager" ));
179191
180- methodBody .assign (ref (fieldName ), _new (jAnonClass ));
192+ targetBlock .add (fieldRef .assign (_new (jAnonClass )));
193+ }
194+
195+ @ Override
196+ public void validateEnclosingElement (Element element , ElementValidation valid ) {
197+ validatorHelper .enclosingElementHasEnhancedComponentAnnotation (element , valid );
181198 }
182199}
0 commit comments