Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 14d5840

Browse files
committed
Merge pull request #602 from rockytriton/599_getRootUrl_Support
getRootUrl support
2 parents 00bdf60 + 7f8afdf commit 14d5840

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public class ValidatorHelper {
8080

8181
private static final String METHOD_NAME_SET_ROOT_URL = "setRootUrl";
8282

83+
private static final String METHOD_NAME_GET_ROOT_URL = "getRootUrl";
84+
8385
private static final List<String> VALID_PREF_RETURN_TYPES = Arrays.asList("int", "boolean", "float", "long", CanonicalNameConstants.STRING);
8486

8587
private static final List<String> INVALID_PREF_METHOD_NAMES = Arrays.asList("edit", "getSharedPreferences", "clear", "getEditor", "apply");
@@ -780,6 +782,8 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
780782
boolean foundGetRestTemplateMethod = false;
781783
boolean foundSetRestTemplateMethod = false;
782784
boolean foundSetRootUrlMethod = false;
785+
boolean foundGetRootUrlMethod = false;
786+
783787
for (Element enclosedElement : enclosedElements) {
784788
if (enclosedElement.getKind() != ElementKind.METHOD) {
785789
valid.invalidate();
@@ -795,8 +799,11 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
795799
}
796800

797801
if (!hasRestAnnotation) {
802+
798803
ExecutableElement executableElement = (ExecutableElement) enclosedElement;
799804
TypeMirror returnType = executableElement.getReturnType();
805+
String simpleName = executableElement.getSimpleName().toString();
806+
800807
if (returnType.toString().equals(CanonicalNameConstants.REST_TEMPLATE)) {
801808
if (executableElement.getParameters().size() > 0) {
802809
valid.invalidate();
@@ -809,6 +816,23 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
809816
foundGetRestTemplateMethod = true;
810817
}
811818
}
819+
} else if (simpleName.equals(METHOD_NAME_GET_ROOT_URL)) {
820+
if (!returnType.toString().equals(CanonicalNameConstants.STRING)) {
821+
valid.invalidate();
822+
annotationHelper.printError(enclosedElement, "The method getRootUrl must return String on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
823+
}
824+
825+
if (executableElement.getParameters().size() != 0) {
826+
valid.invalidate();
827+
annotationHelper.printError(enclosedElement, "The method getRootUrl cannot have parameters on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
828+
}
829+
830+
if (!foundGetRootUrlMethod) {
831+
foundGetRootUrlMethod = true;
832+
} else {
833+
valid.invalidate();
834+
annotationHelper.printError(enclosedElement, "The can be only one getRootUrl method on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
835+
}
812836
} else if (returnType.getKind() == TypeKind.VOID) {
813837
List<? extends VariableElement> parameters = executableElement.getParameters();
814838
if (parameters.size() == 1) {

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/RestProcessor.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
150150
}
151151
}
152152

153+
// Implement getRootUrl method
154+
implementGetRootUrl(holder, eBeansHolder, methods);
155+
153156
// Implement setRootUrl method
154157
for (ExecutableElement method : methods) {
155158
List<? extends VariableElement> parameters = method.getParameters();
@@ -168,4 +171,18 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
168171
}
169172

170173
}
174+
175+
private void implementGetRootUrl(RestImplementationHolder holder, EBeansHolder eBeansHolder, List<ExecutableElement> methods) {
176+
for (ExecutableElement method : methods) {
177+
String methodName = method.getSimpleName().toString();
178+
179+
if (method.getParameters().size() == 0 && method.getReturnType().toString().equals(STRING) && methodName.equals("getRootUrl")) {
180+
JMethod getRootUrlMethod = holder.restImplementationClass.method(JMod.PUBLIC, eBeansHolder.refClass(STRING), methodName);
181+
182+
getRootUrlMethod.annotate(Override.class);
183+
getRootUrlMethod.body()._return(holder.rootUrlField);
184+
return; // Only one implementation
185+
}
186+
}
187+
}
171188
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.rest;
17+
18+
import org.androidannotations.annotations.rest.Rest;
19+
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
20+
import org.springframework.web.client.RestTemplate;
21+
22+
@Rest(converters = { MappingJacksonHttpMessageConverter.class })
23+
public interface ClientWithWrongEnhancedMethod {
24+
// Correct
25+
RestTemplate getTemplate();
26+
27+
String getRootUrl();
28+
29+
// Wrong
30+
Object getRestTemplate();
31+
32+
String getURL();
33+
34+
String getRootURL();
35+
36+
String getRootURL(String param);
37+
38+
boolean setRootURL();
39+
}

‎AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/rest/RestTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ public void client_with_path_variables() throws IOException {
8181
assertCompilationSuccessful(result);
8282
}
8383

84+
@Test
85+
public void client_with_wrong_enhanced_methods() throws IOException {
86+
CompileResult result = compileFiles(ClientWithWrongEnhancedMethod.class);
87+
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "Object getRestTemplate();", result);
88+
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "String getURL();", result);
89+
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "String getRootURL();", result);
90+
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "String getRootURL(String param);", result);
91+
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "boolean setRootURL();", result);
92+
assertCompilationErrorCount(5, result);
93+
}
94+
8495
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,6 @@ ResponseEntity<Event[][]> getEventsArrayOfArrays2(String location, int year)
206206
void setRestTemplate(RestTemplate restTemplate);
207207

208208
void setRootUrl(String test);
209+
210+
String getRootUrl();
209211
}

0 commit comments

Comments
 (0)