|
Re: Xtend versus Java Imports [message #1862645 is a reply to message #1862644] |
Sun, 17 December 2023 12:43 |
|
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
Day Job: https://www.everest-systems.com
|
|
|
|
Powered by
FUDForum. Page generated in 0.04629 seconds