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 @@ -50,11 +50,10 @@ public String retrieveUrlSuffix(Element element) {
}

@Override
protected JVar generateHttpEntityVar(MethodProcessorHolder methodHolder) {
protected JExpression generateHttpEntityVar(MethodProcessorHolder methodHolder) {
ExecutableElement executableElement = (ExecutableElement) methodHolder.getElement();
EBeanHolder holder = methodHolder.getHolder();
JClass httpEntity = holder.refClass(CanonicalNameConstants.HTTP_ENTITY);
JExpression httpEntityValue;

JBlock body = methodHolder.getBody();
JVar httpHeadersVar = generateHttpHeadersVar(holder, body, executableElement);
Expand All @@ -64,15 +63,12 @@ protected JVar generateHttpEntityVar(MethodProcessorHolder methodHolder) {
if (hasHeaders) {
JInvocation newHttpEntityVarCall = JExpr._new(httpEntity.narrow(Object.class));
newHttpEntityVarCall.arg(httpHeadersVar);
httpEntityValue = newHttpEntityVarCall;
} else {
httpEntityValue = httpEntity.staticRef("EMPTY");
}

JVar httpEntityVar;
String httpEntityVarName = "requestEntity";
httpEntityVar = body.decl(httpEntity.narrow(Object.class), httpEntityVarName, httpEntityValue);
String httpEntityVarName = "requestEntity";

return httpEntityVar;
return body.decl(httpEntity.narrow(Object.class), httpEntityVarName, newHttpEntityVarCall);
} else {
return JExpr._null();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.lang.model.element.VariableElement;

import org.androidannotations.annotations.rest.Accept;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.helper.RestAnnotationHelper;
import org.androidannotations.processing.DecoratingElementProcessor;
Expand All @@ -36,6 +37,7 @@
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
Expand All @@ -46,6 +48,7 @@ public abstract class MethodProcessor implements DecoratingElementProcessor {

protected final RestImplementationsHolder restImplementationsHolder;
protected final RestAnnotationHelper restAnnotationHelper;
protected final APTCodeModelHelper helper = new APTCodeModelHelper();

public MethodProcessor(ProcessingEnvironment processingEnv, RestImplementationsHolder restImplementationsHolder) {
this.restImplementationsHolder = restImplementationsHolder;
Expand Down Expand Up @@ -95,10 +98,11 @@ protected void generateRestTemplateCallBlock(MethodProcessorHolder methodHolder)

restCall.arg(httpMethod.staticRef(restMethodInCapitalLetters));

JVar hashMapVar = generateHashMapVar(methodHolder);

restCall = addHttpEntityVar(restCall, methodHolder);
restCall = addResponseEntityArg(restCall, methodHolder);

JVar hashMapVar = generateHashMapVar(methodHolder);
if (hashMapVar != null) {
restCall.arg(hashMapVar);
}
Expand Down Expand Up @@ -161,7 +165,7 @@ private JVar generateHashMapVar(MethodProcessorHolder methodHolder) {
return hashMapVar;
}

protected JVar generateHttpEntityVar(MethodProcessorHolder methodHolder) {
protected JExpression generateHttpEntityVar(MethodProcessorHolder methodHolder) {
ExecutableElement executableElement = (ExecutableElement) methodHolder.getElement();
EBeanHolder holder = methodHolder.getHolder();
JClass httpEntity = holder.refClass(CanonicalNameConstants.HTTP_ENTITY);
Expand All @@ -176,6 +180,10 @@ protected JVar generateHttpEntityVar(MethodProcessorHolder methodHolder) {
}

if (entitySentToServer != null) {
if (entityType.isPrimitive()) {
// Don't narrow primitive types...
entityType = entityType.boxify();
}
newHttpEntityVarCall = JExpr._new(httpEntity.narrow(entityType));
} else {
newHttpEntityVarCall = JExpr._new(httpEntity.narrow(Object.class));
Expand Down Expand Up @@ -211,11 +219,12 @@ protected JVar generateHttpHeadersVar(EBeanHolder holder, JBlock body, Executabl
JVar httpHeadersVar = null;

JClass httpHeadersClass = holder.refClass(CanonicalNameConstants.HTTP_HEADERS);
httpHeadersVar = body.decl(httpHeadersClass, "httpHeaders", JExpr._new(httpHeadersClass));

String mediaType = retrieveAcceptAnnotationValue(executableElement);
boolean hasMediaTypeDefined = mediaType != null;
if (hasMediaTypeDefined) {
httpHeadersVar = body.decl(httpHeadersClass, "httpHeaders", JExpr._new(httpHeadersClass));

JClass collectionsClass = holder.refClass(CanonicalNameConstants.COLLECTIONS);
JClass mediaTypeClass = holder.refClass(CanonicalNameConstants.MEDIA_TYPE);

Expand Down Expand Up @@ -245,13 +254,16 @@ private TreeMap<String, JVar> extractMethodParamsVar(EBeanHolder eBeanHolder, JM
String paramName = parameter.getSimpleName().toString();
String paramType = parameter.asType().toString();

// TODO check in validator that params are not generic. Or create a
// helper to fix that case and generate the right code.
JVar param = method.param(eBeanHolder.refClass(paramType), paramName);
JVar param = null;
if (parameter.asType().getKind().isPrimitive()) {
param = method.param(JType.parse(eBeanHolder.codeModel(), paramType), paramName);
} else {
JClass parameterClass = helper.typeMirrorToJClass(parameter.asType(), eBeanHolder);
param = method.param(parameterClass, paramName);
}
methodParams.put(paramName, param);
}

return methodParams;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (C) 2010-2012 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 java.util.Set;

import org.androidannotations.annotations.rest.Delete;
import org.androidannotations.annotations.rest.Get;
import org.androidannotations.annotations.rest.Head;
import org.androidannotations.annotations.rest.Options;
import org.androidannotations.annotations.rest.Post;
import org.androidannotations.annotations.rest.Put;
import org.androidannotations.annotations.rest.Rest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;

@Rest(converters = MappingJacksonHttpMessageConverter.class)
public interface ClientWithPathVariable {

@Delete("/test/{v1}/{v2}")
void deleteWithParameterEntity(int v1, String v2);

@Get("/test/{v1}/{v2}")
void getWithParameterEntity(int v1, String v2);

@Head("/test/{v1}/{v2}")
HttpHeaders headWithParameterEntity(int v1, String v2);

@Options("/test/{v1}/{v2}")
Set<HttpMethod> optionsWithParameterEntity(int v1, String v2);

@Post("/test/{v1}/{v2}")
void postWithParameterEntity(int v1, String v2);

@Put("/test/{v1}/{v2}")
void putWithParameterEntity(int v1, String v2);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2010-2012 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.Delete;
import org.androidannotations.annotations.rest.Get;
import org.androidannotations.annotations.rest.Head;
import org.androidannotations.annotations.rest.Options;
import org.androidannotations.annotations.rest.Post;
import org.androidannotations.annotations.rest.Put;
import org.androidannotations.annotations.rest.Rest;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;

@Rest(converters = MappingJacksonHttpMessageConverter.class)
public interface ClientWithPrimitiveReturnType {

@Delete("/test/")
int deleteWithPrimitiveReturnType();

@Get("/test/")
int getWithPrimitiveReturnType();

@Head("/test/")
int headWithPrimitiveReturnType();

@Options("/test/")
int optionsWithPrimitiveReturnType();

@Post("/test/")
int postWithPrimitiveReturnType();

@Put("/test/")
int putWithPrimitiveReturnType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (C) 2010-2012 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 java.util.Set;

import org.androidannotations.annotations.rest.Delete;
import org.androidannotations.annotations.rest.Get;
import org.androidannotations.annotations.rest.Head;
import org.androidannotations.annotations.rest.Options;
import org.androidannotations.annotations.rest.Post;
import org.androidannotations.annotations.rest.Put;
import org.androidannotations.annotations.rest.Rest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;

@Rest(converters = MappingJacksonHttpMessageConverter.class)
public interface ClientWithRequestEntity {

@Delete("/test/")
void deleteWithReturnType(Entity entity);

@Get("/test/")
void getWithReturnType(Entity entity);

@Head("/test/")
HttpHeaders headWithReturnType(Entity entity);

@Options("/test/")
Set<HttpMethod> optionsWithReturnType(Entity entity);

@Post("/test/")
void postWithReturnType(Entity entity);

@Put("/test/")
void putWithReturnType(Entity entity);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2010-2012 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.Delete;
import org.androidannotations.annotations.rest.Get;
import org.androidannotations.annotations.rest.Head;
import org.androidannotations.annotations.rest.Options;
import org.androidannotations.annotations.rest.Post;
import org.androidannotations.annotations.rest.Put;
import org.androidannotations.annotations.rest.Rest;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;

@Rest(converters = MappingJacksonHttpMessageConverter.class)
public interface ClientWithResponseEntity {

@Delete("/test/")
Entity deleteWithReturnType();

@Get("/test/")
Entity getWithReturnType();

@Head("/test/")
Entity headWithReturnType();

@Options("/test/")
Entity optionsWithReturnType();

@Post("/test/")
Entity postWithReturnType();

@Put("/test/")
Entity putWithReturnType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.androidannotations.rest;

public class Entity {

private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,42 @@ public void client_no_internet_permission_does_not_compile() throws IOException
assertCompilationErrorOn(ClientWithNoConverters.class, "@Rest", result);
}

@Test
public void client_with_return_type() throws IOException {
CompileResult result = compileFiles(ClientWithResponseEntity.class);
assertCompilationErrorOn(ClientWithResponseEntity.class, "@Put", result);
assertCompilationErrorOn(ClientWithResponseEntity.class, "@Delete", result);
assertCompilationErrorOn(ClientWithResponseEntity.class, "@Options", result);
assertCompilationErrorOn(ClientWithResponseEntity.class, "@Head", result);
assertCompilationErrorCount(4, result);
}

@Test
public void client_with_request_entity() throws IOException {
CompileResult result = compileFiles(ClientWithRequestEntity.class);
assertCompilationErrorOn(ClientWithRequestEntity.class, "@Delete", result);
assertCompilationErrorOn(ClientWithRequestEntity.class, "@Get", result);
assertCompilationErrorOn(ClientWithRequestEntity.class, "@Head", result);
assertCompilationErrorOn(ClientWithRequestEntity.class, "@Options", result);
assertCompilationErrorCount(4, result);
}

@Test
public void client_with_primitive_return_types() throws IOException {
CompileResult result = compileFiles(ClientWithPrimitiveReturnType.class);
assertCompilationErrorOn(ClientWithPrimitiveReturnType.class, "@Delete", result);
assertCompilationErrorOn(ClientWithPrimitiveReturnType.class, "@Get", result);
assertCompilationErrorOn(ClientWithPrimitiveReturnType.class, "@Head", result);
assertCompilationErrorOn(ClientWithPrimitiveReturnType.class, "@Options", result);
assertCompilationErrorOn(ClientWithPrimitiveReturnType.class, "@Post", result);
assertCompilationErrorOn(ClientWithPrimitiveReturnType.class, "@Put", result);
assertCompilationErrorCount(6, result);
}

@Test
public void client_with_path_variables() throws IOException {
CompileResult result = compileFiles(ClientWithPathVariable.class);
assertCompilationSuccessful(result);
}

}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.