0

I'm trying to write an DSL for doing typesafe conversions from one object to another.

src classA
dst classB

map valueA to valueB with ConverterX
map valueC to valueD with ConverterY

the result should be something like:

class Converter
{
public void convert(ClassA a, ClassB b)
{
 a.setValueA(ConverterX.convert(b.getValueB))
 b.setValueC(ConverterY.convert(b.getValueD))

}
}

I simply want to generate the code for that, but i'm not able to access the classes I already defined. The reason for that is to be able to use default converters. Only in case I am able to get the type of the parameters, i will be able to choose the implementation for the default converter.

Robert Franz
  • 418
  • 4
  • 14

2 Answers2

1

You can stick with JvmTypeReference as Xtext documentation suggests. You can get JvmType from jvmTypeRef.getType() where relevant types are castable to JvmDeclaredType. Here is my helper method to get list of all bean properties (class members) using JvmTypeReference:

public List<String> getFieldNamesForClass(JvmTypeReference jvmTypeRef) {
    List<String> result = new ArrayList<String>();
    if (jvmTypeRef.getType() instanceof JvmDeclaredType) {
        JvmDeclaredType declaredType = (JvmDeclaredType)jvmTypeRef.getType();
        for (JvmField field : declaredType.getDeclaredFields()) {
            result.add(field.getSimpleName());
        }
    }
    return result;
}

The output is List of Strings, the filed names, but it can be easily changed to return JvmField/JvmMember instances.

Check my DeepCloneDSL on bitbucket.org/espinosa/deepclonedsl

Espinosa
  • 2,491
  • 24
  • 28
0

I solved the problem by Using JvmDeclaredType instad of JvmTypeReference. JvmTypeReference doesn't offer access to fields and methods, but JvmDeclaredType does. It is also possible to generate a JvmTypeReference by knowing the QualifiedName that is present in the JvmDeclaredType.

Robert Franz
  • 418
  • 4
  • 14