use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.
the class RestMethodHandler method process.
@Override
public void process(Element element, RestHolder holder) {
ExecutableElement executableElement = (ExecutableElement) element;
String methodName = element.getSimpleName().toString();
AbstractJClass methodReturnClass = getMethodReturnClass(element, holder);
boolean methodReturnVoid = executableElement.getReturnType().getKind() == TypeKind.VOID;
// Creating method signature
JMethod method = holder.getGeneratedClass().method(JMod.PUBLIC, methodReturnClass, methodName);
method.annotate(Override.class);
SortedMap<String, JVar> params = addMethodParams(executableElement, holder, method);
JBlock methodBody = new JBlock().bracesRequired(false).indentRequired(false);
// RestTemplate exchange() method call
JInvocation exchangeCall = JExpr.invoke(holder.getRestTemplateField(), "exchange");
exchangeCall.arg(getUrl(element, holder));
exchangeCall.arg(getHttpMethod());
exchangeCall.arg(getRequestEntity(executableElement, holder, methodBody, params));
exchangeCall.arg(getResponseClass(element, holder));
IJExpression urlVariables = getUrlVariables(element, holder, methodBody, params);
if (urlVariables != null) {
exchangeCall.arg(urlVariables);
}
IJExpression response = setCookies(executableElement, holder, methodBody, exchangeCall);
if (methodReturnVoid && response.equals(exchangeCall)) {
methodBody.add(exchangeCall);
} else if (!methodReturnVoid) {
methodBody._return(addResultCallMethod(response, methodReturnClass));
}
methodBody = surroundWithRestTryCatch(holder, methodBody, methodReturnVoid);
codeModelHelper.copy(methodBody, method.body());
}
use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.
the class RestAnnotationHelper method declareHttpEntity.
public IJExpression declareHttpEntity(JBlock body, JVar entitySentToServer, JVar httpHeaders) {
AbstractJType entityType = getEnvironment().getJClass(Object.class);
if (entitySentToServer != null) {
entityType = entitySentToServer.type();
if (entityType.isPrimitive()) {
// Don't narrow primitive types...
entityType = entityType.boxify();
}
}
AbstractJClass httpEntity = getEnvironment().getJClass(HTTP_ENTITY);
AbstractJClass narrowedHttpEntity = httpEntity.narrow(entityType);
JInvocation newHttpEntityVarCall = JExpr._new(narrowedHttpEntity);
if (entitySentToServer != null) {
newHttpEntityVarCall.arg(entitySentToServer);
}
if (httpHeaders != null) {
newHttpEntityVarCall.arg(httpHeaders);
} else if (entitySentToServer == null) {
return JExpr._null();
}
return body.decl(narrowedHttpEntity, "requestEntity", newHttpEntityVarCall);
}
use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.
the class RestAnnotationHelper method retrieveDecoratedResponseClass.
/**
* Recursive method used to find if one of the grand-parent of the
* <code>enclosingJClass</code> is {@link java.util.Map Map}, {@link Set} or
* {@link java.util.Collection Collection}.
*/
private AbstractJClass retrieveDecoratedResponseClass(DeclaredType declaredType, TypeElement typeElement, RestHolder holder) {
String classTypeBaseName = typeElement.toString();
// Looking for basic java.util interfaces to set a default
// implementation
String decoratedClassName = null;
if (typeElement.getKind() == ElementKind.INTERFACE) {
if (classTypeBaseName.equals(CanonicalNameConstants.MAP)) {
decoratedClassName = LinkedHashMap.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.SET)) {
decoratedClassName = TreeSet.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.LIST)) {
decoratedClassName = ArrayList.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.COLLECTION)) {
decoratedClassName = ArrayList.class.getCanonicalName();
}
} else {
decoratedClassName = typeElement.getQualifiedName().toString();
}
if (decoratedClassName != null) {
// Configure the super class of the final decorated class
String decoratedClassNameSuffix = "";
AbstractJClass decoratedSuperClass = getEnvironment().getJClass(decoratedClassName);
for (TypeMirror typeArgument : declaredType.getTypeArguments()) {
TypeMirror actualTypeArgument = typeArgument;
if (typeArgument instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) typeArgument;
if (wildcardType.getExtendsBound() != null) {
actualTypeArgument = wildcardType.getExtendsBound();
} else if (wildcardType.getSuperBound() != null) {
actualTypeArgument = wildcardType.getSuperBound();
}
}
AbstractJClass narrowJClass = codeModelHelper.typeMirrorToJClass(actualTypeArgument);
decoratedSuperClass = decoratedSuperClass.narrow(narrowJClass);
decoratedClassNameSuffix += plainName(narrowJClass);
}
String decoratedFinalClassName = classTypeBaseName + "_" + decoratedClassNameSuffix;
decoratedFinalClassName = decoratedFinalClassName.replaceAll("\\[\\]", "s");
String packageName = holder.getGeneratedClass()._package().name();
decoratedFinalClassName = packageName + "." + decoratedFinalClassName;
JDefinedClass decoratedJClass = getEnvironment().getDefinedClass(decoratedFinalClassName);
decoratedJClass._extends(decoratedSuperClass);
return decoratedJClass;
}
// Try to find the superclass and make a recursive call to the this
// method
TypeMirror enclosingSuperJClass = typeElement.getSuperclass();
if (enclosingSuperJClass != null && enclosingSuperJClass.getKind() == TypeKind.DECLARED) {
DeclaredType declaredEnclosingSuperJClass = (DeclaredType) enclosingSuperJClass;
return retrieveDecoratedResponseClass(declaredType, (TypeElement) declaredEnclosingSuperJClass.asElement(), holder);
}
// Falling back to the current enclosingJClass if Class can't be found
return null;
}
use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.
the class RestAnnotationHelper method declareUrlVariables.
public JVar declareUrlVariables(ExecutableElement element, RestHolder holder, JBlock methodBody, SortedMap<String, JVar> methodParams) {
Map<String, String> urlNameToElementName = new HashMap<String, String>();
for (VariableElement variableElement : element.getParameters()) {
if (variableElement.getAnnotation(Path.class) != null) {
urlNameToElementName.put(getUrlVariableCorrespondingTo(variableElement), variableElement.getSimpleName().toString());
}
}
Set<String> urlVariables = extractUrlVariableNames(element);
// cookies in url?
String[] cookiesToUrl = requiredUrlCookies(element);
if (cookiesToUrl != null) {
for (String cookie : cookiesToUrl) {
urlVariables.add(cookie);
}
}
AbstractJClass hashMapClass = getEnvironment().getClasses().HASH_MAP.narrow(String.class, Object.class);
if (!urlVariables.isEmpty()) {
JVar hashMapVar = methodBody.decl(hashMapClass, "urlVariables", JExpr._new(hashMapClass));
for (String urlVariable : urlVariables) {
String elementName = urlNameToElementName.get(urlVariable);
if (elementName != null) {
JVar methodParam = methodParams.get(elementName);
methodBody.invoke(hashMapVar, "put").arg(urlVariable).arg(methodParam);
methodParams.remove(elementName);
} else {
// cookie from url
JInvocation cookieValue = holder.getAvailableCookiesField().invoke("get").arg(JExpr.lit(urlVariable));
methodBody.invoke(hashMapVar, "put").arg(urlVariable).arg(cookieValue);
}
}
return hashMapVar;
}
return null;
}
use of com.helger.jcodemodel.AbstractJClass in project androidannotations by androidannotations.
the class RestAnnotationHelper method declareHttpHeaders.
public JVar declareHttpHeaders(ExecutableElement executableElement, RestHolder holder, JBlock body) {
JVar httpHeadersVar = null;
String mediaType = acceptedHeaders(executableElement);
boolean hasMediaTypeDefined = mediaType != null;
String[] cookies = requiredCookies(executableElement);
boolean requiresCookies = cookies != null && cookies.length > 0;
String[] headers = requiredHeaders(executableElement);
boolean requiresHeaders = headers != null && headers.length > 0;
boolean requiresAuth = requiredAuthentication(executableElement);
boolean requiresMultipartHeader = multipartHeaderRequired(executableElement);
Map<String, String> headersFromAnnotations = getHeadersFromAnnotations(executableElement);
if (hasMediaTypeDefined || requiresCookies || requiresHeaders || requiresAuth || requiresMultipartHeader || !headersFromAnnotations.isEmpty()) {
// we need the headers
httpHeadersVar = body.decl(getEnvironment().getJClass(HTTP_HEADERS), "httpHeaders", JExpr._new(getEnvironment().getJClass(HTTP_HEADERS)));
}
if (hasMediaTypeDefined) {
AbstractJClass collectionsClass = getEnvironment().getJClass(CanonicalNameConstants.COLLECTIONS);
AbstractJClass mediaTypeClass = getEnvironment().getJClass(MEDIA_TYPE);
JInvocation mediaTypeListParam = collectionsClass.staticInvoke("singletonList").arg(mediaTypeClass.staticInvoke("parseMediaType").arg(mediaType));
body.add(JExpr.invoke(httpHeadersVar, "setAccept").arg(mediaTypeListParam));
}
if (headersFromAnnotations != null) {
for (Map.Entry<String, String> header : headersFromAnnotations.entrySet()) {
body.add(JExpr.invoke(httpHeadersVar, "set").arg(header.getKey()).arg(header.getValue()));
}
}
if (requiresCookies) {
AbstractJClass stringBuilderClass = getEnvironment().getClasses().STRING_BUILDER;
JVar cookiesValueVar = body.decl(stringBuilderClass, "cookiesValue", JExpr._new(stringBuilderClass));
for (String cookie : cookies) {
JInvocation cookieValue = JExpr.invoke(holder.getAvailableCookiesField(), "get").arg(cookie);
JInvocation cookieFormatted = getEnvironment().getClasses().STRING.staticInvoke("format").arg(String.format("%s=%%s;", cookie)).arg(cookieValue);
JInvocation appendCookie = JExpr.invoke(cookiesValueVar, "append").arg(cookieFormatted);
body.add(appendCookie);
}
JInvocation cookiesToString = cookiesValueVar.invoke("toString");
body.add(JExpr.invoke(httpHeadersVar, "set").arg("Cookie").arg(cookiesToString));
}
if (requiresMultipartHeader) {
body.add(JExpr.invoke(httpHeadersVar, "set").arg(JExpr.lit("Content-Type")).arg(getEnvironment().getJClass(MEDIA_TYPE).staticRef("MULTIPART_FORM_DATA_VALUE")));
}
if (requiresHeaders) {
for (String header : headers) {
JBlock block = null;
if (headersFromAnnotations.containsKey(header)) {
block = body._if(JExpr.invoke(holder.getAvailableHeadersField(), "containsKey").arg(header))._then();
} else {
block = body;
}
JInvocation headerValue = JExpr.invoke(holder.getAvailableHeadersField(), "get").arg(header);
block.add(JExpr.invoke(httpHeadersVar, "set").arg(header).arg(headerValue));
}
}
if (requiresAuth) {
// attach auth
body.add(httpHeadersVar.invoke("setAuthorization").arg(holder.getAuthenticationField()));
}
return httpHeadersVar;
}
Aggregations