|
17 | 17 |
|
18 | 18 | import static com.helger.jcodemodel.JExpr.cast; |
19 | 19 | import static com.helger.jcodemodel.JExpr.lit; |
20 | | -import static com.helger.jcodemodel.JExpr.ref; |
21 | 20 |
|
22 | 21 | import javax.lang.model.element.Element; |
23 | 22 | import javax.lang.model.type.TypeMirror; |
|
26 | 25 | import org.androidannotations.ElementValidation; |
27 | 26 | import org.androidannotations.annotations.RootContext; |
28 | 27 | import org.androidannotations.handler.BaseAnnotationHandler; |
| 28 | +import org.androidannotations.handler.MethodInjectionHandler; |
29 | 29 | import org.androidannotations.helper.CanonicalNameConstants; |
| 30 | +import org.androidannotations.helper.InjectHelper; |
30 | 31 | import org.androidannotations.holder.EBeanHolder; |
31 | 32 |
|
32 | 33 | import com.helger.jcodemodel.AbstractJClass; |
| 34 | +import com.helger.jcodemodel.IJAssignmentTarget; |
33 | 35 | import com.helger.jcodemodel.IJExpression; |
34 | 36 | import com.helger.jcodemodel.JBlock; |
35 | 37 | import com.helger.jcodemodel.JConditional; |
36 | 38 | import com.helger.jcodemodel.JInvocation; |
37 | 39 |
|
38 | | -public class RootContextHandler extends BaseAnnotationHandler<EBeanHolder> { |
| 40 | +public class RootContextHandler extends BaseAnnotationHandler<EBeanHolder>implements MethodInjectionHandler<EBeanHolder> { |
| 41 | + |
| 42 | + private final InjectHelper<EBeanHolder> injectHelper; |
39 | 43 |
|
40 | 44 | public RootContextHandler(AndroidAnnotationsEnvironment environment) { |
41 | 45 | super(RootContext.class, environment); |
| 46 | + injectHelper = new InjectHelper<>(validatorHelper, this); |
42 | 47 | } |
43 | 48 |
|
44 | 49 | @Override |
45 | 50 | public void validate(Element element, ElementValidation validation) { |
46 | | - validatorHelper.enclosingElementHasEBeanAnnotation(element, validation); |
| 51 | + injectHelper.validate(RootContext.class, element, validation); |
47 | 52 |
|
48 | | - validatorHelper.extendsContext(element, validation); |
| 53 | + Element param = injectHelper.getParam(element); |
| 54 | + validatorHelper.extendsContext(param, validation); |
49 | 55 |
|
50 | 56 | validatorHelper.isNotPrivate(element, validation); |
51 | 57 | } |
52 | 58 |
|
53 | 59 | @Override |
54 | 60 | public void process(Element element, EBeanHolder holder) { |
55 | | - String fieldName = element.getSimpleName().toString(); |
| 61 | + injectHelper.process(element, holder); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public JBlock getInvocationBlock(EBeanHolder holder) { |
| 66 | + return holder.getInitBodyInjectionBlock(); |
| 67 | + } |
56 | 68 |
|
57 | | - TypeMirror elementType = element.asType(); |
| 69 | + @Override |
| 70 | + public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EBeanHolder holder, Element element, Element param) { |
| 71 | + TypeMirror elementType = param.asType(); |
58 | 72 | String typeQualifiedName = elementType.toString(); |
59 | 73 |
|
60 | | - JBlock body = holder.getInitBodyInjectionBlock(); |
61 | 74 | IJExpression contextRef = holder.getContextRef(); |
62 | 75 |
|
63 | 76 | if (CanonicalNameConstants.CONTEXT.equals(typeQualifiedName)) { |
64 | | - body.assign(ref(fieldName), contextRef); |
| 77 | + targetBlock.add(fieldRef.assign(contextRef)); |
65 | 78 | } else { |
66 | 79 | AbstractJClass extendingContextClass = getEnvironment().getJClass(typeQualifiedName); |
67 | | - JConditional cond = body._if(holder.getContextRef()._instanceof(extendingContextClass)); |
68 | | - cond._then() // |
69 | | - .assign(ref(fieldName), cast(extendingContextClass, holder.getContextRef())); |
| 80 | + |
| 81 | + JConditional cond = getInvocationBlock(holder)._if(holder.getContextRef()._instanceof(extendingContextClass)); |
| 82 | + cond._then().add(fieldRef.assign(cast(extendingContextClass, holder.getContextRef()))); |
70 | 83 |
|
71 | 84 | JInvocation warningInvoke = getClasses().LOG.staticInvoke("w"); |
72 | 85 | warningInvoke.arg(holder.getGeneratedClass().name()); |
73 | | - IJExpression expr = lit("Due to Context class ").plus(holder.getContextRef().invoke("getClass").invoke("getSimpleName")).plus( |
74 | | - lit(", the @RootContext " + extendingContextClass.name() + " won't be populated")); |
75 | | - warningInvoke.arg(expr); |
76 | | - cond._else() // |
77 | | - .add(warningInvoke); |
| 86 | + warningInvoke.arg(lit("Due to Context class ").plus(holder.getContextRef().invoke("getClass").invoke("getSimpleName")) |
| 87 | + .plus(lit(", the @RootContext " + extendingContextClass.name() + " won't be populated"))); |
| 88 | + cond._else().add(warningInvoke); |
78 | 89 | } |
79 | 90 | } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void validateEnclosingElement(Element element, ElementValidation valid) { |
| 94 | + validatorHelper.enclosingElementHasEBeanAnnotation(element, valid); |
| 95 | + } |
80 | 96 | } |
0 commit comments