Codegeneration
    Goodies
   Moritz.Eysholdt@itemis.de



 These slides have been presented at the EclipseCon 2011 conference in Santa Clara:
 http://www.eclipsecon.org/2011/sessions/?page=sessions&id=2253

 They have been modified for slideshare.net to contain additional explanatory comments.

 The source code of the demos is available at
 http://github.com/meysholdt/codegeneration-goodies-demos
Generate Code...
 ...customize?
We can edit the generated source code!
We can edit the generated source code!
             actually, NO.
We can edit the generated source code!
                      actually, NO.




• complicated to merge generated/handwritten code
We can edit the generated source code!
                       actually, NO.




• complicated to merge generated/handwritten code
• handwritten code “gets lost” within generated code
We can edit the generated source code!
                       actually, NO.




• complicated to merge generated/handwritten code
• handwritten code “gets lost” within generated code
• requires to put generated source code under version control
We can edit the generated source code!
                       actually, NO.




• complicated to merge generated/handwritten code
• handwritten code “gets lost” within generated code
• requires to put generated source code under version control
• generated version of source code is lost once modified
JMerge
JMerge



• Unneeded files are not removed
JMerge



• Unneeded files are not removed
• easy to forget to remove @Generated
JMerge



• Unneeded files are not removed
• easy to forget to remove @Generated
• Handwritten, but orphaned code can
  easily be forgotten
JMerge



• Unneeded files are not removed
• easy to forget to remove @Generated
• Handwritten, but orphaned code can
  easily be forgotten

• Import statements are not organized
JMerge



• Unneeded files are not removed
• easy to forget to remove @Generated
• Handwritten, but orphaned code can
  easily be forgotten

• Import statements are not organized
• etc.
Solution: Generation Gap
Solution: Generation Gap



•   inheritance + Java’s @Override instead of @Generated
Solution: Generation Gap



•   inheritance + Java’s @Override instead of @Generated

•   two folders: “src”and “src-gen”
Solution: Generation Gap



•   inheritance + Java’s @Override instead of @Generated

•   two folders: “src”and “src-gen”

•   use MWE’s EcoreGenerator instead of JMerge
This is the
famous library-
example that
ships with EMF.
- interfaces
                  - impl-classes
                  - util
                  (all generated)



This is the
famous library-
example that
ships with EMF.
- interfaces
                  - impl-classes
                  - util
                  (all generated)



This is the       - Ecore Model
famous library-
example that      - Genmodel
ships with EMF.
- interfaces
                  - impl-classes
                  - util
                  (all generated)



This is the       - Ecore Model
famous library-
example that      - Genmodel
ships with EMF.
- interfaces
                  - impl-classes
                  - util
                  (all generated)



This is the        - Ecore Model
famous library-
example that       - Genmodel
ships with EMF.




                  Impl implements
                     interface
unmodified library-   library-example with
example from EMF     Generation Gap
unmodified library-   library-example with
example from EMF     Generation Gap
unmodified library-   library-example with
example from EMF     Generation Gap
unmodified library-   library-example with
example from EMF     Generation Gap
unmodified library-   library-example with
example from EMF     Generation Gap
unmodified library-   library-example with
example from EMF     Generation Gap
unmodified library-   library-example with
example from EMF     Generation Gap
Form the “Library”-EClass, EMF generates a
“Library”-Interface and a “LibraryImpl”-Class.
With the GenerationGap-Pattern, we
additionally have a “LibraryImplCustom”-Class
Form the “Library”-EClass, EMF generates a
“Library”-Interface and a “LibraryImpl”-Class.
With the GenerationGap-Pattern, we
additionally have a “LibraryImplCustom”-Class
Form the “Library”-EClass, EMF generates a
“Library”-Interface and a “LibraryImpl”-Class.
With the GenerationGap-Pattern, we
additionally have a “LibraryImplCustom”-Class
Form the “Library”-EClass, EMF generates a
“Library”-Interface and a “LibraryImpl”-Class.
With the GenerationGap-Pattern, we
additionally have a “LibraryImplCustom”-Class
generated code refers to “custom” classes

as super types


 In the code that is generated by EMF, the
 “Custom”-Classes are automatically used
 instead of the “Impl”-Classes, when
 - another “Impl” class extends them.
 - the EFactory instantiated them.
generated code refers to “custom” classes

as super types


 In the code that is generated by EMF, the
 “Custom”-Classes are automatically used
 instead of the “Impl”-Classes, when
 - another “Impl” class extends them.
 - the EFactory instantiated them.




                                             in the EFactory
JMerge                                generation
                                         gap




               files in VCS
                          results of
                  “git clean -fdX && tree”



         Here we can see which files need to be put under version
         control.

         - on the left-hand side, the unmodified library-example.
         - on the right-hand side, the example with generation
         gap. Please note that here no generated source code is
         being put under version control.
How does this work?


hack’ed EMFGen’s ImportManager


   The EMF-Generator has an ImportManaged that returns a
   SimpleName for each QualifiedName of a Java-Element that is
   used within the generated source code. Now, if there is a
   “Custom”-Class/Interface for a certain generated Java Class/
   Interface, the name of the “Custom”-Class/Interface is
   returned instead.
Generating Code...
   ...without a
Template Language
hey, we can generate this!
hey, we can generate this!


                 yes! I know a good template
                           language.
hey, we can generate this!


                  yes! I know a good template
                            language.


   yeah, let’s do it.
hey, we can generate this!


                    yes! I know a good template
                              language.


   yeah, let’s do it.




                !       not so fast...
code generation == map models to text
code generation == map models to text
code generation == map models to text



Template
code generation == map models to text



Template



             XML Schema
code generation == map models to text



Template
                                Grammar


             XML Schema
code generation == map models to text



Template
                                Grammar


             XML Schema
EMF provides a wizard to convert XML-Schema to Ecore.


                                                                XSD




Ecore
EMF provides a wizard to convert XML-Schema to Ecore.


                                                                XSD




Ecore
EMF provides a wizard to convert XML-Schema to Ecore.


                                                                XSD




Ecore
EMF provides a wizard to convert XML-Schema to Ecore.


                                                                XSD




Ecore
The Package Explorer shows a minimal example that
generates RSS, which is in fact XML. rss-2.0.xsd has
been downloaded from the internet and ecore/
genmodel have been derived from it.

The Java code shows is a complete example of what is
needed to run the code generation.

The Console shows the output of this this example.
The Package Explorer shows a minimal example that
generates RSS, which is in fact XML. rss-2.0.xsd has
been downloaded from the internet and ecore/
genmodel have been derived from it.

The Java code shows is a complete example of what is
needed to run the code generation.

The Console shows the output of this this example.
The Package Explorer shows a minimal example that
generates RSS, which is in fact XML. rss-2.0.xsd has
been downloaded from the internet and ecore/
genmodel have been derived from it.

The Java code shows is a complete example of what is
needed to run the code generation.

The Console shows the output of this this example.
what you get for free if you generate code via...

template       v.s.    XSD
what you get for free if you generate code via...

template       v.s.    XSD
                       • well-formed-ness
                       • xsd-compliance
                       • formatting
                       • escaping
                       • typed Java-model
                       • headers
                       • namespaces
Grammar

Text                                                              Model




       Most people probably know Xtext as a technology that can parse text into models.
       But it also works the other way around - models can be serialized into text!
parse


                      Grammar

Text                                                              Model




       Most people probably know Xtext as a technology that can parse text into models.
       But it also works the other way around - models can be serialized into text!
parse


                      Grammar

Text                                                              Model

             serialize

       Most people probably know Xtext as a technology that can parse text into models.
       But it also works the other way around - models can be serialized into text!
parse


                      Grammar

Text                                                              Model

             serialize

       Most people probably know Xtext as a technology that can parse text into models.
       But it also works the other way around - models can be serialized into text!
This example is a modified version of the arithmetics-
example that ships with Xtext. It has been extended to allow
serialization. The new*-Methods in the java code are
utilities that simplify constructing a model. They delegate to
the generated EFactory.
This example is a modified version of the arithmetics-
example that ships with Xtext. It has been extended to allow
serialization. The new*-Methods in the java code are
utilities that simplify constructing a model. They delegate to
the generated EFactory.
This example is a modified version of the arithmetics-
example that ships with Xtext. It has been extended to allow
serialization. The new*-Methods in the java code are
utilities that simplify constructing a model. They delegate to
the generated EFactory.
what you get for free if you generate code via...


template      v.s.     Grammar
what you get for free if you generate code via...


template      v.s.     Grammar
                       • correct syntax
                       • error messages for
                         invalid models
                       • good separation of
                         concerns (see next slide)
                       • typed Java-model
                       • an Xtext-editor ;)
serialization - separation of concerns
serialization - separation of concerns

model:   Ecore (can be derived from grammar)
serialization - separation of concerns

model:     Ecore (can be derived from grammar)
syntactical structure:   Xtext Grammar
serialization - separation of concerns

model:     Ecore (can be derived from grammar)
syntactical structure:   Xtext Grammar
transient values:   ITransientValueService
serialization - separation of concerns

model:     Ecore (can be derived from grammar)
syntactical structure:   Xtext Grammar
transient values:   ITransientValueService
token syntax:   IValueConverter
serialization - separation of concerns

model:     Ecore (can be derived from grammar)
syntactical structure:   Xtext Grammar
transient values:   ITransientValueService
token syntax:   IValueConverter
formatting:   IFormatter
serialization - separation of concerns

model:     Ecore (can be derived from grammar)
syntactical structure:   Xtext Grammar
transient values:   ITransientValueService
token syntax:   IValueConverter
formatting:   IFormatter
identifiers:   IQualifiedNameProvider,
serialization - separation of concerns

model:     Ecore (can be derived from grammar)
syntactical structure:   Xtext Grammar
transient values:   ITransientValueService
token syntax:   IValueConverter
formatting:   IFormatter
identifiers:   IQualifiedNameProvider,
              IScopeProvider
Xtext’s serializer: status


 Xtext 1.0 (Helios)                Xtext 2.0 (Indigo)
• algorithm: backtracking   • algorithm: object-tree-parser
                            • improves:
                              • customizability
                              • error messages         new
                              • performance
Xtext’s serializer: status


 Xtext 1.0 (Helios)                Xtext 2.0 (Indigo)
• algorithm: backtracking   • algorithm: object-tree-parser
                            • improves:
                              • customizability
                              • error messages         new
                              • performance

                                        complete
                                   re-implementation
Questions?




Moritz.Eysholdt@itemis.de

Codegeneration Goodies

  • 1.
    Codegeneration Goodies Moritz.Eysholdt@itemis.de These slides have been presented at the EclipseCon 2011 conference in Santa Clara: http://www.eclipsecon.org/2011/sessions/?page=sessions&id=2253 They have been modified for slideshare.net to contain additional explanatory comments. The source code of the demos is available at http://github.com/meysholdt/codegeneration-goodies-demos
  • 2.
  • 3.
    We can editthe generated source code!
  • 4.
    We can editthe generated source code! actually, NO.
  • 5.
    We can editthe generated source code! actually, NO. • complicated to merge generated/handwritten code
  • 6.
    We can editthe generated source code! actually, NO. • complicated to merge generated/handwritten code • handwritten code “gets lost” within generated code
  • 7.
    We can editthe generated source code! actually, NO. • complicated to merge generated/handwritten code • handwritten code “gets lost” within generated code • requires to put generated source code under version control
  • 8.
    We can editthe generated source code! actually, NO. • complicated to merge generated/handwritten code • handwritten code “gets lost” within generated code • requires to put generated source code under version control • generated version of source code is lost once modified
  • 9.
  • 10.
  • 11.
    JMerge • Unneeded filesare not removed • easy to forget to remove @Generated
  • 12.
    JMerge • Unneeded filesare not removed • easy to forget to remove @Generated • Handwritten, but orphaned code can easily be forgotten
  • 13.
    JMerge • Unneeded filesare not removed • easy to forget to remove @Generated • Handwritten, but orphaned code can easily be forgotten • Import statements are not organized
  • 14.
    JMerge • Unneeded filesare not removed • easy to forget to remove @Generated • Handwritten, but orphaned code can easily be forgotten • Import statements are not organized • etc.
  • 15.
  • 16.
    Solution: Generation Gap • inheritance + Java’s @Override instead of @Generated
  • 17.
    Solution: Generation Gap • inheritance + Java’s @Override instead of @Generated • two folders: “src”and “src-gen”
  • 18.
    Solution: Generation Gap • inheritance + Java’s @Override instead of @Generated • two folders: “src”and “src-gen” • use MWE’s EcoreGenerator instead of JMerge
  • 19.
    This is the famouslibrary- example that ships with EMF.
  • 20.
    - interfaces - impl-classes - util (all generated) This is the famous library- example that ships with EMF.
  • 21.
    - interfaces - impl-classes - util (all generated) This is the - Ecore Model famous library- example that - Genmodel ships with EMF.
  • 22.
    - interfaces - impl-classes - util (all generated) This is the - Ecore Model famous library- example that - Genmodel ships with EMF.
  • 23.
    - interfaces - impl-classes - util (all generated) This is the - Ecore Model famous library- example that - Genmodel ships with EMF. Impl implements interface
  • 24.
    unmodified library- library-example with example from EMF Generation Gap
  • 25.
    unmodified library- library-example with example from EMF Generation Gap
  • 26.
    unmodified library- library-example with example from EMF Generation Gap
  • 27.
    unmodified library- library-example with example from EMF Generation Gap
  • 28.
    unmodified library- library-example with example from EMF Generation Gap
  • 29.
    unmodified library- library-example with example from EMF Generation Gap
  • 30.
    unmodified library- library-example with example from EMF Generation Gap
  • 31.
    Form the “Library”-EClass,EMF generates a “Library”-Interface and a “LibraryImpl”-Class. With the GenerationGap-Pattern, we additionally have a “LibraryImplCustom”-Class
  • 32.
    Form the “Library”-EClass,EMF generates a “Library”-Interface and a “LibraryImpl”-Class. With the GenerationGap-Pattern, we additionally have a “LibraryImplCustom”-Class
  • 33.
    Form the “Library”-EClass,EMF generates a “Library”-Interface and a “LibraryImpl”-Class. With the GenerationGap-Pattern, we additionally have a “LibraryImplCustom”-Class
  • 34.
    Form the “Library”-EClass,EMF generates a “Library”-Interface and a “LibraryImpl”-Class. With the GenerationGap-Pattern, we additionally have a “LibraryImplCustom”-Class
  • 35.
    generated code refersto “custom” classes as super types In the code that is generated by EMF, the “Custom”-Classes are automatically used instead of the “Impl”-Classes, when - another “Impl” class extends them. - the EFactory instantiated them.
  • 36.
    generated code refersto “custom” classes as super types In the code that is generated by EMF, the “Custom”-Classes are automatically used instead of the “Impl”-Classes, when - another “Impl” class extends them. - the EFactory instantiated them. in the EFactory
  • 37.
    JMerge generation gap files in VCS results of “git clean -fdX && tree” Here we can see which files need to be put under version control. - on the left-hand side, the unmodified library-example. - on the right-hand side, the example with generation gap. Please note that here no generated source code is being put under version control.
  • 38.
    How does thiswork? hack’ed EMFGen’s ImportManager The EMF-Generator has an ImportManaged that returns a SimpleName for each QualifiedName of a Java-Element that is used within the generated source code. Now, if there is a “Custom”-Class/Interface for a certain generated Java Class/ Interface, the name of the “Custom”-Class/Interface is returned instead.
  • 39.
    Generating Code... ...without a Template Language
  • 40.
    hey, we cangenerate this!
  • 41.
    hey, we cangenerate this! yes! I know a good template language.
  • 42.
    hey, we cangenerate this! yes! I know a good template language. yeah, let’s do it.
  • 43.
    hey, we cangenerate this! yes! I know a good template language. yeah, let’s do it. ! not so fast...
  • 44.
    code generation ==map models to text
  • 45.
    code generation ==map models to text
  • 46.
    code generation ==map models to text Template
  • 47.
    code generation ==map models to text Template XML Schema
  • 48.
    code generation ==map models to text Template Grammar XML Schema
  • 49.
    code generation ==map models to text Template Grammar XML Schema
  • 50.
    EMF provides awizard to convert XML-Schema to Ecore. XSD Ecore
  • 51.
    EMF provides awizard to convert XML-Schema to Ecore. XSD Ecore
  • 52.
    EMF provides awizard to convert XML-Schema to Ecore. XSD Ecore
  • 53.
    EMF provides awizard to convert XML-Schema to Ecore. XSD Ecore
  • 54.
    The Package Explorershows a minimal example that generates RSS, which is in fact XML. rss-2.0.xsd has been downloaded from the internet and ecore/ genmodel have been derived from it. The Java code shows is a complete example of what is needed to run the code generation. The Console shows the output of this this example.
  • 55.
    The Package Explorershows a minimal example that generates RSS, which is in fact XML. rss-2.0.xsd has been downloaded from the internet and ecore/ genmodel have been derived from it. The Java code shows is a complete example of what is needed to run the code generation. The Console shows the output of this this example.
  • 56.
    The Package Explorershows a minimal example that generates RSS, which is in fact XML. rss-2.0.xsd has been downloaded from the internet and ecore/ genmodel have been derived from it. The Java code shows is a complete example of what is needed to run the code generation. The Console shows the output of this this example.
  • 57.
    what you getfor free if you generate code via... template v.s. XSD
  • 58.
    what you getfor free if you generate code via... template v.s. XSD • well-formed-ness • xsd-compliance • formatting • escaping • typed Java-model • headers • namespaces
  • 59.
    Grammar Text Model Most people probably know Xtext as a technology that can parse text into models. But it also works the other way around - models can be serialized into text!
  • 60.
    parse Grammar Text Model Most people probably know Xtext as a technology that can parse text into models. But it also works the other way around - models can be serialized into text!
  • 61.
    parse Grammar Text Model serialize Most people probably know Xtext as a technology that can parse text into models. But it also works the other way around - models can be serialized into text!
  • 62.
    parse Grammar Text Model serialize Most people probably know Xtext as a technology that can parse text into models. But it also works the other way around - models can be serialized into text!
  • 63.
    This example isa modified version of the arithmetics- example that ships with Xtext. It has been extended to allow serialization. The new*-Methods in the java code are utilities that simplify constructing a model. They delegate to the generated EFactory.
  • 64.
    This example isa modified version of the arithmetics- example that ships with Xtext. It has been extended to allow serialization. The new*-Methods in the java code are utilities that simplify constructing a model. They delegate to the generated EFactory.
  • 65.
    This example isa modified version of the arithmetics- example that ships with Xtext. It has been extended to allow serialization. The new*-Methods in the java code are utilities that simplify constructing a model. They delegate to the generated EFactory.
  • 66.
    what you getfor free if you generate code via... template v.s. Grammar
  • 67.
    what you getfor free if you generate code via... template v.s. Grammar • correct syntax • error messages for invalid models • good separation of concerns (see next slide) • typed Java-model • an Xtext-editor ;)
  • 68.
  • 69.
    serialization - separationof concerns model: Ecore (can be derived from grammar)
  • 70.
    serialization - separationof concerns model: Ecore (can be derived from grammar) syntactical structure: Xtext Grammar
  • 71.
    serialization - separationof concerns model: Ecore (can be derived from grammar) syntactical structure: Xtext Grammar transient values: ITransientValueService
  • 72.
    serialization - separationof concerns model: Ecore (can be derived from grammar) syntactical structure: Xtext Grammar transient values: ITransientValueService token syntax: IValueConverter
  • 73.
    serialization - separationof concerns model: Ecore (can be derived from grammar) syntactical structure: Xtext Grammar transient values: ITransientValueService token syntax: IValueConverter formatting: IFormatter
  • 74.
    serialization - separationof concerns model: Ecore (can be derived from grammar) syntactical structure: Xtext Grammar transient values: ITransientValueService token syntax: IValueConverter formatting: IFormatter identifiers: IQualifiedNameProvider,
  • 75.
    serialization - separationof concerns model: Ecore (can be derived from grammar) syntactical structure: Xtext Grammar transient values: ITransientValueService token syntax: IValueConverter formatting: IFormatter identifiers: IQualifiedNameProvider, IScopeProvider
  • 76.
    Xtext’s serializer: status Xtext 1.0 (Helios) Xtext 2.0 (Indigo) • algorithm: backtracking • algorithm: object-tree-parser • improves: • customizability • error messages new • performance
  • 77.
    Xtext’s serializer: status Xtext 1.0 (Helios) Xtext 2.0 (Indigo) • algorithm: backtracking • algorithm: object-tree-parser • improves: • customizability • error messages new • performance complete re-implementation
  • 78.