private void addCreateFromParcel(JDefinedClass jclass, JDefinedClass creatorClass) {
   JMethod createFromParcel = creatorClass.method(JMod.PUBLIC, jclass, "createFromParcel");
   JVar in = createFromParcel.param(Parcel.class, "in");
   JVar instance = createFromParcel.body().decl(jclass, "instance", JExpr._new(jclass));
   suppressWarnings(createFromParcel, "unchecked");
   for (JFieldVar f : jclass.fields().values()) {
     if ((f.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
       continue;
     }
     if (f.type().erasure().name().equals("List")) {
       createFromParcel
           .body()
           .invoke(in, "readList")
           .arg(instance.ref(f))
           .arg(JExpr.direct(getGenericType(f.type()) + ".class.getClassLoader()"));
     } else {
       createFromParcel
           .body()
           .assign(
               instance.ref(f),
               JExpr.cast(
                   f.type(),
                   in.invoke("readValue")
                       .arg(JExpr.direct(f.type().erasure().name() + ".class.getClassLoader()"))));
     }
   }
   createFromParcel.body()._return(instance);
 }
Пример #2
0
  private static void processAccessors(
      JFieldVar fieldVar, JDefinedClass packetClass, boolean fromClient) {
    String name = WordUtils.capitalize(fieldVar.name());

    if (fromClient) {
      String methodName = "get" + name;
      JMethod getter = packetClass.method(JMod.PUBLIC, fieldVar.type(), methodName);
      getter.body()._return(fieldVar);
    } else {
      String methodName = "set" + name;
      JMethod setter = packetClass.method(JMod.PUBLIC, Void.TYPE, methodName);
      setter.param(fieldVar.type(), fieldVar.name());
      setter.body().assign(JExpr._this().ref(fieldVar.name()), JExpr.ref(fieldVar.name()));
    }
  }
 private void generateCollectionAccessor(final DefinedPropertyOutline fieldOutline) {
   final JFieldVar fieldVar = fieldOutline.getFieldVar();
   if (fieldVar != null) {
     final JMethod modifier =
         this.modifierClass.method(
             JMod.PUBLIC,
             fieldVar.type(),
             ModifierGenerator.GETTER_PREFIX + fieldOutline.getBaseName());
     if (this.implement) {
       final JFieldRef fieldRef =
           new NestedThisRef(this.classOutline.getImplClass()).ref(fieldVar);
       final JConditional ifNull = modifier.body()._if(fieldRef.eq(JExpr._null()));
       ifNull
           ._then()
           .assign(
               fieldRef,
               JExpr._new(
                   this.classOutline
                       .getImplClass()
                       .owner()
                       .ref(ArrayList.class)
                       .narrow(fieldOutline.getElementType())));
       modifier.body()._return(fieldRef);
     }
   }
 }
Пример #4
0
  private void addConstructors(JDefinedClass jclass, List<String> properties) {

    // no properties to put in the constructor => default constructor is good enough.
    if (properties.isEmpty()) {
      return;
    }

    // add a no-args constructor for serialization purposes
    JMethod noargsConstructor = jclass.constructor(JMod.PUBLIC);
    noargsConstructor.javadoc().add("No args constructor for use in serialization");

    // add the public constructor with property parameters
    JMethod fieldsConstructor = jclass.constructor(JMod.PUBLIC);
    JBlock constructorBody = fieldsConstructor.body();

    Map<String, JFieldVar> fields = jclass.fields();

    for (String property : properties) {
      JFieldVar field = fields.get(property);

      if (field == null) {
        throw new IllegalStateException(
            "Property "
                + property
                + " hasn't been added to JDefinedClass before calling addConstructors");
      }

      fieldsConstructor.javadoc().addParam(property);
      JVar param = fieldsConstructor.param(field.type(), field.name());
      constructorBody.assign(JExpr._this().ref(field), param);
    }
  }
  @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());
    }
  }
Пример #6
0
  private static void processToString(JDefinedClass packetClass, JCodeModel codeModel) {
    JClass string = codeModel.ref(String.class);
    JClass stringBuilder = codeModel.ref(StringBuilder.class);
    JClass arrays = codeModel.ref(Arrays.class);

    JMethod toStringMeth = packetClass.method(JMod.PUBLIC, String.class, "toString");
    toStringMeth.annotate(Override.class);
    JBlock body = toStringMeth.body();

    JVar stringBuilderVar = body.decl(stringBuilder, "sb");
    stringBuilderVar =
        stringBuilderVar.init(JExpr._new(stringBuilder).arg(packetClass.name() + "["));

    JInvocation appendChain = null;

    for (JFieldVar fieldVar : packetClass.fields().values()) {
      if (appendChain != null) {
        // a comma is needed
        appendChain = appendChain.invoke("append").arg("," + fieldVar.name() + "=");
      } else {
        appendChain = stringBuilderVar.invoke("append").arg(fieldVar.name() + "=");
      }

      // now add the field to the toString output
      JExpression expression =
          fieldVar.type().isArray()
              ? arrays.staticInvoke("toString").arg(JExpr._this().ref(fieldVar.name()))
              : fieldVar.type().isReference()
                  ? JExpr._this().ref(fieldVar.name()).invoke("toString")
                  : JExpr._this().ref(fieldVar.name());

      appendChain = appendChain.invoke("append").arg(expression);
    }

    if (appendChain != null) {
      appendChain = appendChain.invoke("append").arg("]");
    } else {
      appendChain = stringBuilderVar.invoke("append").arg("]");
    }

    body.add(appendChain);
    body._return(stringBuilderVar.invoke("toString"));
  }
Пример #7
0
  private void getter(JDefinedClass clazz, JFieldVar field, String fieldName, String remarks) {
    // getter
    JMethod getter = clazz.method(JMod.PUBLIC, field.type(), "get" + fieldName);

    /* Addign java doc for method */
    JDocComment jDoc = addMethodDoc(getter, remarks + "取得");
    JCommentPart rtn = jDoc.addReturn();
    rtn.add(remarks);

    JBlock block = getter.body();
    block._return(field);
  }
Пример #8
0
  private void setter(
      JCodeModel codeModel,
      JDefinedClass clazz,
      JFieldVar field,
      String fieldName,
      String remarks) {
    // setter
    JMethod setter = clazz.method(JMod.PUBLIC, codeModel.VOID, "set" + fieldName);
    JVar param = setter.param(field.type(), toFirstCharLower(fieldName)); // メソッド引数

    /* Addign java doc for method */
    addMethodDoc(setter, remarks + "設定", param);

    JBlock block2 = setter.body();
    block2.assign(JExpr._this().ref(field), param); // ローカル変数からインスタンス変数へセット
  }
  public void addWriteToParcel(JDefinedClass jclass) {
    JMethod method = jclass.method(JMod.PUBLIC, void.class, "writeToParcel");
    JVar dest = method.param(Parcel.class, "dest");
    method.param(int.class, "flags");

    for (JFieldVar f : jclass.fields().values()) {
      if ((f.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
        continue;
      }
      if (f.type().erasure().name().equals("List")) {
        method.body().invoke(dest, "writeList").arg(f);
      } else {
        method.body().invoke(dest, "writeValue").arg(f);
      }
    }
  }
 private void generateSingularPropertyAccessor(final DefinedPropertyOutline fieldOutline) {
   final JFieldVar fieldVar = fieldOutline.getFieldVar();
   if (fieldVar != null) {
     final JMethod modifier =
         this.modifierClass.method(
             JMod.PUBLIC,
             this.modifierClass.owner().VOID,
             ModifierGenerator.SETTER_PREFIX + fieldOutline.getBaseName());
     final JVar parameter =
         modifier.param(JMod.FINAL, fieldVar.type(), fieldOutline.getFieldName());
     if (this.implement) {
       modifier
           .body()
           .add(
               new NestedThisRef(this.classOutline.getImplClass())
                   .invoke(modifier.name())
                   .arg(parameter));
     }
   }
 }