|
23 | 23 | import org.androidannotations.AndroidAnnotationsEnvironment; |
24 | 24 | import org.androidannotations.ElementValidation; |
25 | 25 | import org.androidannotations.handler.BaseAnnotationHandler; |
| 26 | +import org.androidannotations.handler.MethodInjectionHandler; |
| 27 | +import org.androidannotations.helper.InjectHelper; |
26 | 28 | import org.androidannotations.holder.EComponentHolder; |
27 | 29 | import org.androidannotations.rest.spring.annotations.Rest; |
28 | 30 | import org.androidannotations.rest.spring.annotations.RestService; |
29 | 31 |
|
30 | 32 | import com.helger.jcodemodel.AbstractJClass; |
| 33 | +import com.helger.jcodemodel.IJAssignmentTarget; |
31 | 34 | import com.helger.jcodemodel.JBlock; |
32 | 35 | import com.helger.jcodemodel.JExpr; |
33 | | -import com.helger.jcodemodel.JFieldRef; |
34 | 36 |
|
35 | | -public class RestServiceHandler extends BaseAnnotationHandler<EComponentHolder> { |
| 37 | +public class RestServiceHandler extends BaseAnnotationHandler<EComponentHolder>implements MethodInjectionHandler<EComponentHolder> { |
| 38 | + |
| 39 | + private final InjectHelper<EComponentHolder> injectHelper; |
36 | 40 |
|
37 | 41 | public RestServiceHandler(AndroidAnnotationsEnvironment environment) { |
38 | 42 | super(RestService.class, environment); |
| 43 | + injectHelper = new InjectHelper<>(validatorHelper, this); |
39 | 44 | } |
40 | 45 |
|
41 | 46 | @Override |
42 | 47 | public void validate(Element element, ElementValidation validation) { |
43 | | - validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validation); |
| 48 | + injectHelper.validate(RestService.class, element, validation); |
44 | 49 |
|
45 | 50 | validatorHelper.isNotPrivate(element, validation); |
46 | 51 |
|
47 | | - validatorHelper.typeHasAnnotation(Rest.class, element, validation); |
| 52 | + Element param = injectHelper.getParam(element); |
| 53 | + validatorHelper.typeHasAnnotation(Rest.class, param, validation); |
48 | 54 | } |
49 | 55 |
|
50 | 56 | @Override |
51 | 57 | public void process(Element element, EComponentHolder holder) { |
52 | | - String fieldName = element.getSimpleName().toString(); |
| 58 | + injectHelper.process(element, holder); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public JBlock getInvocationBlock(EComponentHolder holder) { |
| 63 | + return holder.getInitBodyInjectionBlock(); |
| 64 | + } |
53 | 65 |
|
54 | | - TypeMirror fieldTypeMirror = element.asType(); |
| 66 | + @Override |
| 67 | + public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) { |
| 68 | + TypeMirror fieldTypeMirror = param.asType(); |
55 | 69 | TypeMirror erasedFieldTypeMirror = getProcessingEnvironment().getTypeUtils().erasure(fieldTypeMirror); |
56 | 70 | String interfaceName = erasedFieldTypeMirror.toString(); |
57 | 71 |
|
58 | 72 | String generatedClassName = interfaceName + classSuffix(); |
59 | 73 |
|
60 | 74 | AbstractJClass clazz = codeModelHelper.narrowGeneratedClass(getJClass(generatedClassName), fieldTypeMirror); |
61 | 75 |
|
62 | | - JBlock methodBody = holder.getInitBodyInjectionBlock(); |
63 | | - |
64 | | - JFieldRef field = JExpr.ref(fieldName); |
| 76 | + targetBlock.add(fieldRef.assign(JExpr._new(clazz).arg(holder.getContextRef()))); |
| 77 | + } |
65 | 78 |
|
66 | | - methodBody.assign(field, JExpr._new(clazz).arg(holder.getContextRef())); |
| 79 | + @Override |
| 80 | + public void validateEnclosingElement(Element element, ElementValidation valid) { |
| 81 | + validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, valid); |
67 | 82 | } |
68 | 83 | } |
0 commit comments