سؤال

I have xtext file similar to the xtext describe in here: (www.ebpml.org/blog2/index.php/2010/11/05/mde-xtext-and-json)

JSONDataSource: root = Object ;
Object: '{' firstObject=TerminalObject (',' objects+=TerminalObject)* '}';
Array: '['firstItem=ObjectValue (',' items+=ObjectValue)* ']';
EmptyObject: isEmpty?= '{}' ;
EmptyArray: isEmpty?= '[]' ;
ObjectValue: value=STRING | Object | Array | EmptyObject | EmptyArray ;
TerminalObject: element=STRING ':' (content = ObjectValue) ;

So after generating the code I have all the objects generated in java. now in I want to have more methods in those generated java files.

what is the best way to do so?
thanks :)

هل كانت مفيدة؟

المحلول

Well, there are no easy ways to customize these classes. You can do basically two things:

  1. Create your own EMF (Ecore) model, and create your grammar in a way that reuses your Ecore model. This way, you can define new operations, and create their implementations manually.
  2. You could also set up a postprocessor for the Ecore model generator that fills out the contents. See http://christiandietrich.wordpress.com/2011/07/22/customizing-xtext-metamodel-inference-using-xtend2/ for details - but be vary, as the model inference cannot directly output custom code; there are some EMF genmodel-annotation based way to create the bodies of generated code (see the final part of http://jevopisdeveloperblog.blogspot.hu/2011/03/implement-tostring-with-xtexts.html for details).

Alltogether, customizing the generated EMF models is quite hard, and I do not recommend doing it. Although it seems unnecessary to introduce new helper classes, technologically it is much easier to do.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top