Skip to content

Commit

Permalink
Key improvements of the generator with:
Browse files Browse the repository at this point in the history
- better import support.
- better respect of the naming convention of Scala.
- correction of some invalid code generated.
- better default values for fields and operations return value.
- new properties files to improve the support of EMF datatypes.
  • Loading branch information
sbegaudeau committed Feb 15, 2012
1 parent bdbc8eb commit d56a4ff
Show file tree
Hide file tree
Showing 39 changed files with 1,467 additions and 1,198 deletions.
Expand Up @@ -20,5 +20,6 @@ Require-Bundle: org.eclipse.core.runtime,
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Bundle-ActivationPolicy: lazy
Eclipse-LazyStart: true
Export-Package: org.obeonetwork.pim.ecore.gen.scala.main
Export-Package: org.obeonetwork.pim.ecore.gen.scala.main,
org.obeonetwork.pim.ecore.gen.scala.services
Bundle-Localization: plugin
Expand Up @@ -22,6 +22,12 @@ public class EcoreGenScalaPlugin extends Plugin {
* The plug-in ID.
*/
public static final String PLUGIN_ID = "org.obeonetwork.pim.ecore.gen.scala";

public static final String PLATFORM_PLUGIN = "platform:/plugin/";

public static final String TYPES_PROPERTIES = PLATFORM_PLUGIN + PLUGIN_ID + "/org/obeonetwork/pim/ecore/gen/scala/properties/types.properties";

public static final String DEFAULT_VALUES_PROPERTIES = PLATFORM_PLUGIN + PLUGIN_ID + "/org/obeonetwork/pim/ecore/gen/scala/properties/defaultvalues.properties";

/**
* The shared instance.
Expand Down
Expand Up @@ -3,10 +3,13 @@

[import org::obeonetwork::pim::ecore::gen::scala::common::name /]
[import org::obeonetwork::pim::ecore::gen::scala::common::type /]
[import org::obeonetwork::pim::ecore::gen::scala::services::ecoreGenScalaUtil /]

[template public genScaladocHeader(aGenClass : GenClass)]
/**
* [aGenClass.genPackage.genModel.copyrightText.substituteAll('\r\n', '\r\n * ')/]
[for (line : String | aGenClass.genPackage.genModel.copyrightText.formatCopyright())]
* [line/]
[/for]
*/
[/template]

Expand Down
@@ -1,13 +1,43 @@
[comment encoding = UTF-8 /]
[module imports('http://www.eclipse.org/emf/2002/GenModel', 'http://www.eclipse.org/emf/2002/Ecore')]

[import org::obeonetwork::pim::ecore::gen::scala::common::type /]
[import org::obeonetwork::pim::ecore::gen::scala::services::ecoreGenScalaUtil /]

[template public genImports(aGenClass : GenClass) post(trim())]
[aGenClass.mandatoryImports()/]

// [protected ('for imports')]
// [/protected]
[/template]

[template private mandatoryImports(aGenClass : GenClass)]
[template private mandatoryImports(aGenClass : GenClass) post(trim())]
[for (aString : String | aGenClass.importValue()->asSequence()->formatImport())]
import [aString/];
[/for]
[/template]

[query private importValue(aGenClass : GenClass) : OrderedSet(String) =
aGenClass.fieldsType()
->union(aGenClass.operationsType())->asOrderedSet()
->reject(anETypedElement : ETypedElement | anETypedElement.eType.genPackage().trim().size() = 0)
.importQualifiedName()->asOrderedSet()
/]

[template public importQualifiedName(anETypedElement : ETypedElement)]
[anETypedElement.eType.genQualifiedTypeName()/]
[/template]


[query private fieldsType(aGenClass : GenClass) : OrderedSet(ETypedElement) =
aGenClass.ecoreClass.eStructuralFeatures->asOrderedSet()
/]

[/template]
[query private operationsType(aGenClass : GenClass) : OrderedSet(ETypedElement) =
let operations : OrderedSet(EOperation) = aGenClass.ecoreClass.eOperations in
if (operations->size() > 0) then
operations->filter(ETypedElement)->union(operations.eParameters->filter(ETypedElement)->asOrderedSet())->asOrderedSet()
else
operations.eParameters->asOrderedSet()
endif
/]
Expand Up @@ -10,7 +10,7 @@
[/template]

[template public genFieldName(anEStructuralFeature : EStructuralFeature)]
[anEStructuralFeature.name.toLowerFirst()/]
_[anEStructuralFeature.name.toLowerFirst()/]
[/template]

[template public genOperationName(anEOperation : EOperation)]
Expand All @@ -30,5 +30,5 @@ new
[/template]

[template public genMutatorName(anEStructuralFeature : EStructuralFeature)]
set[anEStructuralFeature.name.toLowerFirst().toUpperFirst()/]
[anEStructuralFeature.name.toLowerFirst().toLowerFirst()/]_
[/template]
Expand Up @@ -29,6 +29,22 @@
Impl
[/template]

[template public genQualifiedTypeName(anEClassifier : EClassifier) post(trim())]
[if (not anEClassifier.instanceTypeName.oclIsUndefined())]
[anEClassifier.instanceTypeName.resolve()/]
[else]
[anEClassifier.name.toUpperFirst().resolve()/]
[/if]
[/template]

[template public genPackage(anEClassifier : EClassifier) post(trim())]
[if (anEClassifier.genQualifiedTypeName().contains('.'))]
[if (getProperty('__package-convertion__' + anEClassifier.genQualifiedTypeName().substring(1, anEClassifier.genQualifiedTypeName().lastIndex('.') - 1)).size() > 0)]
[getProperty('__package-convertion__' + anEClassifier.genQualifiedTypeName().substring(1, anEClassifier.genQualifiedTypeName().lastIndex('.') - 1))/]
[/if]
[/if]
[/template]

[template public genTypeName(anEClass : EClass) post(trim())]
[if (not anEClass.instanceTypeName.oclIsUndefined())]
[if (anEClass.instanceTypeName.contains('.'))]
Expand All @@ -43,13 +59,21 @@ Impl

[template public genTypeName(anEClassifier : EClassifier) post(trim())]
[if (not anEClassifier.instanceTypeName.oclIsUndefined())]
[if (anEClassifier.instanceTypeName.contains('.'))]
[anEClassifier.instanceTypeName.substring(anEClassifier.instanceTypeName.lastIndex('.') + 1)/]
[anEClassifier.instanceTypeName.resolve().nameFromQualifiedName()/]
[else]
[anEClassifier.instanceTypeName/]
[anEClassifier.name.toUpperFirst().resolve().nameFromQualifiedName()/]
[/if]
[/template]

[template private resolve(aString : String) post(trim())]
[getProperty('__type-convertion__' + aString)/]
[/template]

[template private nameFromQualifiedName(aString : String) post(trim())]
[if (aString.contains('.'))]
[aString.substring(aString.lastIndex('.') + 1)/]
[else]
[anEClassifier.name.toUpperFirst()/]
[aString/]
[/if]
[/template]

Expand Down Expand Up @@ -85,6 +109,54 @@ Impl
[/if]
[/if]
[else]
[anETypedElement.eType.genTypeName()/]
[anETypedElement.eType.genTypeName()/]
[/if]
[/template]

[template public genDefaultValue(anETypedElement : ETypedElement) post(trim())]
[if (anETypedElement.upperBound > 1)]
[if (anETypedElement.unique)]
[if (anETypedElement.ordered)]
[if (anETypedElement.lowerBound > 1)]
None
[else]
HashSet['['/][anETypedElement.eType.genTypeName()/][']'/]()
[/if]
[else]
[if (anETypedElement.lowerBound > 1)]
None
[else]
HashSet['['/][anETypedElement.eType.genTypeName()/][']'/]()
[/if]
[/if]
[else]
[if (anETypedElement.ordered)]
[if (anETypedElement.lowerBound > 1)]
None
[else]
List['['/][anETypedElement.eType.genTypeName()/][']'/]()
[/if]
[else]
[if (anETypedElement.lowerBound > 1)]
None
[else]
List['['/][anETypedElement.eType.genTypeName()/][']'/]()
[/if]
[/if]
[/if]
[else]
[anETypedElement.eType.genDefaultValue()/]
[/if]
[/template]

[template public genDefaultValue(anEClassifier : EClassifier) post(trim())]
[if (not anEClassifier.instanceTypeName.oclIsUndefined())]
[anEClassifier.instanceTypeName.resolve().defaultValue()/]
[else]
[anEClassifier.name.toUpperFirst().resolve().defaultValue()/]
[/if]
[/template]

[template public defaultValue(aString : String) post(trim())]
[getProperty('__default-value__' + aString)/]
[/template]
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.obeonetwork.pim.ecore.gen.scala.EcoreGenScalaPlugin;

/**
* Entry point of the 'EcoreGenScala' generation module.
Expand Down Expand Up @@ -253,7 +254,7 @@ public String getModuleName() {
*
* @return The list of properties file we need to add to the generation context.
* @see java.util.ResourceBundle#getBundle(String)
* @generated
* @generated NOT
*/
@Override
public List<String> getProperties() {
Expand All @@ -268,6 +269,8 @@ public List<String> getProperties() {
*
* To learn more about Properties Files, have a look at the Acceleo Launcher documentation (Help -> Help Contents).
*/
propertiesFiles.add(EcoreGenScalaPlugin.TYPES_PROPERTIES);
propertiesFiles.add(EcoreGenScalaPlugin.DEFAULT_VALUES_PROPERTIES);
return propertiesFiles;
}

Expand Down
Expand Up @@ -65,12 +65,12 @@ extends [aGenClass.traitSuperTypes()->first().genTypeName()/][for (aTrait : ECla
[template private fields(aGenClass : GenClass)]
[for (anEAttribute : EAttribute | aGenClass.ecoreClass.eAttributes->reject(anEAttr: EAttribute | anEAttr.volatile))]
[anEAttribute.genImplementationScalaDoc()/]
[anEAttribute.visibility()/][anEAttribute.override()/]var [anEAttribute.genFieldName()/]: [anEAttribute.genTypeName()/];
[anEAttribute.visibility()/][anEAttribute.override()/]var [anEAttribute.genFieldName()/]: [anEAttribute.genTypeName()/] = [anEAttribute.genDefaultValue()/];

[/for]
[for (anEReference : EReference | aGenClass.ecoreClass.eReferences->reject(anERef: EReference | anERef.volatile))]
[anEReference.genImplementationScalaDoc()/]
[anEReference.visibility()/][anEReference.override()/]var [anEReference.genFieldName()/]: [anEReference.genTypeName()/];
[anEReference.visibility()/][anEReference.override()/]var [anEReference.genFieldName()/]: [anEReference.genTypeName()/] = [anEReference.genDefaultValue()/];

[/for]
[/template]
Expand Down Expand Up @@ -123,7 +123,7 @@ private['['/]this[']'/]

[template private defaultBody(anEOperation : EOperation)]
[if (not anEOperation.eType.oclIsUndefined())]
return null;
return [anEOperation.genDefaultValue()/];
[/if]
[/template]

Expand All @@ -134,28 +134,28 @@ return null;
[template private accessors(aGenClass : GenClass)]
[for (anEAttribute : EAttribute | aGenClass.ecoreClass.eAttributes)]
[anEAttribute.genImplementationGetterScalaDoc()/]
def [anEAttribute.genAccessorName()/](): [anEAttribute.genTypeName()/] {
return this.[anEAttribute.genFieldName()/];
def [anEAttribute.genAccessorName()/](): [anEAttribute.genTypeName()/] = {
[if (not anEAttribute.volatile)]return this.[anEAttribute.genFieldName()/];[else]return [anEAttribute.genDefaultValue()/];[/if]
}
[if (not anEAttribute.unsettable)]

[anEAttribute.genImplementationSetterScalaDoc()/]
def [anEAttribute.genMutatorName()/]([anEAttribute.genSetterPrefix()/][anEAttribute.genFieldName()/]: [anEAttribute.genTypeName()/]) {
this.[anEAttribute.genFieldName()/] = [anEAttribute.genSetterPrefix()/][anEAttribute.genFieldName()/];
[if (not anEAttribute.volatile)]this.[anEAttribute.genFieldName()/] = [anEAttribute.genSetterPrefix()/][anEAttribute.genFieldName()/];[/if]
}

[/if]
[/for]
[for (anEReference : EReference | aGenClass.ecoreClass.eReferences)]
[anEReference.genImplementationGetterScalaDoc()/]
def [anEReference.genAccessorName()/](): [anEReference.genTypeName()/] {
return this.[anEReference.genFieldName()/];
def [anEReference.genAccessorName()/](): [anEReference.genTypeName()/] = {
[if (not anEReference.volatile)]return this.[anEReference.genFieldName()/];[else]return [anEReference.genDefaultValue()/];[/if]
}
[if (not anEReference.unsettable)]

[anEReference.genImplementationSetterScalaDoc()/]
def [anEReference.genMutatorName()/]([anEReference.genSetterPrefix()/][anEReference.genFieldName()/]: [anEReference.genTypeName()/]) {
this.[anEReference.genFieldName()/] = [anEReference.genSetterPrefix()/][anEReference.genFieldName()/];
[if (not anEReference.volatile)]this.[anEReference.genFieldName()/] = [anEReference.genSetterPrefix()/][anEReference.genFieldName()/];[/if]
}

[/if]
Expand Down
@@ -0,0 +1,43 @@
################################################################################
## Copyright (c) 2012 Obeo.
## 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:
## Obeo - initial API and implementation
################################################################################
__default-value__scala.math.BigDecimal = 0
__default-value__scala.math.BigInt = 0
__default-value__scala.Boolean = false
__default-value__scala.Boolean = false
__default-value__scala.Byte = null
__default-value__Array[Byte] = Array[Byte]()
__default-value__scala.Byte = 0
__default-value__scala.Char = 0
__default-value__java.lang.Character = new Character(0)
__default-value__java.util.Date = null
__default-value__org.eclipse.emf.common.util.DiagnosticChain = null
__default-value__scala.Double = 0.0
__default-value__scala.Double = 0.0
__default-value__scala.collection.mutable.List = List()
__default-value__org.eclipse.emf.common.util.Enumerator = null
__default-value__org.eclipse.emf.ecore.util.FeatureMap = null
__default-value__org.eclipse.emf.ecore.util.FeatureMap$Entry = null
__default-value__scala.Float = 0.0f
__default-value__scala.Float = 0.0f
__default-value__scala.Int = 0
__default-value__java.lang.Integer = 0
__default-value__java.lang.reflect.InvocationTargetException = null
__default-value__java.lang.Class = null
__default-value__java.lang.Object = null
__default-value__scala.Long = 0
__default-value__scala.Long = 0
__default-value__scala.collection.mutable.Map = null
__default-value__org.eclipse.emf.ecore.resource.Resource = null
__default-value__org.eclipse.emf.ecore.resource.ResourceSet = null
__default-value__scala.Short = 0
__default-value__scala.Short = 0
__default-value__java.lang.String = ""
__default-value__org.eclipse.emf.common.util.TreeIterator = null
@@ -0,0 +1,54 @@
################################################################################
## Copyright (c) 2012 Obeo.
## 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:
## Obeo - initial API and implementation
################################################################################
__type-convertion__java.math.BigDecimal = scala.math.BigDecimal
__type-convertion__java.math.BigInteger = scala.math.BigInt
__type-convertion__boolean = scala.Boolean
__type-convertion__java.lang.Boolean = scala.Boolean
__type-convertion__byte = scala.Byte
__type-convertion__byte[] = Array[Byte]
__type-convertion__java.lang.Byte = scala.Byte
__type-convertion__char = scala.Char
__type-convertion__java.lang.Character = java.lang.Character
__type-convertion__java.util.Date = java.util.Date
__type-convertion__org.eclipse.emf.common.util.DiagnosticChain = org.eclipse.emf.common.util.DiagnosticChain
__type-convertion__double = scala.Double
__type-convertion__java.lang.Double = scala.Double
__type-convertion__org.eclipse.emf.common.util.EList = scala.collection.mutable.List
__type-convertion__org.eclipse.emf.common.util.Enumerator = org.eclipse.emf.common.util.Enumerator
__type-convertion__org.eclipse.emf.ecore.util.FeatureMap = org.eclipse.emf.ecore.util.FeatureMap
__type-convertion__org.eclipse.emf.ecore.util.FeatureMap$Entry = org.eclipse.emf.ecore.util.FeatureMap$Entry
__type-convertion__float = scala.Float
__type-convertion__java.lang.Float = scala.Float
__type-convertion__int = scala.Int
__type-convertion__java.lang.Integer = java.lang.Integer
__type-convertion__java.lang.reflect.InvocationTargetException = java.lang.reflect.InvocationTargetException
__type-convertion__java.lang.Class = java.lang.Class
__type-convertion__java.lang.Object = java.lang.Object
__type-convertion__long = scala.Long
__type-convertion__java.lang.Long = scala.Long
__type-convertion__java.util.Map = scala.collection.mutable.Map
__type-convertion__org.eclipse.emf.ecore.resource.Resource = org.eclipse.emf.ecore.resource.Resource
__type-convertion__org.eclipse.emf.ecore.resource.ResourceSet = org.eclipse.emf.ecore.resource.ResourceSet
__type-convertion__short = scala.Short
__type-convertion__java.lang.Short = scala.Short
__type-convertion__java.lang.String = java.lang.String
__type-convertion__org.eclipse.emf.common.util.TreeIterator = org.eclipse.emf.common.util.TreeIterator

__package-convertion__scala =
__package-convertion__java.lang =
__package-convertion__org.eclipse.emf.common.util = org.eclipse.emf.common.util
__package-convertion__org.eclipse.emf.ecore.resource = org.eclipse.emf.ecore.resource
__package-convertion__org.eclipse.emf.ecore.util = org.eclipse.emf.ecore.util
__package-convertion__scala.collection.mutable = scala.collection.mutable
__package-convertion__java.lang.reflect = java.lang.reflect
__package-convertion__java.util = java.util
__package-convertion__scala.math = scala.math

0 comments on commit d56a4ff

Please sign in to comment.