importing from java in custom expressions [message #1722540] |
Fri, 05 February 2016 14:16 |
Daniel Cardin Messages: 109 Registered: July 2009 |
Senior Member |
|
|
Hello Christian,
When using a custom xbase compiler, how do you deal with additional imports?
I am trying to create a new literal for "measure", ie. value + unit of measurement.
Since, the character '~' seems unused, I'm using it at the moment for parsing.
val length = ~150 mm
Measure returns xbase::XExpression:
{Measure} '~' measure=XNumberLiteral unit=ID
;
I have modified the compiler to generate the equivalent code
Measure.valueOf(150, SI.MILLI(SI.METER))
How do I make these extensions automatically imported ?
I have modified my custom featurescopes
val measure = EcoreUtil2.getContainerOfType(featureCall, Measure)
if (measure != null) {
newSession = session.addImports [ importer |
var JvmType type = typeReferences.findDeclaredType("javax.measure.units.SI", featureCall.eResource());
if (type instanceof JvmDeclaredType) {
importer.importStaticExtension(type as JvmDeclaredType, true);
}
]
}
But it's never called... maybe because it's not considered a feature call when you output the code directly from the compiler appender?
What is the magic that's needed here?
Thanks !
|
|
|
|
Re: importing from java in custom expressions [message #1722545 is a reply to message #1722543] |
Fri, 05 February 2016 14:49 |
Daniel Cardin Messages: 109 Registered: July 2009 |
Senior Member |
|
|
Type compiler:
def dispatch computeTypes(Measure attribute, ITypeComputationState state) {
if (attribute.unit != null) {
val typeRef = state.referenceOwner.
newUnknownTypeReference('''org.jscience.physics.measures.Measure<javax.measure.quantities.«typeByUnit(attribute.unit)»>''')
state.acceptActualType(typeRef)
}
}
def String typeByUnit(String unit) {
switch unit {
case "mm",
case 'cm',
case 'dm',
case 'm': 'Length'
case 'mm2',
case 'cm2',
case 'dm2',
case 'm2': 'Area'
case 'mm3',
case 'cm3',
case 'dm3',
case 'm3': 'Volume'
case 'mg',
case 'g',
case 'kg': 'Mass'
}
}
The compiler:
def void _toJavaStatement(Measure measure, ITreeAppendable appendable, boolean isReferenced) {
appendable.append('''Measure.valueOf(«number.value», SI.MILLI(SI.METER))''')
}
I have also tried with :
def void _toJavaStatement(Measure measure, ITreeAppendable appendable, boolean isReferenced) {
val measureType = typeReferences.findDeclaredType("org.jscience.physics.measures.Measure", measure).type
appendable.append(measureType.type)
val milli = typeReferences.findDeclaredType("javax.measure.units.SI", measure).type
val number = measure.measure as XNumberLiteral
appendable.append('''.valueOf(«number.value»,''')
appendable.append(milli.type)
appendable.append('''.MILLI(SI.METER))''')
}
Is that the better way? To get the type references there ?
There are many constants in the package, it would not be fun to import everything manually... an import static javax.measure.units.SI.* would be much better.
Thanks !
|
|
|
|
Re: importing from java in custom expressions [message #1722557 is a reply to message #1722548] |
Fri, 05 February 2016 18:00 |
Daniel Cardin Messages: 109 Registered: July 2009 |
Senior Member |
|
|
Is it right to assume that you cannot import anything that is not subsequently "written" to the appender, whether it's in the inferrer or elsewhere ?
Because if I could output a : import static javax.measure.units.SI.* in the resulting java class, it would be best.
Alternatively, since I know exactly what I want to import, maybe I could have default imports ?
does the technique of overriding ImportedNamespaceAwareLocalScopeProvider still work ?
Or is that just to define the scope and then you need to "use" a reference for it to be imported in the end java class ?
[Updated on: Fri, 05 February 2016 18:01] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.02144 seconds