Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend versus Java Imports(Java code generation)
Xtend versus Java Imports [message #1862644] Sun, 17 December 2023 08:52 Go to next message
Franz Fehringer is currently offline Franz FehringerFriend
Messages: 7
Registered: December 2023
Junior Member
Hi all,

I am generating Java code via a Xtend generator (extends AbstractGenerator) using a GeneratorComponent in a mwe2 workflow.
Given that i do not use XBase (or JBase) for the moment, is there any possibility to insert (generate) unqualified Java items like ArrayList and have the Xtext/Xtend/mwe2 machinery compute the necessary imports und insert them into the generated Java code?
Thx in advance
Re: Xtend versus Java Imports [message #1862645 is a reply to message #1862644] Sun, 17 December 2023 12:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14695
Registered: July 2009
Senior Member
you might be able to replicate what xbase does

import org.eclipse.xtend2.lib.StringConcatenation
import org.eclipse.xtend2.lib.StringConcatenationClient

/**
* @author dietrich - Initial contribution and API
*/
class Main {
def static void main(String[] args) {
val c = new ImportingStringConcatenation()
c.append(compile)
println('''
«c.imports»
«c»
''')
}

def static TypeLiteral type(String type) {
new TypeLiteral(type)
}

def static StringConcatenationClient compile() '''
«compile2»
public «type("java.util.List")» doit() {
return new «type("java.util.ArrayList")»();
}
'''

//changed return types are very important to
// get the «type("java.util.Set")» magic to work
def static StringConcatenationClient compile2() '''
public «type("java.util.Set")» doit2() {
return new «type("java.util.HashSet")»();
}
'''
}

class ImportingStringConcatenation extends StringConcatenation {

public val imports = <String>newHashSet // move to an import manager class
// like the one in xbase. aslo look for
// other stuff ImportingStringConcatenation in xbase does do
// dont know if you always have a class to add so that you dont need
// the type literal

override protected getStringRepresentation(Object object) {
if (object instanceof TypeLiteral) {
imports.add(object.type)
return object.type.substring(object.type.lastIndexOf(".")+1) // todo make save
}
super.getStringRepresentation(object)
}

}

class TypeLiteral {
String type
new(String type) {
this.type = type
}
def String getType() {
return this.type
}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend versus Java Imports [message #1862646 is a reply to message #1862645] Sun, 17 December 2023 13:23 Go to previous message
Franz Fehringer is currently offline Franz FehringerFriend
Messages: 7
Registered: December 2023
Junior Member
Many thx, i will give it a try.
Previous Topic:OutofMemoryError when trying to use Spring Boot
Next Topic:Grammar for extension equal to a keyword
Goto Forum:
  


Current Time: Mon Jul 15 19:57:23 GMT 2024

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

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

Back to the top