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

io.joynr.generator.provider.DefaultInterfaceProviderTemplate Maven / Gradle / Ivy

There is a newer version: 1.25.0
Show newest version
package io.joynr.generator.provider;

import com.google.inject.Inject;
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.StringExtensions;
import org.franca.core.franca.FArgument;
import org.franca.core.franca.FAttribute;
import org.franca.core.franca.FMethod;

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

  @Inject
  @Extension
  private NamingUtil _namingUtil;

  @Inject
  @Extension
  private InterfaceUtil _interfaceUtil;

  @Inject
  @Extension
  private AttributeUtil _attributeUtil;

  @Inject
  @Extension
  private MethodUtil _methodUtil;

  @Inject
  @Extension
  private JavaTypeUtil _javaTypeUtil;

  @Inject
  @Extension
  private TemplateBase _templateBase;

  @Override
  public CharSequence generate(final boolean generateVersion) {
    CharSequence _xblockexpression = null;
    {
      HashMap methodToDeferredName = new HashMap();
      ArrayList uniqueMethodsToCreateDeferreds = new ArrayList();
      this.init(this.francaIntf, methodToDeferredName, uniqueMethodsToCreateDeferreds);
      final String interfaceName = this._namingUtil.joynrName(this.francaIntf);
      final String className = (("Default" + interfaceName) + "Provider");
      final String abstractProviderName = (interfaceName + "AbstractProvider");
      final String packagePath = this._joynrJavaGeneratorExtensions.getPackagePathWithJoynrPrefix(this.francaIntf, ".", generateVersion);
      StringConcatenation _builder = new StringConcatenation();
      CharSequence _warning = this._templateBase.warning();
      _builder.append(_warning);
      _builder.newLineIfNotEmpty();
      _builder.append("package ");
      _builder.append(packagePath);
      _builder.append(";");
      _builder.newLineIfNotEmpty();
      _builder.newLine();
      _builder.append("import org.slf4j.Logger;");
      _builder.newLine();
      _builder.append("import org.slf4j.LoggerFactory;");
      _builder.newLine();
      _builder.newLine();
      {
        if (((this._interfaceUtil.hasNonFireAndForgetMethods(this.francaIntf) || this._interfaceUtil.hasReadAttribute(this.francaIntf)) || this._interfaceUtil.hasWriteAttribute(this.francaIntf))) {
          _builder.append("import io.joynr.provider.Promise;");
          _builder.newLine();
          {
            boolean _hasReadAttribute = this._interfaceUtil.hasReadAttribute(this.francaIntf);
            if (_hasReadAttribute) {
              _builder.append("import io.joynr.provider.Deferred;");
              _builder.newLine();
            }
          }
          {
            if ((this._interfaceUtil.hasWriteAttribute(this.francaIntf) || this._interfaceUtil.hasMethodWithoutReturnValue(this.francaIntf))) {
              _builder.append("import io.joynr.provider.DeferredVoid;");
              _builder.newLine();
            }
          }
        }
      }
      _builder.newLine();
      {
        Iterable _requiredIncludesFor = this._joynrJavaGeneratorExtensions.getRequiredIncludesFor(this.francaIntf, true, true, true, false, false, true, generateVersion);
        for(final String datatype : _requiredIncludesFor) {
          _builder.append("import ");
          _builder.append(datatype);
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.newLine();
      _builder.append("public class ");
      _builder.append(className);
      _builder.append(" extends ");
      _builder.append(abstractProviderName);
      _builder.append(" {");
      _builder.newLineIfNotEmpty();
      _builder.append("\t");
      _builder.append("private static final Logger logger = LoggerFactory.getLogger(");
      _builder.append(className, "\t");
      _builder.append(".class);");
      _builder.newLineIfNotEmpty();
      _builder.newLine();
      {
        EList _attributes = this._interfaceUtil.getAttributes(this.francaIntf);
        for(final FAttribute attribute : _attributes) {
          _builder.append("\t");
          final String attributeName = this._namingUtil.joynrName(attribute);
          _builder.newLineIfNotEmpty();
          {
            boolean _isTypeDef = this._javaTypeUtil.isTypeDef(attribute.getType());
            if (_isTypeDef) {
              _builder.append("\t");
              final String typeDefType = this._javaTypeUtil.getTypeName(this._javaTypeUtil.getTypeDefType(attribute.getType()).getActualType());
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("// type resolved from modeled Franca typedef ");
              String _joynrName = this._namingUtil.joynrName(attribute.getType());
              _builder.append(_joynrName, "\t");
              _builder.append(" as ");
              _builder.append(typeDefType, "\t");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("\t");
          _builder.append("protected ");
          String _typeName = this._javaTypeUtil.getTypeName(attribute);
          _builder.append(_typeName, "\t");
          _builder.append(" ");
          _builder.append(attributeName, "\t");
          _builder.append(";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.newLine();
      _builder.append("\t");
      _builder.append("public ");
      _builder.append(className, "\t");
      _builder.append("() {");
      _builder.newLineIfNotEmpty();
      _builder.append("\t");
      _builder.append("}");
      _builder.newLine();
      _builder.newLine();
      {
        EList _attributes_1 = this._interfaceUtil.getAttributes(this.francaIntf);
        for(final FAttribute attribute_1 : _attributes_1) {
          _builder.append("\t");
          final String attributeName_1 = this._namingUtil.joynrName(attribute_1);
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          final String attributeType = this._javaTypeUtil.getTypeName(attribute_1);
          _builder.newLineIfNotEmpty();
          _builder.newLine();
          {
            boolean _isReadable = this._attributeUtil.isReadable(attribute_1);
            if (_isReadable) {
              _builder.append("\t");
              _builder.append("@Override");
              _builder.newLine();
              _builder.append("\t");
              _builder.append("public Promise> get");
              String _firstUpper = StringExtensions.toFirstUpper(attributeName_1);
              _builder.append(_firstUpper, "\t");
              _builder.append("() {");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("Deferred<");
              _builder.append(attributeType, "\t\t");
              _builder.append("> deferred = new Deferred<>();");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("deferred.resolve(");
              _builder.append(attributeName_1, "\t\t");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("return new Promise<>(deferred);");
              _builder.newLine();
              _builder.append("\t");
              _builder.append("}");
              _builder.newLine();
            }
          }
          _builder.newLine();
          {
            boolean _isWritable = this._attributeUtil.isWritable(attribute_1);
            if (_isWritable) {
              _builder.append("\t");
              _builder.append("@Override");
              _builder.newLine();
              _builder.append("\t");
              _builder.append("public Promise set");
              String _firstUpper_1 = StringExtensions.toFirstUpper(attributeName_1);
              _builder.append(_firstUpper_1, "\t");
              _builder.append("(");
              _builder.append(attributeType, "\t");
              _builder.append(" ");
              _builder.append(attributeName_1, "\t");
              _builder.append(") {");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("DeferredVoid deferred = new DeferredVoid();");
              _builder.newLine();
              {
                if ((this._javaTypeUtil.isArray(attribute_1) || this._javaTypeUtil.isByteBuffer(attribute_1.getType()))) {
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("if (");
                  _builder.append(attributeName_1, "\t\t");
                  _builder.append(" != null) {");
                  _builder.newLineIfNotEmpty();
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("this.");
                  _builder.append(attributeName_1, "\t\t\t");
                  _builder.append(" = ");
                  _builder.append(attributeName_1, "\t\t\t");
                  _builder.append(".clone();");
                  _builder.newLineIfNotEmpty();
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("} else {");
                  _builder.newLine();
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("this.");
                  _builder.append(attributeName_1, "\t\t\t");
                  _builder.append(" = null;");
                  _builder.newLineIfNotEmpty();
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("}");
                  _builder.newLine();
                } else {
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append("this.");
                  _builder.append(attributeName_1, "\t\t");
                  _builder.append(" = ");
                  _builder.append(attributeName_1, "\t\t");
                  _builder.append(";");
                  _builder.newLineIfNotEmpty();
                }
              }
              {
                boolean _isNotifiable = this._attributeUtil.isNotifiable(attribute_1);
                if (_isNotifiable) {
                  _builder.append("\t");
                  _builder.append("\t");
                  _builder.append(attributeName_1, "\t\t");
                  _builder.append("Changed(");
                  _builder.append(attributeName_1, "\t\t");
                  _builder.append(");");
                  _builder.newLineIfNotEmpty();
                }
              }
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("deferred.resolve();");
              _builder.newLine();
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("return new Promise<>(deferred);");
              _builder.newLine();
              _builder.append("\t");
              _builder.append("}");
              _builder.newLine();
            }
          }
        }
      }
      _builder.newLine();
      {
        EList _methods = this._interfaceUtil.getMethods(this.francaIntf);
        for(final FMethod method : _methods) {
          _builder.append("\t");
          String methodName = this._namingUtil.joynrName(method);
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          String deferredName = methodToDeferredName.get(method);
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          String params = this._javaTypeUtil.getTypedParameterList(this._methodUtil.getInputParameters(method));
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          final Iterable outputParameters = this._methodUtil.getOutputParameters(method);
          _builder.newLineIfNotEmpty();
          _builder.newLine();
          _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("@Override");
          _builder.newLine();
          {
            boolean _isFireAndForget = method.isFireAndForget();
            if (_isFireAndForget) {
              _builder.append("\t");
              _builder.append("public void ");
              _builder.append(methodName, "\t");
              _builder.append("(");
              _builder.newLineIfNotEmpty();
            } else {
              _builder.append("\t");
              _builder.append("public Promise<");
              _builder.append(deferredName, "\t");
              _builder.append("> ");
              _builder.append(methodName, "\t");
              _builder.append("(");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("\t");
          _builder.append("\t\t");
          {
            boolean _equals = params.equals("");
            boolean _not = (!_equals);
            if (_not) {
              _builder.append(params, "\t\t\t");
            }
          }
          _builder.append(") {");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("\t");
          _builder.append("logger.warn(\"**********************************************\");");
          _builder.newLine();
          _builder.append("\t");
          _builder.append("\t");
          _builder.append("logger.warn(\"* ");
          _builder.append(className, "\t\t");
          _builder.append(".");
          _builder.append(methodName, "\t\t");
          _builder.append(" called\");");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("\t");
          _builder.append("logger.warn(\"**********************************************\");");
          _builder.newLine();
          {
            boolean _isFireAndForget_1 = method.isFireAndForget();
            boolean _not_1 = (!_isFireAndForget_1);
            if (_not_1) {
              _builder.append("\t");
              _builder.append("\t");
              _builder.append(deferredName, "\t\t");
              _builder.append(" deferred = new ");
              _builder.append(deferredName, "\t\t");
              _builder.append("();");
              _builder.newLineIfNotEmpty();
              {
                for(final FArgument outputParameter : outputParameters) {
                  _builder.append("\t");
                  _builder.append("\t");
                  String _typeName_1 = this._javaTypeUtil.getTypeName(outputParameter);
                  _builder.append(_typeName_1, "\t\t");
                  _builder.append(" ");
                  String _name = outputParameter.getName();
                  _builder.append(_name, "\t\t");
                  _builder.append(" = ");
                  String _defaultValue = this._javaTypeUtil.getDefaultValue(outputParameter);
                  _builder.append(_defaultValue, "\t\t");
                  _builder.append(";");
                  _builder.newLineIfNotEmpty();
                }
              }
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("deferred.resolve(");
              String _commaSeperatedUntypedOutputParameterList = this._javaTypeUtil.getCommaSeperatedUntypedOutputParameterList(method);
              _builder.append(_commaSeperatedUntypedOutputParameterList, "\t\t");
              _builder.append(");");
              _builder.newLineIfNotEmpty();
              _builder.append("\t");
              _builder.append("\t");
              _builder.append("return new Promise<>(deferred);");
              _builder.newLine();
            }
          }
          _builder.append("\t");
          _builder.append("}");
          _builder.newLine();
        }
      }
      _builder.append("}");
      _builder.newLine();
      _xblockexpression = _builder;
    }
    return _xblockexpression;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy