|
16 | 16 | package org.androidannotations.internal.core.handler; |
17 | 17 |
|
18 | 18 | import static com.helger.jcodemodel.JExpr._null; |
19 | | -import static com.helger.jcodemodel.JExpr.ref; |
20 | 19 |
|
21 | 20 | import javax.lang.model.element.Element; |
| 21 | +import javax.lang.model.element.ElementKind; |
22 | 22 | import javax.lang.model.type.TypeMirror; |
23 | 23 |
|
24 | 24 | import org.androidannotations.AndroidAnnotationsEnvironment; |
|
27 | 27 | import org.androidannotations.annotations.EBean; |
28 | 28 | import org.androidannotations.annotations.NonConfigurationInstance; |
29 | 29 | import org.androidannotations.handler.BaseAnnotationHandler; |
| 30 | +import org.androidannotations.handler.MethodInjectionHandler; |
| 31 | +import org.androidannotations.helper.InjectHelper; |
30 | 32 | import org.androidannotations.holder.EBeanHolder; |
31 | 33 | import org.androidannotations.holder.EComponentHolder; |
32 | 34 |
|
33 | 35 | import com.helger.jcodemodel.AbstractJClass; |
| 36 | +import com.helger.jcodemodel.IJAssignmentTarget; |
| 37 | +import com.helger.jcodemodel.IJStatement; |
34 | 38 | import com.helger.jcodemodel.JBlock; |
35 | | -import com.helger.jcodemodel.JFieldRef; |
| 39 | +import com.helger.jcodemodel.JConditional; |
36 | 40 | import com.helger.jcodemodel.JInvocation; |
37 | 41 |
|
38 | | -public class BeanHandler extends BaseAnnotationHandler<EComponentHolder> { |
| 42 | +public class BeanHandler extends BaseAnnotationHandler<EComponentHolder>implements MethodInjectionHandler<EComponentHolder> { |
| 43 | + |
| 44 | + private final InjectHelper<EComponentHolder> injectHelper; |
39 | 45 |
|
40 | 46 | public BeanHandler(AndroidAnnotationsEnvironment environment) { |
41 | 47 | super(Bean.class, environment); |
| 48 | + injectHelper = new InjectHelper<>(validatorHelper, this); |
42 | 49 | } |
43 | 50 |
|
44 | 51 | @Override |
45 | 52 | public void validate(Element element, ElementValidation validation) { |
46 | | - validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, validation); |
| 53 | + injectHelper.validate(Bean.class, element, validation); |
| 54 | + |
| 55 | + validatorHelper.typeOrTargetValueHasAnnotation(EBean.class, element, validation); |
47 | 56 |
|
48 | 57 | validatorHelper.isNotPrivate(element, validation); |
| 58 | + } |
49 | 59 |
|
50 | | - validatorHelper.typeOrTargetValueHasAnnotation(EBean.class, element, validation); |
| 60 | + @Override |
| 61 | + public void process(Element element, EComponentHolder holder) { |
| 62 | + injectHelper.process(element, holder); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public JBlock getInvocationBlock(EComponentHolder holder) { |
| 67 | + return holder.getInitBodyInjectionBlock(); |
51 | 68 | } |
52 | 69 |
|
53 | 70 | @Override |
54 | | - public void process(Element element, EComponentHolder holder) throws Exception { |
| 71 | + public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) { |
55 | 72 | TypeMirror typeMirror = annotationHelper.extractAnnotationClassParameter(element); |
56 | 73 | if (typeMirror == null) { |
57 | | - typeMirror = element.asType(); |
| 74 | + typeMirror = param.asType(); |
58 | 75 | typeMirror = getProcessingEnvironment().getTypeUtils().erasure(typeMirror); |
59 | 76 | } |
60 | | - |
61 | 77 | String typeQualifiedName = typeMirror.toString(); |
62 | 78 | AbstractJClass injectedClass = getJClass(annotationHelper.generatedClassQualifiedNameFromQualifiedName(typeQualifiedName)); |
| 79 | + JInvocation beanInstance = injectedClass.staticInvoke(EBeanHolder.GET_INSTANCE_METHOD_NAME).arg(holder.getContextRef()); |
63 | 80 |
|
64 | | - String fieldName = element.getSimpleName().toString(); |
65 | | - JFieldRef beanField = ref(fieldName); |
66 | | - JBlock block = holder.getInitBodyInjectionBlock(); |
67 | | - |
68 | | - boolean hasNonConfigurationInstanceAnnotation = element.getAnnotation(NonConfigurationInstance.class) != null; |
69 | | - if (hasNonConfigurationInstanceAnnotation) { |
70 | | - block = block._if(beanField.eq(_null()))._then(); |
| 81 | + IJStatement assignment = fieldRef.assign(beanInstance); |
| 82 | + if (param.getKind() == ElementKind.FIELD) { |
| 83 | + boolean hasNonConfigurationInstanceAnnotation = element.getAnnotation(NonConfigurationInstance.class) != null; |
| 84 | + if (hasNonConfigurationInstanceAnnotation) { |
| 85 | + JConditional conditional = targetBlock._if(fieldRef.eq(_null())); |
| 86 | + conditional._then().add(assignment); |
| 87 | + assignment = conditional; |
| 88 | + } |
71 | 89 | } |
72 | 90 |
|
73 | | - JInvocation getInstance = injectedClass.staticInvoke(EBeanHolder.GET_INSTANCE_METHOD_NAME).arg(holder.getContextRef()); |
74 | | - block.assign(beanField, getInstance); |
| 91 | + targetBlock.add(assignment); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void validateEnclosingElement(Element element, ElementValidation valid) { |
| 96 | + validatorHelper.enclosingElementHasEnhancedComponentAnnotation(element, valid); |
75 | 97 | } |
76 | 98 | } |
0 commit comments