@Override
  public void propertyField(
      JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
    field.annotate(JsonProperty.class).param("value", propertyName);
    if (field.type().erasure().equals(field.type().owner().ref(Set.class))) {
      field.annotate(JsonDeserialize.class).param("as", LinkedHashSet.class);
    }

    if (propertyNode.has("javaJsonView")) {
      field
          .annotate(JsonView.class)
          .param("value", field.type().owner().ref(propertyNode.get("javaJsonView").asText()));
    }

    if (propertyNode.has("description")) {
      field.annotate(JsonPropertyDescription.class).param("value", propertyNode.asText());
    }
  }
Exemplo n.º 2
0
 private Map<Ref, JFieldVar> genFields(Refs r, JDefinedClass clazz, Collection<Ref> refs) {
   Map<Ref, JFieldVar> fieldVarMap = new LinkedHashMap<Ref, JFieldVar>();
   for (Ref ref : refs) {
     String idPackage = (ref.isAndroidId ? "android" : r.packageName) + ".R";
     JFieldVar var;
     if (ref instanceof View) {
       var = clazz.field(PUBLIC, r.ref(((View) ref).type), ref.fieldName);
       var.javadoc().append("View for {@link " + idPackage + ".id#" + ref.id + "}.");
       if (ref.isNullable) {
         var.annotate(r.nullableAnnotation);
       }
     } else if (ref instanceof Include) {
       var = clazz.field(PUBLIC, r.ref(getClassName(((Include) ref).layout)), ref.fieldName);
       var.javadoc()
           .append("Holdr for {@link " + idPackage + ".layout#" + ((Include) ref).layout + "}.");
     } else {
       throw new IllegalArgumentException("Unknown ref: " + ref);
     }
     fieldVarMap.put(ref, var);
   }
   return fieldVarMap;
 }
Exemplo n.º 3
0
  private static void processField(
      FieldType field,
      JDefinedClass packetClass,
      JCodeModel codeModel,
      AtomicInteger numberOfUnknowns,
      boolean fromClient)
      throws JClassAlreadyExistsException {
    boolean isNested =
        (field.getType() == PacketSimpleTypes.OPTIONAL)
            || (field.getType() == PacketSimpleTypes.ARRAY_STATIC)
            || (field.getType() == PacketSimpleTypes.ARRAY_VAR_SMALL)
            || (field.getType() == PacketSimpleTypes.ARRAY_VAR_BIG);

    boolean isArray =
        (field.getType() == PacketSimpleTypes.BUFFER_STATIC)
            || (field.getType() == PacketSimpleTypes.BUFFER_VAR_SMALL)
            || (field.getType() == PacketSimpleTypes.BUFFER_VAR_BIG)
            || (isNested && (field.getType() != PacketSimpleTypes.OPTIONAL));

    boolean isStaticLength =
        (isArray)
            && ((field.getType() == PacketSimpleTypes.ARRAY_STATIC)
                || (field.getType() == PacketSimpleTypes.BUFFER_STATIC));

    // length is either not set for non arrays and static arrays,
    // its 1byte for small stuff and 2bytes for big stuff.
    int prefixLength =
        (!isArray || isStaticLength)
            ? -1
            : (field.getType() == PacketSimpleTypes.ARRAY_VAR_SMALL)
                    || (field.getType() == PacketSimpleTypes.BUFFER_VAR_SMALL)
                ? 1
                : 2;

    String name = "";

    if (field.getInfo() == null || field.getInfo().getName() == null) {
      // check if we got special fields...
      if (field.getType() == PacketSimpleTypes.AGENTID) {
        name = "AgentID";
      } else if (field.getType() == PacketSimpleTypes.OPTIONAL) {
        name = "Optional";
      } else {
        name = "Unknown";
      }

      name += numberOfUnknowns.incrementAndGet();
    } else {
      name = field.getInfo().getName();
    }

    String fieldName = WordUtils.uncapitalize(name);
    String fieldDescription =
        (field.getInfo() == null
                || field.getInfo().getDescription() == null
                || field.getInfo().getDescription().isEmpty())
            ? ""
            : "\n" + WordUtils.wrap(field.getInfo().getDescription(), /* maximumLength */ 50);

    JType fieldType;

    if (isNested) {
      JDefinedClass nestedClass =
          packetClass
              ._class(JMod.FINAL | JMod.STATIC | JMod.PUBLIC, "Nested" + name)
              ._implements(NestedMarker.class);

      AtomicInteger numberOfUnknownsNested = new AtomicInteger();

      for (FieldType nested : field.getField()) {
        processField(nested, nestedClass, codeModel, numberOfUnknownsNested, fromClient);
      }

      // generate getters, setters
      for (JFieldVar fieldVar : nestedClass.fields().values()) {
        processAccessors(fieldVar, nestedClass, fromClient);
      }

      processToString(nestedClass, codeModel);

      // nested classes are either arrays or optional...
      // meaning we will later have to test if they are null before reading/writing
      fieldType = isArray ? nestedClass.array() : nestedClass;
    } else {
      Class<?> fieldClass = convertFieldTypeToClass(field);
      fieldType = codeModel._ref(fieldClass);
    }

    LOGGER.debug("|+-Processing field: {}, of type: {}", fieldName, fieldType);

    // add the field
    JFieldVar packetField = packetClass.field(JMod.PRIVATE, fieldType, fieldName);

    if (fieldDescription != null && !fieldDescription.trim().equals("")) {
      JDocComment jDocComment = packetField.javadoc();
      jDocComment.add(fieldDescription);
    }

    // and dont forget array annotations if necessary
    if (isArray) {
      int size = (int) (field.getElements() == null ? -1 : field.getElements());

      packetField
          .annotate(IsArray.class)
          .param("constant", isStaticLength)
          .param("size", size)
          .param("prefixLength", prefixLength);
    }

    // or any special annotations
    if (field.getType() == PacketSimpleTypes.LONG) {
      packetField.annotate(IsInt64.class);
    }
    if (field.getType() == PacketSimpleTypes.VARINT) {
      packetField.annotate(IsVarInt.class);
    }
    if (field.getType() == PacketSimpleTypes.ASCII) {
      packetField.annotate(IsASCII.class);
    }
  }
 @Override
 public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName) {
   field.annotate(JsonIgnore.class);
 }