|
28 | 28 | import com.googlecode.androidannotations.model.Instruction; |
29 | 29 | import com.googlecode.androidannotations.model.MetaActivity; |
30 | 30 | import com.googlecode.androidannotations.model.MetaModel; |
| 31 | +import com.sun.codemodel.JBlock; |
| 32 | +import com.sun.codemodel.JCatchBlock; |
| 33 | +import com.sun.codemodel.JClass; |
| 34 | +import com.sun.codemodel.JClassAlreadyExistsException; |
31 | 35 | import com.sun.codemodel.JCodeModel; |
| 36 | +import com.sun.codemodel.JDefinedClass; |
| 37 | +import com.sun.codemodel.JExpr; |
| 38 | +import com.sun.codemodel.JExpression; |
| 39 | +import com.sun.codemodel.JInvocation; |
| 40 | +import com.sun.codemodel.JMethod; |
| 41 | +import com.sun.codemodel.JMod; |
| 42 | +import com.sun.codemodel.JTryBlock; |
| 43 | +import com.sun.codemodel.JVar; |
32 | 44 |
|
33 | 45 | public class UiThreadProcessor implements ElementProcessor { |
34 | 46 |
|
35 | | - @Override |
36 | | - public Class<? extends Annotation> getTarget() { |
37 | | - return UiThread.class; |
38 | | - } |
39 | | - |
40 | | - @Override |
41 | | - public void process(Element element, MetaModel metaModel) { |
42 | | - |
43 | | - String methodName = element.getSimpleName().toString(); |
44 | | - |
45 | | - Element enclosingElement = element.getEnclosingElement(); |
46 | | - MetaActivity metaActivity = metaModel.getMetaActivities().get(enclosingElement); |
47 | | - String className = metaActivity.getClassSimpleName(); |
48 | | - List<Instruction> memberInstructions = metaActivity.getMemberInstructions(); |
49 | | - |
50 | | - List<String> methodArguments = new ArrayList<String>(); |
51 | | - List<String> methodParameters = new ArrayList<String>(); |
52 | | - |
53 | | - ExecutableElement executableElement = (ExecutableElement) element; |
54 | | - |
55 | | - for(VariableElement parameter : executableElement.getParameters()) { |
56 | | - String parameterName = parameter.getSimpleName().toString(); |
57 | | - String parameterType = parameter.asType().toString(); |
58 | | - methodArguments.add(parameterType+" "+parameterName); |
59 | | - methodParameters.add(parameterName); |
60 | | - } |
61 | | - |
62 | | - Instruction instruction = new UiThreadInstruction(className, methodName, methodArguments, methodParameters); |
63 | | - memberInstructions.add(instruction); |
64 | | - } |
65 | | - |
66 | | - @Override |
67 | | - public void process(Element element, JCodeModel codeModel, ActivitiesHolder activitiesHolder) { |
68 | | - // TODO Auto-generated method stub |
69 | | - |
70 | | - } |
| 47 | + @Override |
| 48 | + public Class<? extends Annotation> getTarget() { |
| 49 | + return UiThread.class; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void process(Element element, MetaModel metaModel) { |
| 54 | + |
| 55 | + String methodName = element.getSimpleName().toString(); |
| 56 | + |
| 57 | + Element enclosingElement = element.getEnclosingElement(); |
| 58 | + MetaActivity metaActivity = metaModel.getMetaActivities().get(enclosingElement); |
| 59 | + String className = metaActivity.getClassSimpleName(); |
| 60 | + List<Instruction> memberInstructions = metaActivity.getMemberInstructions(); |
| 61 | + |
| 62 | + List<String> methodArguments = new ArrayList<String>(); |
| 63 | + List<String> methodParameters = new ArrayList<String>(); |
| 64 | + |
| 65 | + ExecutableElement executableElement = (ExecutableElement) element; |
| 66 | + |
| 67 | + for (VariableElement parameter : executableElement.getParameters()) { |
| 68 | + String parameterName = parameter.getSimpleName().toString(); |
| 69 | + String parameterType = parameter.asType().toString(); |
| 70 | + methodArguments.add(parameterType + " " + parameterName); |
| 71 | + methodParameters.add(parameterName); |
| 72 | + } |
| 73 | + |
| 74 | + Instruction instruction = new UiThreadInstruction(className, methodName, methodArguments, methodParameters); |
| 75 | + memberInstructions.add(instruction); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void process(Element element, JCodeModel codeModel, ActivitiesHolder activitiesHolder) throws JClassAlreadyExistsException { |
| 80 | + |
| 81 | + ActivityHolder holder = activitiesHolder.getActivityHolder(element); |
| 82 | + |
| 83 | + // Method |
| 84 | + String backgroundMethodName = element.getSimpleName().toString(); |
| 85 | + JMethod method = holder.activity.method(JMod.PUBLIC, codeModel.VOID, backgroundMethodName); |
| 86 | + method.annotate(Override.class); |
| 87 | + |
| 88 | + // Method parameters |
| 89 | + List<JVar> parameters = new ArrayList<JVar>(); |
| 90 | + ExecutableElement executableElement = (ExecutableElement) element; |
| 91 | + for (VariableElement parameter : executableElement.getParameters()) { |
| 92 | + String parameterName = parameter.getSimpleName().toString(); |
| 93 | + JClass parameterClass = holder.refClass(parameter.asType().toString()); |
| 94 | + JVar param = method.param(JMod.FINAL, parameterClass, parameterName); |
| 95 | + parameters.add(param); |
| 96 | + } |
| 97 | + |
| 98 | + JDefinedClass anonymousRunnableClass = codeModel.anonymousClass(Runnable.class); |
| 99 | + |
| 100 | + JMethod runMethod = anonymousRunnableClass.method(JMod.PUBLIC, codeModel.VOID, "run"); |
| 101 | + runMethod.annotate(Override.class); |
| 102 | + |
| 103 | + JBlock runMethodBody = runMethod.body(); |
| 104 | + JTryBlock runTry = runMethodBody._try(); |
| 105 | + |
| 106 | + JExpression activitySuper = holder.activity.staticRef("super"); |
| 107 | + |
| 108 | + JInvocation superCall = runTry.body().invoke(activitySuper, method); |
| 109 | + for (JVar param : parameters) { |
| 110 | + superCall.arg(param); |
| 111 | + } |
| 112 | + JCatchBlock runCatch = runTry._catch(holder.refClass(RuntimeException.class)); |
| 113 | + JVar exceptionParam = runCatch.param("e"); |
| 114 | + |
| 115 | + JClass logClass = holder.refClass("android.util.Log"); |
| 116 | + |
| 117 | + JInvocation errorInvoke = logClass.staticInvoke("e"); |
| 118 | + |
| 119 | + errorInvoke.arg(holder.activity.name()); |
| 120 | + errorInvoke.arg("A runtime exception was thrown while executing code in the ui thread"); |
| 121 | + errorInvoke.arg(exceptionParam); |
| 122 | + |
| 123 | + runCatch.body().add(errorInvoke); |
| 124 | + |
| 125 | + method.body().invoke("runOnUiThread").arg(JExpr._new(anonymousRunnableClass)); |
| 126 | + |
| 127 | + } |
71 | 128 |
|
72 | 129 | } |
0 commit comments