blob: 2051a4af090f515758c548ad6efc7d8121db4ba2 [file] [log] [blame]
/**
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*/
package org.eclipse.osbp.xtext.functionlibrarydsl.jvmmodel;
import com.google.common.base.Objects;
import com.google.inject.Inject;
import java.util.Arrays;
import java.util.Locale;
import java.util.function.Consumer;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osbp.bpm.BlipBaseFunctionGroup;
import org.eclipse.osbp.ui.api.functionlibrary.IFunctionLibraryGroup;
import org.eclipse.osbp.ui.api.functionlibrary.IFunctionLibraryPackage;
import org.eclipse.osbp.utils.annotation.CommonUtils;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionConvertToModel;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionConvertToPresentation;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryBaseFunction;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryBlipGroup;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryCanExecute;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryConverterGroup;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryExecute;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryFunction;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryGroup;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryGuard;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryOperation;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryPackage;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryParameter;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryRate;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryRating;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryRatingItem;
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryTest;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.common.types.JvmAnnotationReference;
import org.eclipse.xtext.common.types.JvmEnumerationLiteral;
import org.eclipse.xtext.common.types.JvmEnumerationType;
import org.eclipse.xtext.common.types.JvmField;
import org.eclipse.xtext.common.types.JvmFormalParameter;
import org.eclipse.xtext.common.types.JvmGenericType;
import org.eclipse.xtext.common.types.JvmMember;
import org.eclipse.xtext.common.types.JvmOperation;
import org.eclipse.xtext.common.types.JvmParameterizedTypeReference;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.JvmVisibility;
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable;
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer;
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor;
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
/**
* <p>Infers a JVM model from the source model.</p>
*
* <p>The JVM model should contain all elements that would appear in the Java code
* which is generated from the source model. Other models link against the JVM model rather than the source model.</p>
*/
@SuppressWarnings("all")
public class FunctionLibraryDslJvmModelInferrer extends AbstractModelInferrer {
protected static final class MethodParamHelper {
private String name;
private JvmTypeReference reference;
public MethodParamHelper(final String aname, final JvmTypeReference areference) {
this.name = aname;
this.reference = areference;
}
}
public final static String FUNCTION_LIBRARY_PACKAGE = "FunctionLibraryPackage";
/**
* generate the class name for a mock entity
*/
public static String getFunctionLibraryPackageName(final FunctionLibraryPackage pckg) {
StringConcatenation _builder = new StringConcatenation();
String _name = pckg.getName();
_builder.append(_name);
String pckgName = _builder.toString();
return pckgName;
}
/**
* convenience API to build and initialize JVM types and their members.
*/
@Inject
@Extension
private JvmTypesBuilder _jvmTypesBuilder;
@Inject
@Extension
private CommonUtils _commonUtils;
protected void _infer(final FunctionLibraryPackage pkg, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
try {
StringConcatenation _builder = new StringConcatenation();
String _functionLibraryPackageName = FunctionLibraryDslJvmModelInferrer.getFunctionLibraryPackageName(pkg);
_builder.append(_functionLibraryPackageName);
_builder.append(".");
_builder.append(FunctionLibraryDslJvmModelInferrer.FUNCTION_LIBRARY_PACKAGE);
final String generatorName = _builder.toString();
final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
JvmAnnotationReference annotationRef = this._annotationTypesBuilder.annotationRef(Component.class);
JvmTypeReference _typeRef = this._typeReferenceBuilder.typeRef(IFunctionLibraryPackage.class);
this._commonUtils.addAnnAttr(annotationRef, pkg, "service", ((JvmTypeReference) _typeRef));
EList<JvmAnnotationReference> _annotations = it.getAnnotations();
this._jvmTypesBuilder.<JvmAnnotationReference>operator_add(_annotations, annotationRef);
EList<JvmTypeReference> _superTypes = it.getSuperTypes();
JvmTypeReference _typeRef_1 = this._typeReferenceBuilder.typeRef(IFunctionLibraryPackage.class);
this._jvmTypesBuilder.<JvmTypeReference>operator_add(_superTypes, _typeRef_1);
it.setFinal(true);
};
acceptor.<JvmGenericType>accept(this._jvmTypesBuilder.toClass(pkg, generatorName), _function);
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception)_t;
e.printStackTrace();
} else {
throw Exceptions.sneakyThrow(_t);
}
}
final Consumer<FunctionLibraryRating> _function_1 = (FunctionLibraryRating rating) -> {
try {
final Procedure1<JvmEnumerationType> _function_2 = (JvmEnumerationType it) -> {
};
final Procedure1<JvmEnumerationType> _function_3 = (JvmEnumerationType it) -> {
this._jvmTypesBuilder.setDocumentation(it, this._jvmTypesBuilder.getDocumentation(rating));
final Consumer<FunctionLibraryRatingItem> _function_4 = (FunctionLibraryRatingItem item) -> {
EList<JvmMember> _members = it.getMembers();
final Procedure1<JvmEnumerationLiteral> _function_5 = (JvmEnumerationLiteral it_1) -> {
this._jvmTypesBuilder.setDocumentation(it_1, this._jvmTypesBuilder.getDocumentation(item));
};
JvmEnumerationLiteral _enumerationLiteral = this._jvmTypesBuilder.toEnumerationLiteral(pkg, item.getName(), _function_5);
this._jvmTypesBuilder.<JvmEnumerationLiteral>operator_add(_members, _enumerationLiteral);
};
rating.getItems().forEach(_function_4);
};
acceptor.<JvmEnumerationType>accept(this._jvmTypesBuilder.toEnumerationType(rating, pkg.getName().concat(".").concat(rating.getName()), _function_2), _function_3);
} catch (final Throwable _t_1) {
if (_t_1 instanceof Exception) {
final Exception e_1 = (Exception)_t_1;
e_1.printStackTrace();
} else {
throw Exceptions.sneakyThrow(_t_1);
}
}
};
pkg.getRatings().forEach(_function_1);
final Consumer<FunctionLibraryGroup> _function_2 = (FunctionLibraryGroup group) -> {
try {
final Procedure1<JvmGenericType> _function_3 = (JvmGenericType it) -> {
EList<JvmTypeReference> _superTypes = it.getSuperTypes();
JvmTypeReference _typeRef = this._typeReferenceBuilder.typeRef(IFunctionLibraryGroup.class);
this._jvmTypesBuilder.<JvmTypeReference>operator_add(_superTypes, _typeRef);
if ((group instanceof FunctionLibraryBlipGroup)) {
EList<JvmTypeReference> _superTypes_1 = it.getSuperTypes();
JvmTypeReference _typeRef_1 = this._typeReferenceBuilder.typeRef(BlipBaseFunctionGroup.class);
this._jvmTypesBuilder.<JvmTypeReference>operator_add(_superTypes_1, _typeRef_1);
}
this._jvmTypesBuilder.setDocumentation(it, this._jvmTypesBuilder.getDocumentation(group));
this._jvmTypesBuilder.setFileHeader(it, this._jvmTypesBuilder.getDocumentation(pkg));
this.toFields(it, group);
it.setFinal(true);
final Consumer<FunctionLibraryFunction> _function_4 = (FunctionLibraryFunction function) -> {
EList<JvmMember> _members = it.getMembers();
JvmTypeReference _elvis = null;
JvmParameterizedTypeReference _oftype = function.getOftype();
if (_oftype != null) {
_elvis = _oftype;
} else {
JvmTypeReference _inferredType = this._jvmTypesBuilder.inferredType();
_elvis = _inferredType;
}
JvmOperation _method = this.toMethod(function, _elvis);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
};
group.getFunctions().forEach(_function_4);
final Consumer<FunctionLibraryOperation> _function_5 = (FunctionLibraryOperation operation) -> {
EList<JvmMember> _members = it.getMembers();
JvmOperation _method = this.toMethod(operation, this._typeReferenceBuilder.typeRef(boolean.class));
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
};
group.getOperations().forEach(_function_5);
final Consumer<FunctionLibraryRate> _function_6 = (FunctionLibraryRate rate) -> {
EList<JvmMember> _members = it.getMembers();
JvmOperation _method = this.toMethod(rate, this._typeReferenceBuilder.typeRef(pkg.getName().concat(".").concat(rate.getOftype().getName())));
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
};
group.getRates().forEach(_function_6);
final Consumer<FunctionLibraryTest> _function_7 = (FunctionLibraryTest test) -> {
EList<JvmMember> _members = it.getMembers();
JvmOperation _method = this.toMethod(test, this._typeReferenceBuilder.typeRef(boolean.class));
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
};
group.getTests().forEach(_function_7);
final Consumer<FunctionLibraryGuard> _function_8 = (FunctionLibraryGuard guard) -> {
EList<JvmMember> _members = it.getMembers();
JvmOperation _method = this.toMethod(guard, this._typeReferenceBuilder.typeRef(boolean.class));
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
};
group.getGuards().forEach(_function_8);
final Consumer<FunctionLibraryCanExecute> _function_9 = (FunctionLibraryCanExecute can) -> {
EList<JvmMember> _members = it.getMembers();
JvmOperation _method = this.toMethod(can, this._typeReferenceBuilder.typeRef(Boolean.class));
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
};
group.getCanExecutes().forEach(_function_9);
final Consumer<FunctionLibraryExecute> _function_10 = (FunctionLibraryExecute execute) -> {
EList<JvmMember> _members = it.getMembers();
JvmOperation _method = this.toMethod(execute, this._typeReferenceBuilder.typeRef(Boolean.class));
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
};
group.getExecutes().forEach(_function_10);
if ((group instanceof FunctionLibraryConverterGroup)) {
FunctionLibraryConverterGroup converterGroup = ((FunctionLibraryConverterGroup) group);
FunctionConvertToModel _convertToModel = converterGroup.getConvertToModel();
boolean _notEquals = (!Objects.equal(_convertToModel, null));
if (_notEquals) {
BasicEList<FunctionLibraryDslJvmModelInferrer.MethodParamHelper> mustParams = new BasicEList<FunctionLibraryDslJvmModelInferrer.MethodParamHelper>();
JvmTypeReference _jvmTypeReference = converterGroup.getConvertToModelDatatype().getJvmTypeReference();
FunctionLibraryDslJvmModelInferrer.MethodParamHelper _methodParamHelper = new FunctionLibraryDslJvmModelInferrer.MethodParamHelper("presentationValue", _jvmTypeReference);
mustParams.add(_methodParamHelper);
JvmTypeReference _typeRef_2 = this._typeReferenceBuilder.typeRef(Locale.class);
FunctionLibraryDslJvmModelInferrer.MethodParamHelper _methodParamHelper_1 = new FunctionLibraryDslJvmModelInferrer.MethodParamHelper("presentationLocale", _typeRef_2);
mustParams.add(_methodParamHelper_1);
JvmTypeReference _addArrayTypeDimension = this._jvmTypesBuilder.addArrayTypeDimension(this._typeReferenceBuilder.typeRef(Object.class));
FunctionLibraryDslJvmModelInferrer.MethodParamHelper _methodParamHelper_2 = new FunctionLibraryDslJvmModelInferrer.MethodParamHelper("presentationParams", _addArrayTypeDimension);
mustParams.add(_methodParamHelper_2);
EList<JvmMember> _members = it.getMembers();
JvmOperation _method = this.toMethod(converterGroup.getConvertToModel(), "valueToModelConverter", converterGroup.getConvertToModelDatatype().getJvmTypeReference(), mustParams);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
}
FunctionConvertToPresentation _convertToPresentation = converterGroup.getConvertToPresentation();
boolean _notEquals_1 = (!Objects.equal(_convertToPresentation, null));
if (_notEquals_1) {
BasicEList<FunctionLibraryDslJvmModelInferrer.MethodParamHelper> mustParams_1 = new BasicEList<FunctionLibraryDslJvmModelInferrer.MethodParamHelper>();
JvmTypeReference _jvmTypeReference_1 = converterGroup.getConvertToModelDatatype().getJvmTypeReference();
FunctionLibraryDslJvmModelInferrer.MethodParamHelper _methodParamHelper_3 = new FunctionLibraryDslJvmModelInferrer.MethodParamHelper("modelValue", _jvmTypeReference_1);
mustParams_1.add(_methodParamHelper_3);
JvmTypeReference _typeRef_3 = this._typeReferenceBuilder.typeRef(Locale.class);
FunctionLibraryDslJvmModelInferrer.MethodParamHelper _methodParamHelper_4 = new FunctionLibraryDslJvmModelInferrer.MethodParamHelper("modelLocale", _typeRef_3);
mustParams_1.add(_methodParamHelper_4);
JvmTypeReference _addArrayTypeDimension_1 = this._jvmTypesBuilder.addArrayTypeDimension(this._typeReferenceBuilder.typeRef(Object.class));
FunctionLibraryDslJvmModelInferrer.MethodParamHelper _methodParamHelper_5 = new FunctionLibraryDslJvmModelInferrer.MethodParamHelper("modelParams", _addArrayTypeDimension_1);
mustParams_1.add(_methodParamHelper_5);
EList<JvmMember> _members_1 = it.getMembers();
JvmOperation _method_1 = this.toMethod(converterGroup.getConvertToPresentation(), "valueToPresentationConverter", converterGroup.getConvertToPresentationDatatype().getJvmTypeReference(), mustParams_1);
this._jvmTypesBuilder.<JvmOperation>operator_add(_members_1, _method_1);
}
}
};
acceptor.<JvmGenericType>accept(this._jvmTypesBuilder.toClass(pkg, pkg.getName().concat(".").concat(group.getName())), _function_3);
} catch (final Throwable _t_1) {
if (_t_1 instanceof Exception) {
final Exception e_1 = (Exception)_t_1;
e_1.printStackTrace();
} else {
throw Exceptions.sneakyThrow(_t_1);
}
}
};
pkg.getGroups().forEach(_function_2);
}
public JvmOperation toMethod(final FunctionLibraryBaseFunction baseFunction, final JvmTypeReference returnType) {
return this.toMethod(baseFunction, baseFunction.getName(), returnType, null);
}
public JvmOperation toMethod(final FunctionLibraryBaseFunction baseFunction, final String methodName, final JvmTypeReference returnType, final EList<FunctionLibraryDslJvmModelInferrer.MethodParamHelper> mustParams) {
final Procedure1<JvmOperation> _function = (JvmOperation it) -> {
this._jvmTypesBuilder.setDocumentation(it, this._jvmTypesBuilder.getDocumentation(baseFunction));
boolean _notEquals = (!Objects.equal(mustParams, null));
if (_notEquals) {
for (final FunctionLibraryDslJvmModelInferrer.MethodParamHelper param : mustParams) {
EList<JvmFormalParameter> _parameters = it.getParameters();
JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(baseFunction, param.name, param.reference);
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
}
}
EList<FunctionLibraryParameter> _params = baseFunction.getParams();
for (final FunctionLibraryParameter param_1 : _params) {
EList<JvmFormalParameter> _parameters_1 = it.getParameters();
JvmFormalParameter _parameter_1 = this._jvmTypesBuilder.toParameter(param_1, param_1.getName(), param_1.getParameterType());
this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters_1, _parameter_1);
}
it.setStatic(true);
it.setFinal(true);
it.setVisibility(JvmVisibility.PUBLIC);
this._jvmTypesBuilder.setBody(it, baseFunction.getBody());
};
return this._jvmTypesBuilder.toMethod(baseFunction, methodName, returnType, _function);
}
public void toFields(final JvmGenericType type, final FunctionLibraryGroup group) {
JvmField field = null;
final Procedure1<JvmField> _function = (JvmField it) -> {
final Procedure1<ITreeAppendable> _function_1 = (ITreeAppendable it_1) -> {
StringConcatenation _builder = new StringConcatenation();
_builder.append("org.slf4j.LoggerFactory.getLogger(\"functiongroup\")");
it_1.append(_builder);
};
this._jvmTypesBuilder.setInitializer(it, _function_1);
};
field = this._jvmTypesBuilder.toField(group, "log", this._typeReferenceBuilder.typeRef(Logger.class), _function);
field.setStatic(true);
field.setVisibility(JvmVisibility.PRIVATE);
EList<JvmMember> _members = type.getMembers();
this._jvmTypesBuilder.<JvmField>operator_add(_members, field);
}
public void infer(final EObject pkg, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
if (pkg instanceof FunctionLibraryPackage) {
_infer((FunctionLibraryPackage)pkg, acceptor, isPreIndexingPhase);
return;
} else if (pkg != null) {
_infer(pkg, acceptor, isPreIndexingPhase);
return;
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(pkg, acceptor, isPreIndexingPhase).toString());
}
}
}