Java Code Examples for org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration#addAnnotation()

The following examples show how to use org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration#addAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: xtext-lib   File: DataProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public void addDataToString(final MutableClassDeclaration cls) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getString());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("String result = new ");
        _builder.append(ToStringHelper.class);
        _builder.append("().toString(this);");
        _builder.newLineIfNotEmpty();
        _builder.append("return result;");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("toString", _function);
}
 
Example 2
public void addEquals(final MutableClassDeclaration cls, final Iterable<? extends FieldDeclaration> includedFields, final boolean includeSuper) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getPrimitiveBoolean());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    it.addParameter("obj", this.context.getObject());
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("if (this == obj)");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("return true;");
        _builder.newLine();
        _builder.append("if (obj == null)");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("return false;");
        _builder.newLine();
        _builder.append("if (getClass() != obj.getClass())");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("return false;");
        _builder.newLine();
        {
          if (includeSuper) {
            _builder.append("if (!super.equals(obj))");
            _builder.newLine();
            _builder.append("  ");
            _builder.append("return false;");
            _builder.newLine();
          }
        }
        {
          int _size = IterableExtensions.size(includedFields);
          boolean _greaterThan = (_size > 0);
          if (_greaterThan) {
            TypeReference _newWildCardSelfTypeReference = Util.this.newWildCardSelfTypeReference(cls);
            _builder.append(_newWildCardSelfTypeReference);
            _builder.append(" other = (");
            TypeReference _newWildCardSelfTypeReference_1 = Util.this.newWildCardSelfTypeReference(cls);
            _builder.append(_newWildCardSelfTypeReference_1);
            _builder.append(") obj;");
            _builder.newLineIfNotEmpty();
          }
        }
        {
          for(final FieldDeclaration field : includedFields) {
            StringConcatenationClient _contributeToEquals = Util.this.contributeToEquals(field);
            _builder.append(_contributeToEquals);
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("return true;");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("equals", _function);
}
 
Example 3
public void addHashCode(final MutableClassDeclaration cls, final Iterable<? extends FieldDeclaration> includedFields, final boolean includeSuper) {
  String _xifexpression = null;
  if (includeSuper) {
    _xifexpression = "super.hashCode()";
  } else {
    _xifexpression = "1";
  }
  final String defaultBase = _xifexpression;
  final int fields = IterableExtensions.size(includedFields);
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getPrimitiveInt());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        {
          if ((fields >= 2)) {
            _builder.append("final int prime = ");
            _builder.append(EqualsHashCodeProcessor.Util.PRIME_VALUE);
            _builder.append(";");
            _builder.newLineIfNotEmpty();
            _builder.append("int result = ");
            _builder.append(defaultBase);
            _builder.append(";");
            _builder.newLineIfNotEmpty();
            {
              ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, fields, true);
              for(final Integer i : _doubleDotLessThan) {
                {
                  if (((i).intValue() == (fields - 1))) {
                    _builder.append("return");
                  } else {
                    _builder.append("result =");
                  }
                }
                _builder.append(" prime * result + ");
                StringConcatenationClient _contributeToHashCode = Util.this.contributeToHashCode(((FieldDeclaration[])Conversions.unwrapArray(includedFields, FieldDeclaration.class))[(i).intValue()]);
                _builder.append(_contributeToHashCode);
                _builder.append(";");
                _builder.newLineIfNotEmpty();
              }
            }
          } else {
            if ((fields == 1)) {
              _builder.append("return ");
              _builder.append(EqualsHashCodeProcessor.Util.PRIME_VALUE);
              _builder.append(" * ");
              _builder.append(defaultBase);
              _builder.append(" + ");
              StringConcatenationClient _contributeToHashCode_1 = Util.this.contributeToHashCode(IterableExtensions.head(includedFields));
              _builder.append(_contributeToHashCode_1);
              _builder.append(";");
              _builder.newLineIfNotEmpty();
            } else {
              _builder.append("return ");
              _builder.append(defaultBase);
              _builder.append(";");
              _builder.newLineIfNotEmpty();
            }
          }
        }
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("hashCode", _function);
}
 
Example 4
public void addReflectiveToString(final MutableClassDeclaration cls, final ToStringConfiguration config) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getString());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("return new ");
        _builder.append(ToStringBuilder.class);
        _builder.append("(this)");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append(".addAllFields()");
        _builder.newLine();
        _builder.append("\t");
        {
          boolean _isSkipNulls = config.isSkipNulls();
          if (_isSkipNulls) {
            _builder.append(".skipNulls()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        {
          boolean _isSingleLine = config.isSingleLine();
          if (_isSingleLine) {
            _builder.append(".singleLine()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        {
          boolean _isHideFieldNames = config.isHideFieldNames();
          if (_isHideFieldNames) {
            _builder.append(".hideFieldNames()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        {
          boolean _isVerbatimValues = config.isVerbatimValues();
          if (_isVerbatimValues) {
            _builder.append(".verbatimValues()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append(".toString();");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("toString", _function);
}
 
Example 5
public void addToString(final MutableClassDeclaration cls, final Iterable<? extends FieldDeclaration> fields, final ToStringConfiguration config) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getString());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append(ToStringBuilder.class);
        _builder.append(" b = new ");
        _builder.append(ToStringBuilder.class);
        _builder.append("(this);");
        _builder.newLineIfNotEmpty();
        {
          boolean _isSkipNulls = config.isSkipNulls();
          if (_isSkipNulls) {
            _builder.append("b.skipNulls();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          boolean _isSingleLine = config.isSingleLine();
          if (_isSingleLine) {
            _builder.append("b.singleLine();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          boolean _isHideFieldNames = config.isHideFieldNames();
          if (_isHideFieldNames) {
            _builder.append("b.hideFieldNames();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          boolean _isVerbatimValues = config.isVerbatimValues();
          if (_isVerbatimValues) {
            _builder.append("b.verbatimValues();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          for(final FieldDeclaration field : fields) {
            _builder.append("b.add(\"");
            String _simpleName = field.getSimpleName();
            _builder.append(_simpleName);
            _builder.append("\", this.");
            String _simpleName_1 = field.getSimpleName();
            _builder.append(_simpleName_1);
            _builder.append(");");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("return b.toString();");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("toString", _function);
}
 
Example 6
protected MutableMethodDeclaration generateToString(final MutableClassDeclaration impl, @Extension final TransformationContext context) {
  MutableMethodDeclaration _xblockexpression = null;
  {
    final ArrayList<FieldDeclaration> toStringFields = CollectionLiterals.<FieldDeclaration>newArrayList();
    ClassDeclaration c = impl;
    do {
      {
        Iterable<? extends FieldDeclaration> _declaredFields = c.getDeclaredFields();
        Iterables.<FieldDeclaration>addAll(toStringFields, _declaredFields);
        TypeReference _extendedClass = c.getExtendedClass();
        Type _type = null;
        if (_extendedClass!=null) {
          _type=_extendedClass.getType();
        }
        c = ((ClassDeclaration) _type);
      }
    } while(((c != null) && (!Objects.equal(c, context.getObject()))));
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
      it.setReturnType(context.getString());
      it.addAnnotation(context.newAnnotationReference(Override.class));
      it.addAnnotation(context.newAnnotationReference(Pure.class));
      final AccessorsProcessor.Util accessorsUtil = new AccessorsProcessor.Util(context);
      StringConcatenationClient _client = new StringConcatenationClient() {
        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
          _builder.append(ToStringBuilder.class);
          _builder.append(" b = new ");
          _builder.append(ToStringBuilder.class);
          _builder.append("(this);");
          _builder.newLineIfNotEmpty();
          {
            for(final FieldDeclaration field : toStringFields) {
              _builder.append("b.add(\"");
              String _simpleName = field.getSimpleName();
              _builder.append(_simpleName);
              _builder.append("\", ");
              {
                TypeDeclaration _declaringType = field.getDeclaringType();
                boolean _equals = Objects.equal(_declaringType, impl);
                if (_equals) {
                  _builder.append("this.");
                  String _simpleName_1 = field.getSimpleName();
                  _builder.append(_simpleName_1);
                } else {
                  String _getterName = accessorsUtil.getGetterName(field);
                  _builder.append(_getterName);
                  _builder.append("()");
                }
              }
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("return b.toString();");
          _builder.newLine();
        }
      };
      it.setBody(_client);
    };
    _xblockexpression = impl.addMethod("toString", _function);
  }
  return _xblockexpression;
}