Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class ValidatorHelper {

private static final String METHOD_NAME_SET_ROOT_URL = "setRootUrl";

private static final String METHOD_NAME_GET_ROOT_URL = "getRootUrl";

private static final List<String> VALID_PREF_RETURN_TYPES = Arrays.asList("int", "boolean", "float", "long", CanonicalNameConstants.STRING);

private static final List<String> INVALID_PREF_METHOD_NAMES = Arrays.asList("edit", "getSharedPreferences", "clear", "getEditor", "apply");
Expand Down Expand Up @@ -780,6 +782,8 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
boolean foundGetRestTemplateMethod = false;
boolean foundSetRestTemplateMethod = false;
boolean foundSetRootUrlMethod = false;
boolean foundGetRootUrlMethod = false;

for (Element enclosedElement : enclosedElements) {
if (enclosedElement.getKind() != ElementKind.METHOD) {
valid.invalidate();
Expand All @@ -795,8 +799,11 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
}

if (!hasRestAnnotation) {

ExecutableElement executableElement = (ExecutableElement) enclosedElement;
TypeMirror returnType = executableElement.getReturnType();
String simpleName = executableElement.getSimpleName().toString();

if (returnType.toString().equals(CanonicalNameConstants.REST_TEMPLATE)) {
if (executableElement.getParameters().size() > 0) {
valid.invalidate();
Expand All @@ -809,6 +816,23 @@ public void unannotatedMethodReturnsRestTemplate(TypeElement typeElement, IsVali
foundGetRestTemplateMethod = true;
}
}
} else if (simpleName.equals(METHOD_NAME_GET_ROOT_URL)) {
if (!returnType.toString().equals(CanonicalNameConstants.STRING)) {
valid.invalidate();
annotationHelper.printError(enclosedElement, "The method getRootUrl must return String on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
}

if (executableElement.getParameters().size() != 0) {
valid.invalidate();
annotationHelper.printError(enclosedElement, "The method getRootUrl cannot have parameters on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
}

if (!foundGetRootUrlMethod) {
foundGetRootUrlMethod = true;
} else {
valid.invalidate();
annotationHelper.printError(enclosedElement, "The can be only one getRootUrl method on a " + TargetAnnotationHelper.annotationName(Rest.class) + " annotated interface");
}
} else if (returnType.getKind() == TypeKind.VOID) {
List<? extends VariableElement> parameters = executableElement.getParameters();
if (parameters.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
}
}

// Implement getRootUrl method
implementGetRootUrl(holder, eBeansHolder, methods);

// Implement setRootUrl method
for (ExecutableElement method : methods) {
List<? extends VariableElement> parameters = method.getParameters();
Expand All @@ -168,4 +171,18 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
}

}

private void implementGetRootUrl(RestImplementationHolder holder, EBeansHolder eBeansHolder, List<ExecutableElement> methods) {
for (ExecutableElement method : methods) {
String methodName = method.getSimpleName().toString();

if (method.getParameters().size() == 0 && method.getReturnType().toString().equals(STRING) && methodName.equals("getRootUrl")) {
JMethod getRootUrlMethod = holder.restImplementationClass.method(JMod.PUBLIC, eBeansHolder.refClass(STRING), methodName);

getRootUrlMethod.annotate(Override.class);
getRootUrlMethod.body()._return(holder.rootUrlField);
return; // Only one implementation
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (C) 2010-2013 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.rest;

import org.androidannotations.annotations.rest.Rest;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Rest(converters = { MappingJacksonHttpMessageConverter.class })
public interface ClientWithWrongEnhancedMethod {
// Correct
RestTemplate getTemplate();

String getRootUrl();

// Wrong
Object getRestTemplate();

String getURL();

String getRootURL();

String getRootURL(String param);

boolean setRootURL();
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@ public void client_with_path_variables() throws IOException {
assertCompilationSuccessful(result);
}

@Test
public void client_with_wrong_enhanced_methods() throws IOException {
CompileResult result = compileFiles(ClientWithWrongEnhancedMethod.class);
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "Object getRestTemplate();", result);
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "String getURL();", result);
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "String getRootURL();", result);
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "String getRootURL(String param);", result);
assertCompilationErrorOn(ClientWithWrongEnhancedMethod.class, "boolean setRootURL();", result);
assertCompilationErrorCount(5, result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,6 @@ ResponseEntity<Event[][]> getEventsArrayOfArrays2(String location, int year)
void setRestTemplate(RestTemplate restTemplate);

void setRootUrl(String test);

String getRootUrl();
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.