All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.joynr.generator.interfaces.InterfacesTemplate Maven / Gradle / Ivy

There is a newer version: 0.9.3
Show newest version
package io.joynr.generator.interfaces;

import com.google.common.base.Objects;
import com.google.inject.Inject;
import io.joynr.generator.util.JoynrJavaGeneratorExtensions;
import io.joynr.generator.util.TemplateBase;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
import org.franca.core.franca.FInterface;

@SuppressWarnings("all")
public class InterfacesTemplate {
  @Inject
  @Extension
  private JoynrJavaGeneratorExtensions _joynrJavaGeneratorExtensions;
  
  @Inject
  @Extension
  private TemplateBase _templateBase;
  
  public CharSequence generate(final FInterface serviceInterface) {
    CharSequence _xblockexpression = null;
    {
      final String interfaceName = this._joynrJavaGeneratorExtensions.joynrName(serviceInterface);
      final String className = interfaceName;
      final String packagePath = this._joynrJavaGeneratorExtensions.getPackagePathWithJoynrPrefix(serviceInterface, ".");
      StringConcatenation _builder = new StringConcatenation();
      _builder.newLine();
      _builder.append("\t\t");
      CharSequence _warning = this._templateBase.warning();
      _builder.append(_warning, "		");
      _builder.append("   ");
      _builder.newLineIfNotEmpty();
      _builder.append("package ");
      _builder.append(packagePath, "");
      _builder.append(";  \t");
      _builder.newLineIfNotEmpty();
      _builder.newLine();
      _builder.append("import java.util.List;");
      _builder.newLine();
      _builder.newLine();
      _builder.append("import com.fasterxml.jackson.core.type.TypeReference;");
      _builder.newLine();
      {
        Iterable _requiredIncludesFor = this._joynrJavaGeneratorExtensions.getRequiredIncludesFor(serviceInterface);
        for(final String datatype : _requiredIncludesFor) {
          _builder.append("import ");
          _builder.append(datatype, "");
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.newLine();
      _builder.append("//The current generator is not able to check wether some of the imports are acutally necessary for this specific interface.");
      _builder.newLine();
      _builder.append("//Therefore some imports migth be unused in this version of the interface.");
      _builder.newLine();
      _builder.append("//To prevent warnings @SuppressWarnings(\"unused\") is being used. ");
      _builder.newLine();
      _builder.append("//To prevent warnings about an unnecessary SuppressWarnings we have to import something that is not used. (e.g. TreeSet)");
      _builder.newLine();
      _builder.append("import java.util.TreeSet;");
      _builder.newLine();
      _builder.append("@SuppressWarnings(\"unused\")");
      _builder.newLine();
      _builder.append("public interface ");
      _builder.append(className, "");
      _builder.append("  {");
      _builder.newLineIfNotEmpty();
      _builder.append("    ");
      _builder.append("public static String INTERFACE_NAME = \"");
      String _packagePathWithoutJoynrPrefix = this._joynrJavaGeneratorExtensions.getPackagePathWithoutJoynrPrefix(serviceInterface, "/");
      _builder.append(_packagePathWithoutJoynrPrefix, "    ");
      _builder.append("/");
      String _lowerCase = interfaceName.toLowerCase();
      _builder.append(_lowerCase, "    ");
      _builder.append("\";");
      _builder.newLineIfNotEmpty();
      _builder.append("    ");
      _builder.newLine();
      {
        Collection _allTypes = this._joynrJavaGeneratorExtensions.getAllTypes(serviceInterface);
        ArrayList _filterTypesByToken = this.filterTypesByToken(_allTypes);
        for(final Object type : _filterTypesByToken) {
          _builder.append("\t");
          _builder.append("public static class ");
          String _xifexpression = null;
          String _typeName = this._joynrJavaGeneratorExtensions.typeName(type);
          boolean _equals = Objects.equal(_typeName, null);
          if (_equals) {
            _xifexpression = "Typename not found";
          } else {
            String _typeName_1 = this._joynrJavaGeneratorExtensions.typeName(type);
            String _tokenTypeForArrayType = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(_typeName_1);
            _xifexpression = _tokenTypeForArrayType;
          }
          _builder.append(_xifexpression, "	");
          _builder.append("Token extends TypeReference<");
          String _typeName_2 = this._joynrJavaGeneratorExtensions.typeName(type);
          String _tokenTypeForArrayType_1 = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(_typeName_2);
          _builder.append(_tokenTypeForArrayType_1, "	");
          _builder.append("> {}\t");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("public static class List");
          String _typeName_3 = this._joynrJavaGeneratorExtensions.typeName(type);
          String _tokenTypeForArrayType_2 = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(_typeName_3);
          _builder.append(_tokenTypeForArrayType_2, "	");
          _builder.append("Token extends TypeReference > {}\t");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("\t");
      _builder.newLine();
      _builder.append("}");
      _builder.newLine();
      _xblockexpression = (_builder);
    }
    return _xblockexpression;
  }
  
  public ArrayList filterTypesByToken(final Collection objects) {
    ArrayList _arrayList = new ArrayList();
    final ArrayList result = _arrayList;
    ArrayList _arrayList_1 = new ArrayList();
    final ArrayList tokens = _arrayList_1;
    for (final Object object : objects) {
      boolean _and = false;
      boolean _notEquals = (!Objects.equal(object, null));
      if (!_notEquals) {
        _and = false;
      } else {
        String _typeName = this._joynrJavaGeneratorExtensions.typeName(object);
        String _tokenTypeForArrayType = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(_typeName);
        boolean _contains = tokens.contains(_tokenTypeForArrayType);
        boolean _not = (!_contains);
        _and = (_notEquals && _not);
      }
      if (_and) {
        result.add(object);
        String _typeName_1 = this._joynrJavaGeneratorExtensions.typeName(object);
        String _tokenTypeForArrayType_1 = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(_typeName_1);
        tokens.add(_tokenTypeForArrayType_1);
      }
    }
    return result;
  }
}