Skip to content

Commit 96e10f9

Browse files
committed
Small refacto. Handle method defined directly in RestClient. Add method un functional-tests
1 parent eed9f0c commit 96e10f9

4 files changed

Lines changed: 33 additions & 20 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ public List<ExecutableElement> getMethods(TypeElement typeElement) {
295295
}
296296

297297
public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableElement> methods, String methodName, String returnType, String... parameterTypes) {
298+
return implementMethod(holder, methods, methodName, returnType, false, parameterTypes);
299+
}
300+
301+
public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableElement> methods, String methodName, String returnType, boolean finalParams, String... parameterTypes) {
298302
// First get the ExecutableElement method object from the util function.
299303
ExecutableElement method = getMethod(methods, methodName, returnType, parameterTypes);
300304
JMethod jmethod = null;
@@ -309,9 +313,10 @@ public JMethod implementMethod(GeneratedClassHolder holder, List<ExecutableEleme
309313
jmethod.annotate(Override.class);
310314

311315
// Create the parameters.
316+
int paramMods = finalParams ? JMod.FINAL : JMod.NONE;
312317
for (int i = 0; i < method.getParameters().size(); i++) {
313318
VariableElement param = method.getParameters().get(i);
314-
jmethod.param(holder.refClass(parameterTypes[i]), param.getSimpleName().toString());
319+
jmethod.param(paramMods, holder.refClass(parameterTypes[i]), param.getSimpleName().toString());
315320
}
316321
}
317322

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class ValidatorHelper {
8888

8989
private static final String METHOD_NAME_SET_ROOT_URL = "setRootUrl";
9090
private static final String METHOD_NAME_SET_AUTHENTICATION = "setAuthentication";
91+
private static final String METHOD_NAME_SET_BEARER_AUTH = "setBearerAuth";
9192
private static final String METHOD_NAME_GET_COOKIE = "getCookie";
9293
private static final String METHOD_NAME_GET_HEADER = "getHeader";
9394

@@ -752,6 +753,7 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
752753
boolean foundGetRestTemplateMethod = false;
753754
boolean foundSetRestTemplateMethod = false;
754755
boolean foundSetAuthenticationMethod = false;
756+
boolean foundSetBearerAuthMethod = false;
755757
boolean foundSetRootUrlMethod = false;
756758
boolean foundGetCookieMethod = false;
757759
boolean foundGetHeaderMethod = false;
@@ -822,6 +824,8 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
822824
foundSetRootUrlMethod = true;
823825
} else if (executableElement.getSimpleName().toString().equals(METHOD_NAME_SET_AUTHENTICATION) && !foundSetAuthenticationMethod) {
824826
foundSetAuthenticationMethod = true;
827+
} else if (executableElement.getSimpleName().toString().equals(METHOD_NAME_SET_BEARER_AUTH) && !foundSetBearerAuthMethod) {
828+
foundSetBearerAuthMethod = true;
825829
} else {
826830
valid.invalidate();
827831
annotationHelper.printError(enclosedElement, "The method to set a RestTemplate should have only one RestTemplate parameter on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/RestHolder.java‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
*/
1616
package org.androidannotations.holder;
1717

18+
import static com.sun.codemodel.JExpr._new;
1819
import static com.sun.codemodel.JExpr._this;
1920
import static com.sun.codemodel.JExpr.lit;
20-
import static com.sun.codemodel.JExpr.ref;
21-
import static com.sun.codemodel.JExpr._new;
2221
import static com.sun.codemodel.JMod.FINAL;
2322
import static com.sun.codemodel.JMod.PUBLIC;
2423
import static org.androidannotations.helper.CanonicalNameConstants.REST_TEMPLATE;
@@ -30,20 +29,23 @@
3029
import javax.lang.model.element.TypeElement;
3130
import javax.lang.model.type.TypeKind;
3231

32+
import org.androidannotations.api.rest.RestErrorHandler;
33+
import org.androidannotations.helper.APTCodeModelHelper;
34+
import org.androidannotations.helper.CanonicalNameConstants;
35+
import org.androidannotations.helper.ModelConstants;
36+
import org.androidannotations.process.ProcessHolder;
37+
3338
import com.sun.codemodel.ClassType;
3439
import com.sun.codemodel.JBlock;
3540
import com.sun.codemodel.JClass;
3641
import com.sun.codemodel.JDefinedClass;
3742
import com.sun.codemodel.JExpr;
43+
import com.sun.codemodel.JExpression;
3844
import com.sun.codemodel.JFieldVar;
3945
import com.sun.codemodel.JInvocation;
4046
import com.sun.codemodel.JMethod;
4147
import com.sun.codemodel.JMod;
42-
import org.androidannotations.api.rest.RestErrorHandler;
43-
import org.androidannotations.helper.APTCodeModelHelper;
44-
import org.androidannotations.helper.CanonicalNameConstants;
45-
import org.androidannotations.helper.ModelConstants;
46-
import org.androidannotations.process.ProcessHolder;
48+
import com.sun.codemodel.JVar;
4749

4850
public class RestHolder extends BaseGeneratedClassHolder {
4951

@@ -141,23 +143,22 @@ private void implementSetBasicAuth(List<ExecutableElement> methods) {
141143
}
142144

143145
private void implementSetBearerAuth(List<ExecutableElement> methods) {
144-
JMethod setBearerMethod = codeModelHelper.implementMethod(this, methods, "setBearerAuth", TypeKind.VOID.toString(), STRING);
146+
JMethod setBearerMethod = codeModelHelper.implementMethod(this, methods, "setBearerAuth", TypeKind.VOID.toString(), true, STRING);
145147

146148
if (setBearerMethod != null) {
147-
JClass authClass = classes().HTTP_AUTHENTICATION;
148-
JDefinedClass anonymousHttpAuthClass = codeModel().anonymousClass(authClass);
149-
150-
JMethod getHeaderValueMethod = anonymousHttpAuthClass.method(JMod.PUBLIC, String.class, "getHeaderValue");
151-
getHeaderValueMethod.annotate(Override.class);
152-
JBlock getHeaderValueMethodBody = getHeaderValueMethod.body();
149+
JVar tokenParamVar = setBearerMethod.params().get(0);
150+
JExpression tokenExpr = lit("Bearer ").plus(tokenParamVar);
153151

154-
JBlock setBearerBody = setBearerMethod.body();
155-
156-
setBearerBody.decl(FINAL, refClass(String.class), "bearerHeader", lit("Bearer ").plus(setBearerMethod.params().get(0)));
152+
JClass authClass = classes().HTTP_AUTHENTICATION;
153+
JDefinedClass anonymousHttpAuthClass = codeModel().anonymousClass(authClass);
157154

158-
getHeaderValueMethodBody._return(ref("bearerHeader"));
155+
JMethod getHeaderValueMethod = anonymousHttpAuthClass.method(JMod.PUBLIC, String.class, "getHeaderValue");
156+
getHeaderValueMethod.annotate(Override.class);
157+
JBlock getHeaderValueMethodBody = getHeaderValueMethod.body();
158+
getHeaderValueMethodBody._return(tokenExpr);
159159

160-
setBearerBody.assign(_this().ref(getAuthenticationField()), _new(anonymousHttpAuthClass));
160+
JBlock setBearerBody = setBearerMethod.body();
161+
setBearerBody.assign(_this().ref(getAuthenticationField()), _new(anonymousHttpAuthClass));
161162
}
162163
}
163164

‎AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/rest/MyService.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.androidannotations.annotations.rest.Rest;
4242
import org.androidannotations.annotations.rest.SetsCookie;
4343
import org.androidannotations.api.rest.MediaType;
44+
import org.androidannotations.api.rest.RestClientHeaders;
4445

4546
// if defined, the rootUrl will be added as a prefix to every request
4647
@Rest(rootUrl = "http://company.com/ajax/services", converters = { MappingJacksonHttpMessageConverter.class }, interceptors = { RequestInterceptor.class })
@@ -274,4 +275,6 @@ public interface MyService {
274275
void setAuthentication(HttpAuthentication auth);
275276

276277
void setHttpBasicAuth(String username, String password);
278+
279+
void setBearerAuth(String token);
277280
}

0 commit comments

Comments
 (0)