Пример #1
0
  public void generateQuery(Feed feed) {
    try {
      JClass transformedType = getTransformedType(feed);
      if (transformedType == null) {
        return;
      }

      JDefinedClass cls =
          pkg._class(JMod.PUBLIC | JMod.FINAL, String.format("%sQuery", feed.getName()));
      cls._extends(parent.narrow(transformedType));

      if (feed.getTitle() != null) {
        cls.javadoc().add(String.format("<p>%s</p>", feed.getTitle()));
      }

      addResourcePath(cls, feed);
      addResultTypeMethod(model, cls, transformedType);
      addConstructor(cls);
      JDefinedClass bldrCls = addBuilderCls(feed, cls);

      cls.method(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, bldrCls, "builder")
          .body()
          ._return(JExpr._new(bldrCls));

      JMethod cpyMthd = cls.method(JMod.PROTECTED | JMod.FINAL, cls, "copy");
      JVar cpyParam = cpyMthd.param(immutableMap.narrow(String.class, Object.class), "params");
      cpyMthd.body()._return(JExpr._new(cls).arg(cpyParam));

    } catch (Exception e) {
      throw new IllegalStateException(e);
    }
  }
Пример #2
0
  private void addResourcePath(JDefinedClass cls, Feed feed) {
    JFieldVar field = cls.field(privateStaticFinal, String.class, "RESOURCE_PATH");
    field.init(JExpr.lit(feed.getHref()));

    JMethod method = cls.method(JMod.PROTECTED | JMod.FINAL, String.class, "resourcePath");
    method.annotate(Override.class);
    method.body()._return(field);
  }
Пример #3
0
  private JDefinedClass addBuilderCls(Feed feed, JDefinedClass cls)
      throws JClassAlreadyExistsException, ClassNotFoundException {
    JDefinedClass bldrCls = cls._class(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, "Builder");
    JVar paramBuilder =
        bldrCls
            .field(JMod.PRIVATE | JMod.FINAL, immutableMapBldr, "params")
            .init(immutableMap.staticInvoke("builder"));

    for (Filter filter : feed.getFilters().getFilter()) {
      if (!Boolean.TRUE.equals(filter.isDeprecated())) {
        addWithersFor(filter, bldrCls, paramBuilder);
      }
    }

    if (feed.getMixins() != null && feed.getMixins().getMixin() != null) {
      JDefinedClass mixinEnum = getMixinEnum(feed);
      for (Mixin mixin : feed.getMixins().getMixin()) {
        String mixinName = mixin.getName().toUpperCase().replace(' ', '_');
        JEnumConstant mixinCnst = mixinEnum.enumConstant(mixinName);
        mixinCnst.arg(JExpr.lit(mixin.getName().replace(' ', '+')));
      }
      JFieldVar field = cls.field(privateStaticFinal, String.class, "MIXIN");
      field.init(JExpr.lit("mixin"));
      JMethod iterWither = bldrCls.method(JMod.PUBLIC, bldrCls, "withMixins");
      JVar param = iterWither.param(iterable(mixinEnum), "mixins");
      JBlock mthdBody = iterWither.body();
      mthdBody.add(
          paramBuilder
              .invoke("put")
              .arg(field)
              .arg(immutableList.staticInvoke("copyOf").arg(param)));
      mthdBody._return(JExpr._this());
      JMethod varArgWither = bldrCls.method(JMod.PUBLIC, bldrCls, "withMixins");
      param = varArgWither.varParam(mixinEnum, "mixins");
      varArgWither
          .body()
          ._return(JExpr.invoke(iterWither).arg(immutableList.staticInvoke("copyOf").arg(param)));
    }

    JMethod bldMthd = bldrCls.method(JMod.PUBLIC, cls, "build");
    bldMthd.body()._return(JExpr._new(cls).arg(paramBuilder.invoke("build")));

    // TODO: add sorts
    return bldrCls;
  }
Пример #4
0
  private JDefinedClass getMixinEnum(Feed feed) throws JClassAlreadyExistsException {
    String enumName = camel(feed.getName(), true) + "Mixin";
    if (pkg.isDefined(enumName)) {
      return pkg._getClass(enumName);
    }
    JDefinedClass valueEnum = pkg._enum(enumName);

    JFieldVar valField = valueEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "value");
    JMethod ctor = valueEnum.constructor(JMod.PRIVATE);
    JVar param = ctor.param(String.class, "val");
    ctor.body().assign(valField, param);

    JMethod toString = valueEnum.method(JMod.PUBLIC, String.class, "toString");
    toString.annotate(Override.class);
    toString.body()._return(valField);
    return valueEnum;
  }
Пример #5
0
 private JClass getTransformedType(Feed feed) {
   Class<?> cls = null;
   String name = feed.getName();
   if (name.equals("Versions")) {
     cls = Version.class;
     return model.ref(cls);
   }
   if (name.equals("Groups")) {
     cls = GroupBody.class;
     return model.ref(cls);
   }
   cls = tryLoadClass(MODEL_PKG + name);
   if (cls == null) { // attempt in possible naive singular.
     cls = tryLoadClass(MODEL_PKG + name.substring(0, name.length() - 1));
   }
   if (cls == null) {
     cls = Object.class;
   }
   return model.ref(cls);
 }