@@ -56,27 +56,34 @@ public String getTarget() {
5656 public void process (Element element , JCodeModel codeModel , EBeanHolder holder ) throws JClassAlreadyExistsException {
5757
5858 ExecutableElement executableElement = (ExecutableElement ) element ;
59+ UiThread annotation = element .getAnnotation (UiThread .class );
60+ boolean useDirect = annotation .useDirect ();
5961
6062 JMethod delegatingMethod = helper .overrideAnnotatedMethod (executableElement , holder );
63+ JBlock clonedBody = null ;
6164
62- //Need to clone the body to be put in the check. This is done here because the next line
63- //will remove the body from the method.
64- JBlock clonedBody = cloneMethodBody (delegatingMethod );
65+ if (useDirect ) {
66+ // Need to clone the body to be put in the check. This is done here
67+ // because the next line
68+ // will remove the body from the method.
69+ clonedBody = cloneMethodBody (delegatingMethod );
70+ }
6571
6672 JDefinedClass anonymousRunnableClass = helper .createDelegatingAnonymousRunnableClass (holder , delegatingMethod );
6773
6874 {
69- //Execute Runnable
70- UiThread annotation = element .getAnnotation (UiThread .class );
75+ // Execute Runnable
7176 long delay = annotation .delay ();
7277
7378 if (holder .handler == null ) {
7479 JClass handlerClass = holder .classes ().HANDLER ;
7580 holder .handler = holder .generatedClass .field (JMod .PRIVATE , handlerClass , "handler_" , JExpr ._new (handlerClass ));
7681 }
7782
78- //Put in the check for the UI thread.
79- addUIThreadCheck (delegatingMethod , clonedBody , codeModel );
83+ if (useDirect ) {
84+ // Put in the check for the UI thread.
85+ addUIThreadCheck (delegatingMethod , clonedBody , codeModel );
86+ }
8087
8188 if (delay == 0 ) {
8289 delegatingMethod .body ().invoke (holder .handler , "post" ).arg (_new (anonymousRunnableClass ));
@@ -89,8 +96,9 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
8996
9097 /**
9198 * Clone a method body.
92- *
93- * @param method the method to clone.
99+ *
100+ * @param method
101+ * the method to clone.
94102 * @return A new JBlock containing the method body.
95103 */
96104 private JBlock cloneMethodBody (JMethod method ) {
@@ -105,26 +113,28 @@ private JBlock cloneMethodBody(JMethod method) {
105113
106114 /**
107115 * Add the pre-check to see if we are already in the UI thread.
108- *
116+ *
109117 * @param delegatingMethod
110118 * @param codeModel
111119 * @throws JClassAlreadyExistsException
112120 */
113121 private void addUIThreadCheck (JMethod delegatingMethod , JBlock clonedBody , JCodeModel codeModel ) throws JClassAlreadyExistsException {
114- //Get the Thread and Looper class.
122+ // Get the Thread and Looper class.
115123 JClass tClass = codeModel .ref (Thread .class );
116124 JClass lClass = codeModel .ref (Looper .class );
117125
118- //invoke the methods.
126+ // invoke the methods.
119127 JExpression lhs = tClass .staticInvoke (METHOD_CUR_THREAD );
120128 JExpression rhs = lClass .staticInvoke (METHOD_MAIN_LOOPER ).invoke (METHOD_GET_THREAD );
121129
122- //create the conditional and the block.
130+ // create the conditional and the block.
123131 JConditional con = delegatingMethod .body ()._if (JOp .eq (lhs , rhs ));
124132 JBlock block = con ._then ();
125133
126- //Put the cloned method body in the if() block, this is necessary because the
127- //method may have parameters and a simple super.methodCall() won't work.
134+ // Put the cloned method body in the if() block, this is necessary
135+ // because the
136+ // method may have parameters and a simple super.methodCall() won't
137+ // work.
128138 for (Object statement : clonedBody .getContents ()) {
129139 block .add ((JStatement ) statement );
130140 }
0 commit comments