Skip to main content
Jump to: navigation, search

Difference between revisions of "Java Code Generation"

(Incremental code generation)
 
(26 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The Java code generation is available in the extra plugins of Papyrus. It can be used by installing the latest version of Papyrus. It allows to create Java source code from a class diagram.
+
The Java code generation is available via Papyrus SW designer, a Papyrus component. It can be used by installing the latest version of Papyrus. It allows to create Java source code from a class diagram.
  
== Generate a class source from a class diagram. ==
+
== Installation instructions==
Open a class diagram, and choose a UML class by clicking on it.
+
The Java features of Papyrus are available as part of the [http://wiki.eclipse.org/Papyrus_Software_Designer Papyrus Software Designer] extra feature.
Right clicking, and choose Java -> Generate Java code
+
 
A Java class while be created on a project, which has the same name that you model
+
You can install it by using the Papyrus Software Designer update-site:
Note that you can also generate java code source for multiple classes, or generate packages.
+
* http://download.eclipse.org/modeling/mdt/papyrus/components/designer/
Moreover, the generation process also create all needed classes, like associations or fields.
+
Or you can use the nightly build update site (paste following url in eclipse update site):
 +
* https://hudson.eclipse.org/papyrus/job/papyrus-designer-oxygen/lastSuccessfulBuild/artifact/releng/org.eclipse.papyrus.designer.p2/target/repository/
 +
 
 +
Then select "Papyrus Java profile, library and code generation (Incubation)" feature. You need JDT installed (which comes with most Eclipse packages).
 +
 
 +
[[Image:InstallSWDesignerJavaFeature.png]]
 +
 
 +
== Code generation from UML elements ==
 +
 
 +
You can generate code for a specific classifier or package in your UML model. To generate code, do the following:
 +
 
 +
* Right click on classifier or package, either in a diagram or in the model explorer
 +
* Designer > Generate Java code
 +
* Follow the JDT dialogs that let you create a new JDT project in Eclipse, where code will be generated, if there is no JDT project associated with your model (e.g. the first time you generate code from your model)
 +
 
 +
When you generate code from a classifier, its required classifiers are also generated. Required classifiers are classifiers related for the generated classifier, e.g. typing one of its attributes, inheritance relationship, dependency relationship. When you generate code from a package (e.g. the root of your model), all of its classifiers, and their required classifiers, will be generated.
  
 
[[Image:GenerateJavaCode.jpg]]
 
[[Image:GenerateJavaCode.jpg]]
  
== Change generation preferences ==
+
== Generation preferences ==
You can change the generation preferences using the eclipse preferences menu, and choose Papyrus -> Java classes Generation.
+
You can change the Java code generation preferences using the Eclipse preferences menu, and choose Papyrus > Java code generation.
 
Available options are:
 
Available options are:
  
*stop generation on first error (enabled by default),   
+
* Extension for Java files
*implement methods of inheres interfaces and abstract class (enabled by default),  
+
* Prefix for name of created JDT project (if unspecified in the model, see below)
*generate getters & setters (enabled by default)
+
* Header for generated files
*manage packages to not include into generation,   
+
*choose the generic classe to use for association (java.util.List by default). 
+
  
== Change project name for generation ==
+
[[File:JavaCodegenPrefs.png]]
  
=== By Using Stereotype ===
+
== Java profile and library ==
By default, the generator use the name of the model as Java project name for generation. To change the default project name, you can use the profiles system. 
+
 
First, add the java profile to your model:
+
The installed Java feature, of Papyrus Software Designer, comes with a Java profile and a Java library. Furthermore, it also installs a generic code generation profile for meta-information (e.g. project name modeling).
 +
 
 +
=== Java profile ===
 +
The Java profile adds stereotypes to model specific Java elements that otherwise cannot be modeled in UML. It also adds stereotypes for managing elements that you do not want to generate (e.g. <<NoCodeGen>>, <<External>>, <<ExternLibrary>> stereotypes). The stereotypes can be seen as tags to put on UML elements. To apply the Java profile:
 +
 
 +
* Click on your model (in the Model Explorer view)
 +
* In the Properties view, choose the Profile tab
 +
* Click on Apply registered profile
 +
* In the dialog that opens, choose the Java profile
  
*click on your model (on the Model Explorer view),
 
*in the Properties view, choose the Profile tab,
 
*click on Apply registered profile,
 
 
[[File:applyRegisteredProfile.jpg]]
 
[[File:applyRegisteredProfile.jpg]]
*into the opened pop-up, choose Java
+
 
 
[[File:javaRegisteredProfile.jpg]]
 
[[File:javaRegisteredProfile.jpg]]
  
You now can change the default project name by adding a profile into your model
+
=== Java library ===
 +
The Java library provides Java primitives and their wrapper classes. To import, this library:
 +
 
 +
* Right click on your model (in the Model Explorer view)
 +
* Import > Import Registered Package
 +
* Choose the JavaLibrary package
 +
* Then you can use the Java primitives in this package, when you type attributes, parameters, etc...
 +
 
 +
[[File:importPapyrusRegisteredPackage.png]]
 +
 
 +
[[File:javaLibrary.png]]
 +
 
 +
=== Common code generation profile ===
 +
Papyrus Software Designer also offers a common code generation profile, that can be used for Java code generation. This profile lets the user model meta-information, e.g. path where code is generated.
 +
 
 +
To use this feature, first the Papyrus code generation profile must be applied:
 +
 
 +
[[File:codeGenerationProfile.png]]
 +
 
 +
Then you can apply the <<Project>> stereotype to your model (root element). This stereotype lets you
 +
* Generation mode: either batch or incremental
 +
* Name of the JDT project (can be an existing one in the workspace)
 +
* Source folder
 +
* Prefix for all packages
 +
 
 +
[[File:swDesignerProjectStereotype.png]]
 +
 
 +
== Incremental code generation ==
  
*on Properties View, Profile tab, click on Apply stereotypes,
+
Incremental code generation is no longer supported.
*on the opened pop-up choose JavaSrcFolder, and add it into Applied Stereotypes, and close the pop-up by clicking on Ok button.
+
[[File:JavaSrcFolder.jpg]]
+
*you now can change the project name into the JavaSrcFolder stereotype
+
[[File:changeProjectName.jpg]]
+
  
 +
== Developer resources ==
  
=== From Eclipse Preferences ===
+
* Git repo: http://git.eclipse.org/c/papyrus/org.eclipse.papyrus-designer.git
Papyrus provides eclipse preferences to change the default source folder name. This preferences  value is used for all generation, if nothing is provided by a stereotype:
+
* Java plugins path: languages/java/
*Open Eclipse preferences,
+
* Java code generation plugins:
*Search for ''Papyrus'' section,
+
** org.eclipse.papyrus.designer.languages.java.codegen
*Choose ''Java Classes Generation'' sub-section,
+
*** Contains the transformation rules, in the org.eclipse.papyrus.designer.languages.java.codegen.xtend package
*Change ''default source folder name value''
+
** org.eclipse.papyrus.designer.languages.java.codegen.ui
 +
*** Starting point when called from UI: org.eclipse.papyrus.designer.languages.java.codegen.ui.handlers.GenerateCodeHandler.execute(ExecutionEvent)
 +
** org.eclipse.papyrus.designer.languages.java.jdt.project
 +
*** Minor plugin to support JDT project creation (wizard, etc...)
  
[[Image:JavaCodeGenerationEclipsePreferencesDefaultSourceFolderName.png]]
+
[[Category:Modeling]]
 +
[[Category:Papyrus]]
 +
[[Category:Papyrus-Software-Designer]]

Latest revision as of 06:28, 3 July 2021

The Java code generation is available via Papyrus SW designer, a Papyrus component. It can be used by installing the latest version of Papyrus. It allows to create Java source code from a class diagram.

Installation instructions

The Java features of Papyrus are available as part of the Papyrus Software Designer extra feature.

You can install it by using the Papyrus Software Designer update-site:

Or you can use the nightly build update site (paste following url in eclipse update site):

Then select "Papyrus Java profile, library and code generation (Incubation)" feature. You need JDT installed (which comes with most Eclipse packages).

InstallSWDesignerJavaFeature.png

Code generation from UML elements

You can generate code for a specific classifier or package in your UML model. To generate code, do the following:

  • Right click on classifier or package, either in a diagram or in the model explorer
  • Designer > Generate Java code
  • Follow the JDT dialogs that let you create a new JDT project in Eclipse, where code will be generated, if there is no JDT project associated with your model (e.g. the first time you generate code from your model)

When you generate code from a classifier, its required classifiers are also generated. Required classifiers are classifiers related for the generated classifier, e.g. typing one of its attributes, inheritance relationship, dependency relationship. When you generate code from a package (e.g. the root of your model), all of its classifiers, and their required classifiers, will be generated.

GenerateJavaCode.jpg

Generation preferences

You can change the Java code generation preferences using the Eclipse preferences menu, and choose Papyrus > Java code generation. Available options are:

  • Extension for Java files
  • Prefix for name of created JDT project (if unspecified in the model, see below)
  • Header for generated files

JavaCodegenPrefs.png

Java profile and library

The installed Java feature, of Papyrus Software Designer, comes with a Java profile and a Java library. Furthermore, it also installs a generic code generation profile for meta-information (e.g. project name modeling).

Java profile

The Java profile adds stereotypes to model specific Java elements that otherwise cannot be modeled in UML. It also adds stereotypes for managing elements that you do not want to generate (e.g. <<NoCodeGen>>, <<External>>, <<ExternLibrary>> stereotypes). The stereotypes can be seen as tags to put on UML elements. To apply the Java profile:

  • Click on your model (in the Model Explorer view)
  • In the Properties view, choose the Profile tab
  • Click on Apply registered profile
  • In the dialog that opens, choose the Java profile

ApplyRegisteredProfile.jpg

JavaRegisteredProfile.jpg

Java library

The Java library provides Java primitives and their wrapper classes. To import, this library:

  • Right click on your model (in the Model Explorer view)
  • Import > Import Registered Package
  • Choose the JavaLibrary package
  • Then you can use the Java primitives in this package, when you type attributes, parameters, etc...

ImportPapyrusRegisteredPackage.png

JavaLibrary.png

Common code generation profile

Papyrus Software Designer also offers a common code generation profile, that can be used for Java code generation. This profile lets the user model meta-information, e.g. path where code is generated.

To use this feature, first the Papyrus code generation profile must be applied:

CodeGenerationProfile.png

Then you can apply the <<Project>> stereotype to your model (root element). This stereotype lets you

  • Generation mode: either batch or incremental
  • Name of the JDT project (can be an existing one in the workspace)
  • Source folder
  • Prefix for all packages

SwDesignerProjectStereotype.png

Incremental code generation

Incremental code generation is no longer supported.

Developer resources

  • Git repo: http://git.eclipse.org/c/papyrus/org.eclipse.papyrus-designer.git
  • Java plugins path: languages/java/
  • Java code generation plugins:
    • org.eclipse.papyrus.designer.languages.java.codegen
      • Contains the transformation rules, in the org.eclipse.papyrus.designer.languages.java.codegen.xtend package
    • org.eclipse.papyrus.designer.languages.java.codegen.ui
      • Starting point when called from UI: org.eclipse.papyrus.designer.languages.java.codegen.ui.handlers.GenerateCodeHandler.execute(ExecutionEvent)
    • org.eclipse.papyrus.designer.languages.java.jdt.project
      • Minor plugin to support JDT project creation (wizard, etc...)

Back to the top