コード例 #1
0
 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
  public void addCreator(JDefinedClass jclass) {
    JClass creatorType = jclass.owner().ref(Creator.class).narrow(jclass);
    JDefinedClass creatorClass = jclass.owner().anonymousClass(creatorType);

    addCreateFromParcel(jclass, creatorClass);
    addNewArray(jclass, creatorClass);

    JFieldVar creatorField =
        jclass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, creatorType, "CREATOR");
    creatorField.init(JExpr._new(creatorClass));
  }
コード例 #3
0
  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);
      }
    }
  }
コード例 #4
0
 private void addNewArray(JDefinedClass jclass, JDefinedClass creatorClass) {
   JMethod newArray = creatorClass.method(JMod.PUBLIC, jclass.array(), "newArray");
   newArray.param(int.class, "size");
   newArray.body()._return(JExpr.direct("new " + jclass.name() + "[size]"));
 }
コード例 #5
0
 public void addDescribeContents(JDefinedClass jclass) {
   JMethod method = jclass.method(JMod.PUBLIC, int.class, "describeContents");
   method.body()._return(JExpr.lit(0));
 }