io.joynr.generator.interfaces.InterfaceAsyncTemplate Maven / Gradle / Ivy
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.Iterator;
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.FAttribute;
import org.franca.core.franca.FInterface;
import org.franca.core.franca.FMethod;
@SuppressWarnings("all")
public class InterfaceAsyncTemplate {
@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 asyncClassName = (interfaceName + "Async");
final String packagePath = this._joynrJavaGeneratorExtensions.getPackagePathWithJoynrPrefix(serviceInterface, ".");
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();
{
boolean _needsListImport = this._joynrJavaGeneratorExtensions.needsListImport(serviceInterface);
if (_needsListImport) {
_builder.append("import java.util.List;");
_builder.newLine();
}
}
_builder.append("import com.fasterxml.jackson.core.type.TypeReference;");
_builder.newLine();
_builder.newLine();
_builder.append("import io.joynr.dispatcher.rpc.Callback;");
_builder.newLine();
_builder.append("import io.joynr.dispatcher.rpc.JoynrAsyncInterface;");
_builder.newLine();
_builder.append("import io.joynr.dispatcher.rpc.annotation.JoynrRpcCallback;");
_builder.newLine();
_builder.append("import io.joynr.dispatcher.rpc.annotation.JoynrRpcParam;");
_builder.newLine();
_builder.append("import io.joynr.proxy.Future;");
_builder.newLine();
_builder.newLine();
{
Iterable _requiredIncludesFor = this._joynrJavaGeneratorExtensions.getRequiredIncludesFor(serviceInterface);
for(final String datatype : _requiredIncludesFor) {
_builder.append("import ");
_builder.append(datatype, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_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(asyncClassName, "");
_builder.append(" extends ");
_builder.append(interfaceName, "");
_builder.append(", JoynrAsyncInterface {");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("public static class VoidToken extends TypeReference {}\t");
_builder.newLine();
{
EList _attributes = this._joynrJavaGeneratorExtensions.getAttributes(serviceInterface);
for(final FAttribute attribute : _attributes) {
_builder.append("\t");
String attributeName = this._joynrJavaGeneratorExtensions.joynrName(attribute);
_builder.newLineIfNotEmpty();
_builder.append("\t");
String _mappedDatatypeOrList = this._joynrJavaGeneratorExtensions.getMappedDatatypeOrList(attribute);
String attributeType = this._joynrJavaGeneratorExtensions.getObjectDataTypeForPlainType(_mappedDatatypeOrList);
_builder.append(" ");
_builder.newLineIfNotEmpty();
_builder.append("\t");
String _firstUpper = StringExtensions.toFirstUpper(attributeName);
String getAttribute = ("get" + _firstUpper);
_builder.append(" ");
_builder.newLineIfNotEmpty();
{
boolean _isReadable = this._joynrJavaGeneratorExtensions.isReadable(attribute);
if (_isReadable) {
_builder.append("\t");
_builder.append("public Future<");
_builder.append(attributeType, " ");
_builder.append("> ");
_builder.append(getAttribute, " ");
_builder.append("(@JoynrRpcCallback(deserialisationType = ");
String _tokenTypeForArrayType = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(attributeType);
_builder.append(_tokenTypeForArrayType, " ");
_builder.append("Token.class) Callback<");
_builder.append(attributeType, " ");
_builder.append("> callback);\t");
_builder.newLineIfNotEmpty();
}
}
}
}
_builder.append("\t");
_builder.newLine();
{
EList _methods = this._joynrJavaGeneratorExtensions.getMethods(serviceInterface);
for(final FMethod method : _methods) {
_builder.append("\t");
String methodName = this._joynrJavaGeneratorExtensions.joynrName(method);
_builder.newLineIfNotEmpty();
_builder.append("\t");
Iterable _mappedOutputParameter = this._joynrJavaGeneratorExtensions.getMappedOutputParameter(method);
Iterator _iterator = _mappedOutputParameter.iterator();
String outPutParameterType = _iterator.next();
_builder.newLineIfNotEmpty();
_builder.append("\t");
String outPutObjectType = this._joynrJavaGeneratorExtensions.getObjectDataTypeForPlainType(outPutParameterType);
_builder.newLineIfNotEmpty();
_builder.append("\t");
String params = this._joynrJavaGeneratorExtensions.getTypedParameterListJavaRpc(method);
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("/*");
_builder.newLine();
_builder.append("\t");
_builder.append("* ");
_builder.append(methodName, " ");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("*/");
_builder.newLine();
{
Iterable _mappedOutputParameter_1 = this._joynrJavaGeneratorExtensions.getMappedOutputParameter(method);
Iterator _iterator_1 = _mappedOutputParameter_1.iterator();
String _next = _iterator_1.next();
boolean _equals = Objects.equal(_next, "void");
if (_equals) {
_builder.append("\t");
_builder.append("public void ");
_builder.append(methodName, " ");
_builder.append("(@JoynrRpcCallback(deserialisationType = VoidToken.class) Callback callback ");
CharSequence _generateMethodEnd = this.generateMethodEnd(params);
_builder.append(_generateMethodEnd, " ");
_builder.newLineIfNotEmpty();
} else {
{
boolean _equals_1 = Objects.equal(outPutObjectType, "");
if (_equals_1) {
_builder.append("\t");
_builder.append("public Future<");
_builder.append(outPutParameterType, " ");
_builder.append("> ");
_builder.append(methodName, " ");
_builder.append("(@JoynrRpcCallback(deserialisationType = ");
String _tokenTypeForArrayType_1 = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(outPutParameterType);
_builder.append(_tokenTypeForArrayType_1, " ");
_builder.append("Token.class) Callback<");
_builder.append(outPutParameterType, " ");
_builder.append("> callback ");
CharSequence _generateMethodEnd_1 = this.generateMethodEnd(params);
_builder.append(_generateMethodEnd_1, " ");
_builder.newLineIfNotEmpty();
} else {
_builder.append("\t");
_builder.append("public Future<");
_builder.append(outPutObjectType, " ");
_builder.append("> ");
_builder.append(methodName, " ");
_builder.append("(@JoynrRpcCallback(deserialisationType = ");
String _tokenTypeForArrayType_2 = this._joynrJavaGeneratorExtensions.getTokenTypeForArrayType(outPutObjectType);
_builder.append(_tokenTypeForArrayType_2, " ");
_builder.append("Token.class) Callback<");
_builder.append(outPutObjectType, " ");
_builder.append("> callback ");
CharSequence _generateMethodEnd_2 = this.generateMethodEnd(params);
_builder.append(_generateMethodEnd_2, " ");
_builder.newLineIfNotEmpty();
}
}
}
}
}
}
_builder.append("}");
_builder.newLine();
_xblockexpression = (_builder);
}
return _xblockexpression;
}
public CharSequence generateMethodEnd(final String params) {
StringConcatenation _builder = new StringConcatenation();
{
boolean _equals = params.equals("");
if (_equals) {
_builder.append(");");
_builder.newLine();
} else {
_builder.append(", ");
_builder.append(params, "");
_builder.append(");\t");
_builder.newLineIfNotEmpty();
}
}
return _builder;
}
}