Пример #1
0
 private void addStatusField(RestActionClass actionClass, MethodSpec.Builder builder) {
   for (Element element : actionClass.getAnnotatedElements(Status.class)) {
     String fieldAddress = getFieldAddress(actionClass, element);
     if (TypeUtils.containsType(element, Boolean.class, boolean.class)) {
       builder.addStatement(fieldAddress + " = response.isSuccessful()", element);
     } else if (TypeUtils.containsType(element, Integer.class, int.class, long.class)) {
       builder.addStatement(
           fieldAddress + " = ($T) response.getStatus()", element, element.asType());
     } else if (equalTypes(element, String.class)) {
       builder.addStatement(fieldAddress + " = Integer.toString(response.getStatus())", element);
     } else if (TypeUtils.containsType(element, Long.class)) {
       builder.addStatement(fieldAddress + " = (long) response.getStatus()", element);
     }
   }
 }
Пример #2
0
 private MethodSpec createFillErrorMethod(RestActionClass actionClass) {
   MethodSpec.Builder builder =
       MethodSpec.methodBuilder("fillError")
           .addModifiers(Modifier.PUBLIC)
           .addAnnotation(Override.class)
           .returns(actionClass.getTypeName())
           .addParameter(actionClass.getTypeName(), "action")
           .addParameter(Throwable.class, "error");
   for (Element element : actionClass.getAnnotatedElements(Error.class)) {
     String fieldAddress = getFieldAddress(actionClass, element);
     if (TypeUtils.containsType(element, Throwable.class)) {
       builder.addStatement(fieldAddress + " = error", element);
     } else if (TypeUtils.containsType(element, Exception.class)) {
       builder.addStatement(fieldAddress + " = ($T) error", element, Exception.class);
     }
   }
   builder.addStatement("return action");
   return builder.build();
 }
Пример #3
0
 private void addParts(RestActionClass actionClass, MethodSpec.Builder builder) {
   for (Element element : actionClass.getAnnotatedElements(Part.class)) {
     Part part = element.getAnnotation(Part.class);
     String partName = part.value();
     String name = element.getSimpleName().toString();
     if (StringUtils.isEmpty(partName)) {
       partName = name;
     }
     String encode = part.encoding();
     String httpBodyName = "httpBody";
     builder.beginControlFlow("if (action.$L != null)", name);
     if (TypeUtils.equalTypes(element, File.class)) {
       builder.addStatement(
           "$T $L = new $T($S, action.$L)",
           HttpBody.class,
           httpBodyName,
           FileBody.class,
           encode,
           name);
     } else if (TypeUtils.equalTypes(element, byte[].class)) {
       builder.addStatement(
           "$T $L = new $T($S, action.$L)",
           HttpBody.class,
           httpBodyName,
           ByteArrayBody.class,
           encode,
           name);
     } else if (TypeUtils.equalTypes(element, String.class)) {
       builder.addStatement(
           "$T $L = new $T($S, action.$L.getBytes())",
           HttpBody.class,
           httpBodyName,
           ByteArrayBody.class,
           encode,
           name);
     } else {
       builder.addStatement("$T $L = action.$L", HttpBody.class, httpBodyName, name);
     }
     builder.addStatement("requestBuilder.addPart($S, $L, $S)", partName, httpBodyName, encode);
     builder.endControlFlow();
   }
 }
Пример #4
0
 private void addRequestQueries(RestActionClass actionClass, MethodSpec.Builder builder) {
   for (Element element : actionClass.getAnnotatedElements(Query.class)) {
     Query annotation = element.getAnnotation(Query.class);
     if (TypeUtils.isPrimitive(element)) {
       builder.addStatement(
           "requestBuilder.addQueryParam($S, action.$L + \"\", $L, $L)",
           annotation.value(),
           element,
           annotation.encodeName(),
           annotation.encodeValue());
     } else {
       builder.beginControlFlow("if (action.$L != null)", element);
       builder.addStatement(
           "requestBuilder.addQueryParam($S, action.$L.toString(), $L, $L)",
           annotation.value(),
           element,
           annotation.encodeName(),
           annotation.encodeValue());
       builder.endControlFlow();
     }
   }
 }