Example #1
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);
    }
  }
 private JDocComment addBaseJavaDoc(final Action action, final JMethod method) {
   final JDocComment javadoc = method.javadoc();
   if (isNotBlank(action.getDescription())) {
     javadoc.add(action.getDescription());
   }
   return javadoc;
 }
Example #3
0
  private void genConstructor(
      Refs r,
      JDefinedClass clazz,
      Collection<Ref> refs,
      Map<Ref, JFieldVar> fieldVarMap,
      JFieldVar holderListener,
      Map<Listener.Type, ListenerType> listenerTypeMap) {
    // private MyLayoutViewModel(View view) {
    JMethod constructor = clazz.constructor(PUBLIC);
    JVar viewVar = constructor.param(r.viewClass, "view");
    JBlock body = constructor.body();

    // super(view);
    body.invoke("super").arg(viewVar);

    // myLinearLayout = (LinearLayout) view.findViewById(R.id.my_linear_layout);
    // myTextView = (TextView) myLinearLayout.findViewById(R.id.my_text_view);
    genInitFields(r, fieldVarMap, viewVar, refs, body);

    // myButton.setOnClickListener((view) -> { if (_holderListener != null)
    // _holderListener.onMyButtonClick(myButton); });
    genListeners(r, fieldVarMap, holderListener, refs, body, listenerTypeMap);

    JDocComment doc = constructor.javadoc();
    doc.append(
        "Constructs a new {@link me.tatarka.holdr.Holdr} for {@link "
            + r.packageName
            + ".R.layout#"
            + r.layoutName
            + "}.");
    doc.addParam(viewVar).append("The root view to search for the holdr's views.");
  }
 private static void addSetterDeclaration(
     Names names, JDefinedClass paramType, JDefinedClass setterReturnType) {
   JMethod method = setterReturnType.method(JMod.NONE, setterReturnType, names.getSetterName());
   method.param(paramType, names.getParamName());
   JDocComment javadoc = method.javadoc();
   javadoc.append(names.getJavadoc());
 }
  public static void addGetterSetterDeclaration(
      Names names, JClass type, JDefinedClass jDefinedClass) {
    addGetterDeclaration(names, type, jDefinedClass);

    JMethod method = jDefinedClass.method(JMod.NONE, jDefinedClass, names.getSetterName());
    method.param(type, names.getParamName());

    JDocComment javadoc = method.javadoc();
    javadoc.append(names.getJavadoc());
  }
Example #6
0
  /**
   * メソッドレベルのJavaDoc追加
   *
   * @param method メソッド
   * @param doc コメント
   * @param params パラメータ
   */
  private JDocComment addMethodDoc(JMethod method, String doc, JVar... params) {
    /* Addign java doc for method */
    JDocComment jDoc = method.javadoc();
    jDoc.add(doc);

    for (JVar param : params) {
      JCommentPart paramDoc = jDoc.addParam(param);
    }

    return jDoc;
  }
Example #7
0
 private void addVarArgsWither(
     Filter filter, JMethod wither, JDefinedClass bldrCls, JClass paramType)
     throws JClassAlreadyExistsException {
   JMethod method = bldrCls.method(wither.mods().getValue(), wither.type(), wither.name());
   if (filter.getTitle() != null) {
     method.javadoc().add(String.format("<p>%s</p>", filter.getTitle()));
   }
   JVar param = method.varParam(paramType, wither.listParams()[0].name());
   method
       .body()
       ._return(JExpr.invoke(wither).arg(immutableList.staticInvoke("copyOf").arg(param)));
 }
  protected void setOnFinishInflate() {
    onFinishInflate = generatedClass.method(PUBLIC, codeModel().VOID, "onFinishInflate");
    onFinishInflate.annotate(Override.class);
    onFinishInflate.javadoc().append(ALREADY_INFLATED_COMMENT);

    JBlock ifNotInflated = onFinishInflate.body()._if(getAlreadyInflated().not())._then();
    ifNotInflated.assign(getAlreadyInflated(), JExpr.TRUE);

    getInit();
    viewNotifierHelper.invokeViewChanged(ifNotInflated);

    onFinishInflate.body().invoke(JExpr._super(), "onFinishInflate");
  }
Example #9
0
 private JMethod addIterableWither(
     Filter filter, JDefinedClass bldrCls, JVar paramBuilder, JFieldVar field, JClass paramType)
     throws JClassAlreadyExistsException {
   JMethod method = createWitherMethod(filter, bldrCls);
   if (filter.getTitle() != null) {
     method.javadoc().add(String.format("<p>%s</p>", filter.getTitle()));
   }
   JVar param = addParam(filter, method, iterable(paramType));
   JBlock mthdBody = method.body();
   mthdBody.add(
       paramBuilder.invoke("put").arg(field).arg(immutableList.staticInvoke("copyOf").arg(param)));
   mthdBody._return(JExpr._this());
   return method;
 }
Example #10
0
 private void addWither(
     Filter filter, JDefinedClass bldrCls, JVar paramBuilder, JFieldVar field, JClass paramType)
     throws JClassAlreadyExistsException {
   JMethod method = createWitherMethod(filter, bldrCls);
   if (filter.getTitle() != null) {
     method.javadoc().add(String.format("<p>%s</p>", filter.getTitle()));
   }
   JVar param = addParam(filter, method, paramType);
   JBlock mthdBody = method.body();
   boolean needsNullCheck = true;
   if (filter.getMinValue() != null) {
     mthdBody.add(precs.staticInvoke("checkNotNull").arg(param));
     needsNullCheck = false;
     int min = filter.getMinValue().intValue();
     mthdBody.add(
         precs
             .staticInvoke("checkArgument")
             .arg(JExpr.lit(min).lte(param))
             .arg(JExpr.lit(param.name() + ": %s < " + min))
             .arg(param));
   }
   if (filter.getMaxValue() != null) {
     if (needsNullCheck) {
       mthdBody.add(precs.staticInvoke("checkNotNull").arg(param));
       needsNullCheck = false;
     }
     int max = filter.getMaxValue().intValue();
     mthdBody.add(
         precs
             .staticInvoke("checkArgument")
             .arg(JExpr.lit(max).gte(param))
             .arg(JExpr.lit(param.name() + ": %s > " + max))
             .arg(param));
   }
   JInvocation putIntoMap = paramBuilder.invoke("put").arg(field);
   if (needsNullCheck) {
     putIntoMap.arg(precs.staticInvoke("checkNotNull").arg(param));
   } else {
     putIntoMap.arg(param);
   }
   mthdBody.add(putIntoMap);
   mthdBody._return(JExpr._this());
 }
  private void createResponseBuilderInResourceMethodReturnType(
      final JDefinedClass responseClass,
      final int statusCode,
      final Response response,
      final MimeType responseMimeType)
      throws Exception {
    final String responseBuilderMethodName =
        Names.buildResponseMethodName(statusCode, responseMimeType);

    final JMethod responseBuilderMethod =
        responseClass.method(PUBLIC + STATIC, responseClass, responseBuilderMethodName);

    final JDocComment javadoc = responseBuilderMethod.javadoc();

    if (isNotBlank(response.getDescription())) {
      javadoc.add(response.getDescription());
    }

    if ((responseMimeType != null) && (isNotBlank(responseMimeType.getExample()))) {
      javadoc.add(EXAMPLE_PREFIX + responseMimeType.getExample());
    }

    JInvocation builderArgument =
        types
            .getGeneratorClass(javax.ws.rs.core.Response.class)
            .staticInvoke("status")
            .arg(JExpr.lit(statusCode));

    if (responseMimeType != null) {
      builderArgument =
          builderArgument
              .invoke("header")
              .arg(HttpHeaders.CONTENT_TYPE)
              .arg(responseMimeType.getType());
    }

    final StringBuilder freeFormHeadersDescription = new StringBuilder();

    for (final Entry<String, Header> namedHeaderParameter : response.getHeaders().entrySet()) {
      final String headerName = namedHeaderParameter.getKey();
      final Header header = namedHeaderParameter.getValue();

      if (headerName.contains(RESPONSE_HEADER_WILDCARD_SYMBOL)) {
        appendParameterJavadocDescription(header, freeFormHeadersDescription);
        continue;
      }

      final String argumentName = Names.buildVariableName(headerName);

      builderArgument =
          builderArgument.invoke("header").arg(headerName).arg(JExpr.ref(argumentName));

      addParameterJavaDoc(header, argumentName, javadoc);

      responseBuilderMethod.param(types.buildParameterType(header, argumentName), argumentName);
    }

    final JBlock responseBuilderMethodBody = responseBuilderMethod.body();

    final JVar builderVariable =
        responseBuilderMethodBody.decl(
            types.getGeneratorType(ResponseBuilder.class), "responseBuilder", builderArgument);

    if (freeFormHeadersDescription.length() > 0) {
      // generate a Map<String, List<Object>> argument for {?} headers
      final JClass listOfObjectsClass = types.getGeneratorClass(List.class).narrow(Object.class);
      final JClass headersArgument =
          types
              .getGeneratorClass(Map.class)
              .narrow(types.getGeneratorClass(String.class), listOfObjectsClass);

      builderArgument =
          responseBuilderMethodBody
              .invoke("headers")
              .arg(JExpr.ref(MULTIPLE_RESPONSE_HEADERS_ARGUMENT_NAME))
              .arg(builderVariable);

      final JVar param =
          responseBuilderMethod.param(headersArgument, MULTIPLE_RESPONSE_HEADERS_ARGUMENT_NAME);

      javadoc.addParam(param).add(freeFormHeadersDescription.toString());
    }

    if (responseMimeType != null) {
      responseBuilderMethodBody
          .invoke(builderVariable, "entity")
          .arg(JExpr.ref(GENERIC_PAYLOAD_ARGUMENT_NAME));
      responseBuilderMethod.param(
          types.getResponseEntityClass(responseMimeType), GENERIC_PAYLOAD_ARGUMENT_NAME);
      javadoc
          .addParam(GENERIC_PAYLOAD_ARGUMENT_NAME)
          .add(defaultString(responseMimeType.getExample()));
    }

    responseBuilderMethodBody._return(
        JExpr._new(responseClass).arg(builderVariable.invoke("build")));
  }
 public static void addGetterDeclaration(Names names, Class<?> type, JDefinedClass jDefinedClass) {
   JMethod method = jDefinedClass.method(JMod.NONE, type, names.getGetterName());
   JDocComment javadoc = method.javadoc();
   javadoc.append(names.getJavadoc());
 }