This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMethodProcessor.java
More file actions
269 lines (222 loc) · 10.1 KB
/
Copy pathMethodProcessor.java
File metadata and controls
269 lines (222 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* 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.processing.rest;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ExecutableElement;
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;
import org.androidannotations.processing.EBeanHolder;
import com.sun.codemodel.JBlock;
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;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
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;
restAnnotationHelper = new RestAnnotationHelper(processingEnv, getTarget());
}
protected void generateRestTemplateCallBlock(MethodProcessorHolder methodHolder) {
RestImplementationHolder holder = restImplementationsHolder.getEnclosingHolder(methodHolder.getElement());
ExecutableElement executableElement = (ExecutableElement) methodHolder.getElement();
EBeanHolder eBeanHolder = methodHolder.getHolder();
JClass expectedClass = methodHolder.getExpectedClass();
JClass methodReturnClass = methodHolder.getMethodReturnClass();
// Creating method signature
JMethod method;
String methodName = executableElement.getSimpleName().toString();
boolean methodReturnVoid = methodReturnClass == null && expectedClass == null;
if (methodReturnVoid) {
method = holder.restImplementationClass.method(JMod.PUBLIC, void.class, methodName);
} else {
method = holder.restImplementationClass.method(JMod.PUBLIC, methodHolder.getMethodReturnClass(), methodName);
}
method.annotate(Override.class);
// Keep a reference on method's body
JBlock body = method.body();
methodHolder.setBody(body);
// Keep a reference on method's parameters
TreeMap<String, JVar> methodParams = extractMethodParamsVar(eBeanHolder, method, executableElement, holder);
methodHolder.setMethodParams(methodParams);
// RestTemplate exchange() method call
JInvocation restCall = JExpr.invoke(holder.restTemplateField, "exchange");
// RestTemplate exchange() 1st arg : concat root url + suffix
JInvocation concatCall = JExpr.invoke(holder.rootUrlField, "concat");
// RestTemplate exchange() 2nd arg : add url param
restCall.arg(concatCall.arg(JExpr.lit(methodHolder.getUrlSuffix())));
// RestTemplate exchange() 3rd arg : add HttpMethod type param
JClass httpMethod = eBeanHolder.refClass(CanonicalNameConstants.HTTP_METHOD);
// add method type param
String restMethodInCapitalLetters = getTarget().getSimpleName().toUpperCase(Locale.ENGLISH);
restCall.arg(httpMethod.staticRef(restMethodInCapitalLetters));
JVar hashMapVar = generateHashMapVar(methodHolder);
restCall = addHttpEntityVar(restCall, methodHolder);
restCall = addResponseEntityArg(restCall, methodHolder);
if (hashMapVar != null) {
restCall.arg(hashMapVar);
}
insertRestCallInBody(body, restCall, methodHolder, methodReturnVoid);
}
/**
* Add the HttpEntity attribute to restTemplate.exchange() method. By
* default, the value will be <code>null</code> (for DELETE, HEAD and
* OPTIONS method type)
*/
protected JInvocation addHttpEntityVar(JInvocation restCall, MethodProcessorHolder methodHolder) {
return restCall.arg(JExpr._null());
}
/**
* Add the response type to restTemplate.exchange() method. This is used to
* bind the response into a specific Java object.
*/
protected JInvocation addResponseEntityArg(JInvocation restCall, MethodProcessorHolder methodHolder) {
return restCall.arg(JExpr._null());
}
/**
* Add an extra method calls on the result of restTemplate.exchange(). By
* default, just return the result
*/
protected JInvocation addResultCallMethod(JInvocation restCall, MethodProcessorHolder methodHolder) {
return restCall;
}
private void insertRestCallInBody(JBlock body, JInvocation restCall, MethodProcessorHolder methodHolder, boolean methodReturnVoid) {
if (methodReturnVoid) {
body.add(restCall);
} else {
restCall = addResultCallMethod(restCall, methodHolder);
body._return(restCall);
}
}
private JVar generateHashMapVar(MethodProcessorHolder methodHolder) {
ExecutableElement element = (ExecutableElement) methodHolder.getElement();
JCodeModel codeModel = methodHolder.getCodeModel();
JBlock body = methodHolder.getBody();
Map<String, JVar> methodParams = methodHolder.getMethodParams();
JVar hashMapVar = null;
Set<String> urlVariables = restAnnotationHelper.extractUrlVariableNames(element);
JClass hashMapClass = codeModel.ref(HashMap.class).narrow(String.class, Object.class);
if (!urlVariables.isEmpty()) {
hashMapVar = body.decl(hashMapClass, "urlVariables", JExpr._new(hashMapClass));
for (String urlVariable : urlVariables) {
JVar urlValue = methodParams.get(urlVariable);
body.invoke(hashMapVar, "put").arg(urlVariable).arg(urlValue);
methodParams.remove(urlVariable);
}
}
return hashMapVar;
}
protected JExpression generateHttpEntityVar(MethodProcessorHolder methodHolder) {
ExecutableElement executableElement = (ExecutableElement) methodHolder.getElement();
EBeanHolder holder = methodHolder.getHolder();
JClass httpEntity = holder.refClass(CanonicalNameConstants.HTTP_ENTITY);
JInvocation newHttpEntityVarCall;
TreeMap<String, JVar> methodParams = methodHolder.getMethodParams();
JVar entitySentToServer = null;
JType entityType = null;
if (!methodParams.isEmpty()) {
entitySentToServer = methodParams.firstEntry().getValue();
entityType = entitySentToServer.type();
}
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));
}
JBlock body = methodHolder.getBody();
JVar httpHeadersVar = generateHttpHeadersVar(holder, body, executableElement);
boolean hasHeaders = httpHeadersVar != null;
if (entitySentToServer != null) {
newHttpEntityVarCall.arg(entitySentToServer);
}
if (hasHeaders) {
newHttpEntityVarCall.arg(httpHeadersVar);
} else if (methodParams.isEmpty()) {
newHttpEntityVarCall.arg(JExpr._null());
}
JVar httpEntityVar;
String httpEntityVarName = "requestEntity";
if (entitySentToServer != null) {
httpEntityVar = body.decl(httpEntity.narrow(entityType), httpEntityVarName, newHttpEntityVarCall);
} else {
httpEntityVar = body.decl(httpEntity.narrow(Object.class), httpEntityVarName, newHttpEntityVarCall);
}
return httpEntityVar;
}
protected JVar generateHttpHeadersVar(EBeanHolder holder, JBlock body, ExecutableElement executableElement) {
JVar httpHeadersVar = null;
JClass httpHeadersClass = holder.refClass(CanonicalNameConstants.HTTP_HEADERS);
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);
JInvocation mediaTypeListParam = collectionsClass.staticInvoke("singletonList").arg(mediaTypeClass.staticInvoke("parseMediaType").arg(mediaType));
body.add(JExpr.invoke(httpHeadersVar, "setAccept").arg(mediaTypeListParam));
}
return httpHeadersVar;
}
private String retrieveAcceptAnnotationValue(ExecutableElement executableElement) {
Accept acceptAnnotation = executableElement.getAnnotation(Accept.class);
if (acceptAnnotation == null) {
acceptAnnotation = executableElement.getEnclosingElement().getAnnotation(Accept.class);
}
if (acceptAnnotation != null) {
return acceptAnnotation.value();
} else {
return null;
}
}
private TreeMap<String, JVar> extractMethodParamsVar(EBeanHolder eBeanHolder, JMethod method, ExecutableElement executableElement, RestImplementationHolder holder) {
List<? extends VariableElement> params = executableElement.getParameters();
TreeMap<String, JVar> methodParams = new TreeMap<String, JVar>();
for (VariableElement parameter : params) {
String paramName = parameter.getSimpleName().toString();
String paramType = parameter.asType().toString();
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;
}
}