Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Adding API to access ImportManager or to add imports to ITreeAppendable
Adding API to access ImportManager or to add imports to ITreeAppendable [message #949670] Fri, 19 October 2012 07:15 Go to next message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
Compared to ugly code like (from http://www.eclipse.org/forums/index.php/t/262321/):
@Inject extension TypeReferenceSerializer

...

f.toField(name, type) [
  val Procedure1<ITreeAppendable> proc = [
     append('''new ''')
     newTypeRef('java.util.BitSet').serialize(f,it)
     append('''(«dataDef.fields.size»)''')
    ]
  initializer = proc
]

I'd prefer to write
f.toField(name, type) [
  val Procedure1<ITreeAppendable> proc = [
     addImport(newTypeRef('java.util.BitSet').type)
     append('''new BitSet(«dataDef.fields.size»)''')
    ]
  initializer = proc
]

The obvious problem is breaking user-defined classes implementing ITreeAppendable, but I am not sure if people do this in practice. Of course, after I wrote it, I saw that this problem could be solved by creating my own class extending TreeAppendable, and overriding JvmModelGenerator.createAppendable to return it... So is it worth filing a bug for it?
Re: Adding API to access ImportManager or to add imports to ITreeAppendable [message #961538 is a reply to message #949670] Sun, 28 October 2012 11:00 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 10/19/2012 09:15 AM, Alexey Romanov wrote:
> Compared to ugly code like (from
> http://www.eclipse.org/forums/index.php/t/262321/):
>
> @Inject extension TypeReferenceSerializer
>
> ..
>
> f.toField(name, type) [
> val Procedure1<ITreeAppendable> proc = [
> append('''new ''')
> newTypeRef('java.util.BitSet').serialize(f,it)
> append('''(«dataDef.fields.size»)''')
> ]
> initializer = proc
> ]
>
> I'd prefer to write
>
> f.toField(name, type) [
> val Procedure1<ITreeAppendable> proc = [
> addImport(newTypeRef('java.util.BitSet').type)
> append('''new BitSet(«dataDef.fields.size»)''')
> ]
> initializer = proc
> ]
>
> The obvious problem is breaking user-defined classes implementing
> ITreeAppendable, but I am not sure if people do this in practice. Of
> course, after I wrote it, I saw that this problem could be solved by
> creating my own class extending TreeAppendable, and overriding
> JvmModelGenerator.createAppendable to return it... So is it worth filing
> a bug for it?


the appendable supports append of JvmType, so you could write

f.toField(name, type) [
val Procedure1<ITreeAppendable> proc = [
append('''new ''')
append(newTypeRef('java.util.BitSet').type)
append('''(«dataDef.fields.size»)''')
]
initializer = proc
]

without using the serializer... which might be less ugly?

from what I understand about the appendable and import manager, using

f.toField(name, type) [
val Procedure1<ITreeAppendable> proc = [
addImport(newTypeRef('java.util.BitSet').type)
append('''new BitSet(«dataDef.fields.size»)''')
]
initializer = proc
]

would still not be safe, since the "BitSet" you write in append('''new
BitSet(«dataDef.fields.size»)''') would not ensure that the string
"BitSet" is actually referred to the type ref java.util.BitSet (and
possible ambiguities would not be handled transparently by the
ImportManager).

somehow, the append of a type in the appendable should still be done NOT
as a string, but as a JvmType so that the ImportManager can deal with
that... at least that's how I understand it works

hope that helps
cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Re: Adding API to access ImportManager or to add imports to ITreeAppendable [message #961660 is a reply to message #961538] Sun, 28 October 2012 13:29 Go to previous messageGo to next message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
> f.toField(name, type) [
> val Procedure1<ITreeAppendable> proc = [
> append('''new ''')
> append(newTypeRef('java.util.BitSet').type)
> append('''(«dataDef.fields.size»)''')
> ]
> initializer = proc
> ]

Yes, that's a bit better. Too bad something like

append('''new «append(newTypeRef('java.util.BitSet').type)»(«dataDef.fields.size»)'''

without breaking up the flow won't work.

> since the "BitSet" you write in append('''new
BitSet(«dataDef.fields.size»)''') would not ensure that the string
"BitSet" is actually referred to the type ref java.util.BitSet

Yes, this would only work for the non-ambiguous case.
Re: Adding API to access ImportManager or to add imports to ITreeAppendable [message #961818 is a reply to message #961660] Sun, 28 October 2012 16:22 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
We will adress that issue in the next release and allow things like

'''<<typeof(BitSet)>> and <<type('java.util.BitSet')>>'''

See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=382015

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 28.10.12 14:29, schrieb Alexey Romanov:
>> f.toField(name, type) [
>> val Procedure1<ITreeAppendable> proc = [
>> append('''new ''')
>> append(newTypeRef('java.util.BitSet').type)
>> append('''(«dataDef.fields.size»)''')
>> ]
>> initializer = proc
>> ]
>
> Yes, that's a bit better. Too bad something like
> append('''new
> «append(newTypeRef('java.util.BitSet').type)»(«dataDef.fields.size»)'''
>
> without breaking up the flow won't work.
>
>> since the "BitSet" you write in append('''new
> BitSet(«dataDef.fields.size»)''') would not ensure that the string
> "BitSet" is actually referred to the type ref java.util.BitSet
>
> Yes, this would only work for the non-ambiguous case.
Re: Adding API to access ImportManager or to add imports to ITreeAppendable [message #961834 is a reply to message #961818] Sun, 28 October 2012 16:42 Go to previous message
Alexey Romanov is currently offline Alexey RomanovFriend
Messages: 263
Registered: May 2010
Senior Member
Very nice!
Previous Topic:Generating Objects
Next Topic:Changing a cross-reference during generation
Goto Forum:
  


Current Time: Fri Apr 26 19:17:30 GMT 2024

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

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

Back to the top