Exemple #1
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);
  }
  private ModifierGenerator(
      final PluginContext pluginContext,
      final DefinedTypeOutline classOutline,
      final String modifierClassName,
      final String modifierInterfaceName,
      final Collection<TypeOutline> interfaces,
      final String modifierMethodName,
      final boolean implement)
      throws JClassAlreadyExistsException {
    this.classOutline = classOutline;
    final JDefinedClass definedClass = classOutline.getImplClass();
    this.implement = implement;
    this.modifierClass =
        definedClass._class(
            JMod.PUBLIC, modifierClassName, classOutline.getImplClass().getClassType());
    if (interfaces != null) {
      for (final TypeOutline interfaceOutline : interfaces) {
        this.modifierClass._implements(
            pluginContext.ref(interfaceOutline.getImplClass(), modifierInterfaceName, true));
      }
    }
    final JFieldRef cachedModifierField;
    if (!"java.lang.Object".equals(definedClass._extends().fullName())) {
      this.modifierClass._extends(
          pluginContext.ref(definedClass._extends(), modifierClassName, false));
      cachedModifierField = JExpr.refthis(ModifierGenerator.MODIFIER_CACHE_FIELD_NAME);
    } else {
      if (implement) {
        cachedModifierField =
            JExpr._this()
                .ref(
                    definedClass.field(
                        JMod.PROTECTED | JMod.TRANSIENT,
                        this.modifierClass,
                        ModifierGenerator.MODIFIER_CACHE_FIELD_NAME));
      } else {
        cachedModifierField = null;
      }
    }

    final JDefinedClass typeDefinition =
        classOutline.isInterface()
                && ((DefinedInterfaceOutline) classOutline).getSupportInterface() != null
            ? ((DefinedInterfaceOutline) classOutline).getSupportInterface()
            : definedClass;
    final JMethod modifierMethod =
        typeDefinition.method(JMod.PUBLIC, this.modifierClass, modifierMethodName);
    if (this.implement) {
      final JConditional ifCacheNull =
          modifierMethod.body()._if(JExpr._null().eq(cachedModifierField));
      ifCacheNull._then().assign(cachedModifierField, JExpr._new(this.modifierClass));
      modifierMethod.body()._return(JExpr.cast(this.modifierClass, cachedModifierField));
    }
  }