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

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

The newest version!
package io.joynr.generator.interfaces;

import com.google.common.base.Objects;
import com.google.inject.Inject;
import io.joynr.generator.templates.InterfaceTemplate;
import io.joynr.generator.templates.util.AttributeUtil;
import io.joynr.generator.templates.util.InterfaceUtil;
import io.joynr.generator.templates.util.MethodUtil;
import io.joynr.generator.templates.util.NamingUtil;
import io.joynr.generator.util.JavaTypeUtil;
import io.joynr.generator.util.JoynrJavaGeneratorExtensions;
import io.joynr.generator.util.TemplateBase;
import java.util.ArrayList;
import java.util.HashMap;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.franca.core.franca.FArgument;
import org.franca.core.franca.FAttribute;
import org.franca.core.franca.FInterface;
import org.franca.core.franca.FMethod;

@SuppressWarnings("all")
public class InterfaceSyncTemplate extends InterfaceTemplate {
  @Inject
  @Extension
  private JoynrJavaGeneratorExtensions _joynrJavaGeneratorExtensions;

  @Inject
  @Extension
  private JavaTypeUtil _javaTypeUtil;

  @Inject
  @Extension
  private MethodUtil _methodUtil;

  @Inject
  @Extension
  private AttributeUtil _attributeUtil;

  @Inject
  @Extension
  private InterfaceUtil _interfaceUtil;

  @Inject
  @Extension
  private NamingUtil _namingUtil;

  @Inject
  @Extension
  private TemplateBase _templateBase;

  public void init(final FInterface serviceInterface, final HashMap methodToReturnTypeName, final ArrayList uniqueMultioutMethods, final boolean generateVersion) {
    HashMap uniqueMultioutMethodSignatureToContainerNames = new HashMap();
    HashMap methodCounts = this._methodUtil.overloadedMethodCounts(this._interfaceUtil.getMethods(serviceInterface));
    HashMap indexForMethod = new HashMap();
    EList _methods = this._interfaceUtil.getMethods(serviceInterface);
    for (final FMethod method : _methods) {
      int _size = IterableExtensions.size(this._methodUtil.getOutputParameters(method));
      boolean _lessThan = (_size < 2);
      if (_lessThan) {
        final String outputParamterType = this._javaTypeUtil.getTypeNamesForOutputParameter(method).iterator().next();
        boolean _equals = Objects.equal(outputParamterType, "void");
        if (_equals) {
          methodToReturnTypeName.put(method, "void");
        } else {
          methodToReturnTypeName.put(method, this._javaTypeUtil.getObjectDataTypeForPlainType(outputParamterType));
        }
      } else {
        String containerName = StringExtensions.toFirstUpper(method.getName());
        Integer _get = methodCounts.get(method.getName());
        boolean _equals_1 = ((_get).intValue() == 1);
        if (_equals_1) {
          uniqueMultioutMethods.add(method);
          String _containerName = containerName;
          containerName = (_containerName + "Returned");
        } else {
          boolean _containsKey = indexForMethod.containsKey(method.getName());
          boolean _not = (!_containsKey);
          if (_not) {
            indexForMethod.put(method.getName(), Integer.valueOf(0));
          }
          final String methodSignature = this._methodUtil.createMethodSignatureFromOutParameters(method);
          boolean _containsKey_1 = uniqueMultioutMethodSignatureToContainerNames.containsKey(methodSignature);
          boolean _not_1 = (!_containsKey_1);
          if (_not_1) {
            Integer index = indexForMethod.get(method.getName());
            index++;
            indexForMethod.put(method.getName(), index);
            String _firstUpper = StringExtensions.toFirstUpper(method.getName());
            String _plus = (_firstUpper + index);
            uniqueMultioutMethodSignatureToContainerNames.put(methodSignature, _plus);
            uniqueMultioutMethods.add(method);
          }
          String _containerName_1 = containerName;
          String _get_1 = uniqueMultioutMethodSignatureToContainerNames.get(methodSignature);
          String _plus_1 = (_get_1 + "Returned");
          containerName = (_containerName_1 + _plus_1);
        }
        methodToReturnTypeName.put(method, containerName);
      }
    }
  }

  @Override
  public CharSequence generate(final boolean generateVersion) {
    CharSequence _xblockexpression = null;
    {
      HashMap methodToReturnTypeName = new HashMap();
      ArrayList uniqueMultioutMethods = new ArrayList();
      this.init(this.francaIntf, methodToReturnTypeName, uniqueMultioutMethods, generateVersion);
      final String interfaceName = this._namingUtil.joynrName(this.francaIntf);
      final String syncClassName = (interfaceName + "Sync");
      final String packagePath = this._joynrJavaGeneratorExtensions.getPackagePathWithJoynrPrefix(this.francaIntf, ".", generateVersion);
      StringConcatenation _builder = new StringConcatenation();
      CharSequence _warning = this._templateBase.warning();
      _builder.append(_warning);
      _builder.newLineIfNotEmpty();
      _builder.newLine();
      _builder.append("package ");
      _builder.append(packagePath);
      _builder.append(";");
      _builder.newLineIfNotEmpty();
      _builder.newLine();
      _builder.append("import io.joynr.messaging.MessagingQos;");
      _builder.newLine();
      _builder.append("import io.joynr.Sync;");
      _builder.newLine();
      _builder.append("import io.joynr.ProvidedBy;");
      _builder.newLine();
      _builder.append("import io.joynr.UsedBy;");
      _builder.newLine();
      {
        boolean _hasMethodWithErrorEnum = this._interfaceUtil.hasMethodWithErrorEnum(this.francaIntf);
        if (_hasMethodWithErrorEnum) {
          _builder.append("import joynr.exceptions.ApplicationException;");
          _builder.newLine();
        }
      }
      _builder.newLine();
      {
        Iterable _requiredIncludesFor = this._joynrJavaGeneratorExtensions.getRequiredIncludesFor(this.francaIntf, true, true, true, false, false, false, generateVersion);
        for(final String datatype : _requiredIncludesFor) {
          _builder.append("import ");
          _builder.append(datatype);
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.newLine();
      _builder.append("@Sync");
      _builder.newLine();
      _builder.append("@ProvidedBy(");
      String _providerClassName = this._joynrJavaGeneratorExtensions.getProviderClassName(this.francaIntf);
      _builder.append(_providerClassName);
      _builder.append(".class)");
      _builder.newLineIfNotEmpty();
      _builder.append("@UsedBy(");
      String _proxyClassName = this._joynrJavaGeneratorExtensions.getProxyClassName(this.francaIntf);
      _builder.append(_proxyClassName);
      _builder.append(".class)");
      _builder.newLineIfNotEmpty();
      _builder.append("public interface ");
      _builder.append(syncClassName);
      _builder.append(" extends ");
      _builder.append(interfaceName);
      {
        boolean _hasFireAndForgetMethods = this._interfaceUtil.hasFireAndForgetMethods(this.francaIntf);
        if (_hasFireAndForgetMethods) {
          _builder.append(", ");
          _builder.append(interfaceName);
          _builder.append("FireAndForget");
        }
      }
      _builder.append(" {");
      _builder.newLineIfNotEmpty();
      _builder.newLine();
      {
        EList _attributes = this._interfaceUtil.getAttributes(this.francaIntf);
        boolean _hasElements = false;
        for(final FAttribute attribute : _attributes) {
          if (!_hasElements) {
            _hasElements = true;
          } else {
            _builder.appendImmediate("\n", "");
          }
          String attributeName = this._namingUtil.joynrName(attribute);
          _builder.newLineIfNotEmpty();
          String attributeType = this._javaTypeUtil.getObjectDataTypeForPlainType(this._javaTypeUtil.getTypeName(attribute));
          _builder.newLineIfNotEmpty();
          String _firstUpper = StringExtensions.toFirstUpper(attributeName);
          String getAttribute = ("get" + _firstUpper);
          _builder.newLineIfNotEmpty();
          String _firstUpper_1 = StringExtensions.toFirstUpper(attributeName);
          String setAttribute = ("set" + _firstUpper_1);
          _builder.newLineIfNotEmpty();
          {
            boolean _isReadable = this._attributeUtil.isReadable(attribute);
            if (_isReadable) {
              _builder.append("\t");
              _builder.append("public ");
              _builder.append(attributeType, "\t");
              _builder.append(" ");
              _builder.append(getAttribute, "\t");
              _builder.append("();");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("default public ");
              _builder.append(attributeType, "\t");
              _builder.append(" ");
              _builder.append(getAttribute, "\t");
              _builder.append("(MessagingQos messagingQos) {");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("return ");
              _builder.append(getAttribute, "\t\t");
              _builder.append("();");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("}");
              _builder.newLine();
            }
          }
          {
            boolean _isWritable = this._attributeUtil.isWritable(attribute);
            if (_isWritable) {
              _builder.append("\t");
              _builder.append("void ");
              _builder.append(setAttribute, "\t");
              _builder.append("(");
              _builder.append(attributeType, "\t");
              _builder.append(" ");
              _builder.append(attributeName, "\t");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("default void ");
              _builder.append(setAttribute, "\t");
              _builder.append("(");
              _builder.append(attributeType, "\t");
              _builder.append(" ");
              _builder.append(attributeName, "\t");
              _builder.append(", MessagingQos messagingQos) {");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("}");
              _builder.newLine();
            }
          }
        }
      }
      _builder.newLine();
      {
        for(final FMethod method : uniqueMultioutMethods) {
          final String containerName = methodToReturnTypeName.get(method);
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("public class ");
          _builder.append(containerName, "\t");
          _builder.append(" implements io.joynr.dispatcher.rpc.MultiReturnValuesContainer {");
          _builder.newLineIfNotEmpty();
          {
            Iterable _outputParameters = this._methodUtil.getOutputParameters(method);
            for(final FArgument outParameter : _outputParameters) {
              _builder.append("\t\t");
              _builder.append("public final ");
              String _typeName = this._javaTypeUtil.getTypeName(outParameter);
              _builder.append(_typeName, "\t\t");
              _builder.append(" ");
              String _name = outParameter.getName();
              _builder.append(_name, "\t\t");
              _builder.append(";");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("\t\t");
          _builder.append("public ");
          _builder.append(containerName, "\t\t");
          _builder.append("(Object... outParameters) {");
          _builder.newLineIfNotEmpty();
          _builder.append("\t\t\t");
          int index = 0;
          _builder.newLineIfNotEmpty();
          {
            Iterable _outputParameters_1 = this._methodUtil.getOutputParameters(method);
            for(final FArgument outParameter_1 : _outputParameters_1) {
              _builder.append("\t\t\t");
              _builder.append("this.");
              String _name_1 = outParameter_1.getName();
              _builder.append(_name_1, "\t\t\t");
              _builder.append(" = (");
              String _typeName_1 = this._javaTypeUtil.getTypeName(outParameter_1);
              _builder.append(_typeName_1, "\t\t\t");
              _builder.append(") outParameters[");
              int _plusPlus = index++;
              _builder.append(_plusPlus, "\t\t\t");
              _builder.append("];");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("\t\t");
          _builder.append("}");
          _builder.newLine();
          _builder.newLine();
          _builder.append("\t\t");
          _builder.append("public static Class[] getDatatypes() {");
          _builder.newLine();
          _builder.append("\t\t\t");
          _builder.append("return new Class[] {");
          {
            Iterable _outputParameters_2 = this._methodUtil.getOutputParameters(method);
            boolean _hasElements_1 = false;
            for(final FArgument outParameter_2 : _outputParameters_2) {
              if (!_hasElements_1) {
                _hasElements_1 = true;
              } else {
                _builder.appendImmediate(", ", "\t\t\t");
              }
              String _typeName_2 = this._javaTypeUtil.getTypeName(outParameter_2);
              _builder.append(_typeName_2, "\t\t\t");
              _builder.append(".class");
            }
          }
          _builder.append("};");
          _builder.newLineIfNotEmpty();
          _builder.append("\t\t");
          _builder.append("}");
          _builder.newLine();
          _builder.newLine();
          _builder.append("\t\t");
          _builder.append("public Object[] getValues() {");
          _builder.newLine();
          _builder.append("\t\t    ");
          _builder.append("return new Object[] {");
          _builder.newLine();
          {
            Iterable _outputParameters_3 = this._methodUtil.getOutputParameters(method);
            boolean _hasElements_2 = false;
            for(final FArgument outParameter_3 : _outputParameters_3) {
              if (!_hasElements_2) {
                _hasElements_2 = true;
              } else {
                _builder.appendImmediate(",", "\t\t        ");
              }
              _builder.append("\t\t        ");
              String _name_2 = outParameter_3.getName();
              _builder.append(_name_2, "\t\t        ");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("\t\t    ");
          _builder.append("};");
          _builder.newLine();
          _builder.append("\t\t");
          _builder.append("}");
          _builder.newLine();
          _builder.append("\t");
          _builder.append("}");
          _builder.newLine();
        }
      }
      _builder.newLine();
      {
        final Function1 _function = new Function1() {
          @Override
          public Boolean apply(final FMethod it) {
            boolean _isFireAndForget = it.isFireAndForget();
            return Boolean.valueOf((!_isFireAndForget));
          }
        };
        Iterable _filter = IterableExtensions.filter(this._interfaceUtil.getMethods(this.francaIntf), _function);
        boolean _hasElements_3 = false;
        for(final FMethod method_1 : _filter) {
          if (!_hasElements_3) {
            _hasElements_3 = true;
          } else {
            _builder.appendImmediate("\n", "");
          }
          String methodName = this._namingUtil.joynrName(method_1);
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("/*");
          _builder.newLine();
          _builder.append("\t");
          _builder.append("* ");
          _builder.append(methodName, "\t");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("*/");
          _builder.newLine();
          _builder.append("\t");
          _builder.append("public ");
          String _get = methodToReturnTypeName.get(method_1);
          _builder.append(_get, "\t");
          _builder.append(" ");
          _builder.append(methodName, "\t");
          _builder.append("(");
          _builder.newLineIfNotEmpty();
          _builder.append("\t\t\t");
          String _typedParameterList = this._javaTypeUtil.getTypedParameterList(this._methodUtil.getInputParameters(method_1));
          _builder.append(_typedParameterList, "\t\t\t");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append(")");
          {
            boolean _hasErrorEnum = this._methodUtil.hasErrorEnum(method_1);
            if (_hasErrorEnum) {
              _builder.append(" throws ApplicationException");
            }
          }
          _builder.append(";");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("default public ");
          String _get_1 = methodToReturnTypeName.get(method_1);
          _builder.append(_get_1, "\t");
          _builder.append(" ");
          _builder.append(methodName, "\t");
          _builder.append("(");
          _builder.newLineIfNotEmpty();
          _builder.append("\t\t\t");
          String _typedParameterList_1 = this._javaTypeUtil.getTypedParameterList(this._methodUtil.getInputParameters(method_1));
          _builder.append(_typedParameterList_1, "\t\t\t");
          {
            boolean _isEmpty = IterableExtensions.isEmpty(this._methodUtil.getInputParameters(method_1));
            boolean _not = (!_isEmpty);
            if (_not) {
              _builder.append(",");
            }
          }
          _builder.newLineIfNotEmpty();
          _builder.append("\t\t\t");
          _builder.append("MessagingQos messagingQos");
          _builder.newLine();
          _builder.append("\t");
          _builder.append(")");
          {
            boolean _hasErrorEnum_1 = this._methodUtil.hasErrorEnum(method_1);
            if (_hasErrorEnum_1) {
              _builder.append(" throws ApplicationException");
            }
          }
          _builder.append(" {");
          _builder.newLineIfNotEmpty();
          {
            boolean _equals = methodToReturnTypeName.get(method_1).equals("void");
            if (_equals) {
              _builder.append("\t\t");
              _builder.append("return;");
              _builder.newLine();
            } else {
              _builder.append("\t\t");
              _builder.append("return ");
              _builder.append(methodName, "\t\t");
              _builder.append("(");
              _builder.newLineIfNotEmpty();
              {
                Iterable _inputParameters = this._methodUtil.getInputParameters(method_1);
                boolean _hasElements_4 = false;
                for(final FArgument inParameter : _inputParameters) {
                  if (!_hasElements_4) {
                    _hasElements_4 = true;
                  } else {
                    _builder.appendImmediate(",", "\t\t\t");
                  }
                  _builder.append("\t\t");
                  _builder.append("\t");
                  String _name_3 = inParameter.getName();
                  _builder.append(_name_3, "\t\t\t");
                  _builder.newLineIfNotEmpty();
                }
              }
              _builder.append("\t\t");
              _builder.append(");");
              _builder.newLine();
            }
          }
          _builder.append("\t");
          _builder.append("}");
          _builder.newLine();
        }
      }
      _builder.append("}");
      _builder.newLine();
      _xblockexpression = _builder;
    }
    return _xblockexpression;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy