Bug 372771 - [jvmmodel] Support constructor args on JvmEnumLiterals
Summary: [jvmmodel] Support constructor args on JvmEnumLiterals
Status: NEW
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: 2.2.1   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 421511 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-02-28 12:29 EST by Jan Koehnlein CLA
Modified: 2016-02-29 02:56 EST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Koehnlein CLA 2012-02-28 12:29:08 EST
Currently, there is no way to specify constructor arguments on enum literals in the JvmModel in analogy to this Java example: 

public enum FOO {
  BAR("x"), BAZ("y");  // constructor args

  private String arg;
  private Foo(String arg) {
    this.arg = arg;
  }
}
Comment 1 Thomas Fritsch CLA 2013-07-03 08:26:48 EDT
Here is a work-around.

Create your own JvmModelGenerator class:
  class ExtendedJvmModelGenerator extends JvmModelGenerator {
    @Inject extension JvmTypeExtensions
	
    override generateEumLiteral(JvmEnumerationLiteral it,
          ITreeAppendable appendable, GeneratorConfig config) {
      super.generateEnumLiteral(it, appendable, config)
      compilationStrategy?.apply(appendable)
    }
}

Enhance YourLanguageRuntimeModule to get the generator from above injected everywhere:
  @Override
  public Class<? extends IGenerator> bindGenerator() {
    return ExtendedJvmModelGenerator.class;
  }

In YourLanguageJvmModelInferrer set an enum literal's compilationStrategy by calling
JvmTypesTypesBuilder's method setInitializer(JvmField, Procedure1<ITreeAppendable>).
For example, to generate the enum literal
  BAR("x")
you write
  @Inject extension JvmTypesBuilder
  ...
  members += v.toEnumerationLiteral('BAR') [
    initializer = [
      append('(')
      append('"x"')
      append(')')
    ]
  ]
Of course, in real life you wouldn't use hard-coded strings, but get strings or XLiterals from the DSL model.
Comment 2 Oliver Libutzki CLA 2013-11-15 09:50:36 EST
*** Bug 421511 has been marked as a duplicate of this bug. ***
Comment 3 Scott Forbes CLA 2016-02-29 02:56:10 EST
A couple of typos in the workaround:
'generateEumLiteral' for 'generateEnumLiteral'
'bindGenerator' for 'bindIGenerator'