Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Infer local variable inside a method
Infer local variable inside a method [message #1770669] Wed, 16 August 2017 05:42 Go to next message
Amit Yadav is currently offline Amit YadavFriend
Messages: 22
Registered: June 2017
Junior Member
Hello,

I ma using Xbase and JvmModelInferrer to infer and generate code based on my model. I need to define a method within a generated class. The body of this method contains some logic (which is not coming from the DSL). This logic requires declaring a local variable within the class.

I am wondering what is the best way to achieve this?

Say for example I need to create a map
Map<String, SomeClass> blah;

I tried doing the following in the setBody function for the JvmOperation I am creating.

JvmTypeReference ref = typeReferenceBuilder.typeRef(Map.class,
typeReferenceBuilder.typeRef(String.class),
typeReferenceBuilder.typeRef(SomeClass.class));
jvmTypesBuilder.setBody(op, tree -> {
tree.append(ref.getSimpleName()).append(" ref blah;");
});

But this shows up as
Map blah;

in the generated code.

Then I tried
jvmTypesBuilder.setBody(op, tree -> {
tree.append(ref.getIdentifier()).append(" ref blah;");
});

but that shows up as
java.util.Map<java.lang.String, com.example.SomeClass> blah;

is there a way to get this to generate
Map<String, SomeClass> blah;

in the generated code ?

Thanks,
Re: Infer local variable inside a method [message #1770670 is a reply to message #1770669] Wed, 16 August 2017 05:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11588
Registered: July 2009
Senior Member
something like this should work

body = '''«Map»<«String»,«SomeClass»> bla;'''


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com

[Updated on: Wed, 16 August 2017 05:49]

Report message to a moderator

Re: Infer local variable inside a method [message #1770672 is a reply to message #1770670] Wed, 16 August 2017 07:34 Go to previous messageGo to next message
Amit Yadav is currently offline Amit YadavFriend
Messages: 22
Registered: June 2017
Junior Member
Sorry, should have mentioned that i am using java for model inference.
Re: Infer local variable inside a method [message #1770679 is a reply to message #1770672] Wed, 16 August 2017 08:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11588
Registered: July 2009
Senior Member
well, should have been easy to try it in xtend and have a look at the generated java code

public class MyDslJvmModelInferrer extends AbstractModelInferrer {
  @Inject
  @Extension
  private JvmTypesBuilder _jvmTypesBuilder;
  
  protected void _infer(final Model element, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
    EList<Greeting> _greetings = element.getGreetings();
    for (final Greeting greeting : _greetings) {
      String _name = greeting.getName();
      String _plus = ("test." + _name);
      final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
        EList<JvmMember> _members = it.getMembers();
        final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
          StringConcatenationClient _client = new StringConcatenationClient() {
            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
              _builder.append(Map.class);
              _builder.append("<");
              _builder.append(String.class);
              _builder.append(",");
              _builder.append(Integer.class);
              _builder.append("> map;");
            }
          };
          this._jvmTypesBuilder.setBody(it_1, _client);
        };
        JvmOperation _method = this._jvmTypesBuilder.toMethod(greeting, "demo", this._typeReferenceBuilder.typeRef(Void.TYPE), _function_1);
        this._jvmTypesBuilder.<JvmOperation>operator_add(_members, _method);
      };
      acceptor.<JvmGenericType>accept(this._jvmTypesBuilder.toClass(greeting, _plus), _function);
    }
  }
  
  public void infer(final EObject element, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
    if (element instanceof Model) {
      _infer((Model)element, acceptor, isPreIndexingPhase);
      return;
    } else if (element != null) {
      _infer(element, acceptor, isPreIndexingPhase);
      return;
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(element, acceptor, isPreIndexingPhase).toString());
    }
  }
}


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Infer local variable inside a method [message #1770779 is a reply to message #1770679] Thu, 17 August 2017 04:43 Go to previous message
Amit Yadav is currently offline Amit YadavFriend
Messages: 22
Registered: June 2017
Junior Member
Thank you Christian. That was helpful.
Previous Topic:<MyDSL>TerminalsValidator class not being generated
Next Topic:implement Xpath using Xtext
Goto Forum:
  


Current Time: Thu Jan 18 12:09:11 GMT 2018

Powered by FUDForum. Page generated in 0.01531 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software