/**
   * Add the pre-check to see if we are already in the UI thread.
   *
   * @param delegatingMethod
   * @param holder
   * @throws JClassAlreadyExistsException
   */
  private void addUIThreadCheck(
      JMethod delegatingMethod, JBlock previousBody, EComponentHolder holder)
      throws JClassAlreadyExistsException {
    // Get the Thread and Looper class.
    JClass tClass = holder.classes().THREAD;
    JClass lClass = holder.classes().LOOPER;

    // invoke the methods.
    JExpression lhs = tClass.staticInvoke(METHOD_CUR_THREAD);
    JExpression rhs = lClass.staticInvoke(METHOD_MAIN_LOOPER).invoke(METHOD_GET_THREAD);

    // create the conditional and the block.
    JConditional con = delegatingMethod.body()._if(JOp.eq(lhs, rhs));
    JBlock thenBlock = con._then().add(previousBody);
    thenBlock._return();
  }
Ejemplo n.º 2
0
  @Override
  public void generate(JDefinedClass cls, GenerationContext context) {
    if (classSpec.getFields().isEmpty()) {
      return; // No equals needed.
    }

    // Import the objects class, for hash coding.
    JClass objects = context.getTypeManager().getClassDirect("java.util.Objects");

    // Create the equals method.
    JMethod hashMethod = cls.method(JMod.PUBLIC, int.class, "hashCode");
    hashMethod.annotate(Override.class);
    JBlock body = hashMethod.body();

    // Check if the object is null.
    JVar hash = body.decl(JType.parse(context.getCodeModel(), "int"), "hash", JExpr.lit(3));

    // Do check for each field.
    for (DataFieldSpecification fieldSpec : classSpec.getFields()) {
      List<String> parts = NameFormat.namesToList(fieldSpec.getFieldName());
      String camelCaseFieldName = NameFormat.camelCase(parts, false);
      // Get the field value.
      JExpression thisField = JExpr.refthis(camelCaseFieldName);
      // Accumulate the hash code.
      JExpression fieldHashCode = objects.staticInvoke("hashCode").arg(thisField);

      body.assign(hash, JExpr.lit(79).mul(hash).plus(fieldHashCode));
    }

    // Return the processed hash value.
    body._return(hash);
  }
Ejemplo n.º 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;
  }
Ejemplo n.º 4
0
 /**
  * Creates the equals method on the supplied class. Leverages {@link
  * org.ldaptive.LdapUtils#areEqual(Object, Object)}.
  *
  * @param clazz to put equals method on
  */
 private void createEquals(final JDefinedClass clazz) {
   final JClass ldapUtilsClass = codeModel.ref(org.ldaptive.LdapUtils.class);
   final JInvocation areEqual = ldapUtilsClass.staticInvoke("areEqual");
   final JMethod equals = clazz.method(JMod.PUBLIC, boolean.class, "equals");
   equals.annotate(java.lang.Override.class);
   areEqual.arg(JExpr._this());
   areEqual.arg(equals.param(Object.class, "o"));
   equals.body()._return(areEqual);
 }
Ejemplo n.º 5
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());
 }
Ejemplo n.º 6
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)));
 }
Ejemplo n.º 7
0
 /**
  * Creates the hashCode method on the supplied class. Leverages {@link
  * org.ldaptive.LdapUtils#computeHashCode(int, Object...)}.
  *
  * @param clazz to put hashCode method on
  */
 private void createHashCode(final JDefinedClass clazz) {
   final JClass ldapUtilsClass = codeModel.ref(org.ldaptive.LdapUtils.class);
   final JInvocation computeHashCode = ldapUtilsClass.staticInvoke("computeHashCode");
   final JMethod hashCode = clazz.method(JMod.PUBLIC, int.class, "hashCode");
   hashCode.annotate(java.lang.Override.class);
   // CheckStyle:MagicNumber OFF
   computeHashCode.arg(JExpr.lit(7919));
   // CheckStyle:MagicNumber ON
   for (Map.Entry<String, JFieldVar> entry : clazz.fields().entrySet()) {
     computeHashCode.arg(JExpr._this().ref(entry.getValue()));
   }
   hashCode.body()._return(computeHashCode);
 }
Ejemplo n.º 8
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;
 }
Ejemplo n.º 9
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"));
  }
Ejemplo n.º 10
0
  /**
   * Creates the toString method on the supplied class. Creates a string that contains every
   * property on the generated bean.
   *
   * @param clazz to put toString method on
   */
  private void createToString(final JDefinedClass clazz) {
    final JClass stringClass = codeModel.ref(java.lang.String.class);
    final JInvocation format = stringClass.staticInvoke("format");
    final JMethod toString = clazz.method(JMod.PUBLIC, String.class, "toString");
    toString.annotate(java.lang.Override.class);

    final StringBuilder sb = new StringBuilder("[%s@%d::");
    for (Map.Entry<String, JFieldVar> entry : clazz.fields().entrySet()) {
      sb.append(entry.getKey()).append("=%s, ");
    }
    sb.setLength(sb.length() - 2);
    sb.append("]");
    format.arg(sb.toString());
    format.arg(JExpr._this().invoke("getClass").invoke("getName"));
    format.arg(JExpr._this().invoke("hashCode"));
    for (Map.Entry<String, JFieldVar> entry : clazz.fields().entrySet()) {
      format.arg(JExpr._this().ref(entry.getValue()));
    }
    toString.body()._return(format);
  }