[xpand] problem calling xpand from Java [message #559366] |
Thu, 16 September 2010 13:31 |
Arwin Messages: 3 Registered: September 2010 |
Junior Member |
|
|
I keep getting the following exception when calling Xpand from a Java program:
Exception in thread "main" EvaluationException : No Definition templates::Template::main for idl::Interface could be found!
Internal error : element was null
However I am sure I pass an object of type Interface to Xpand AND the template containing a definition
main FOR Interface is found by Xpand.
My code:
ResourceSet resourceSet = new ResourceSetImpl();
URI uri = URI.createFileURI(InputFileName);
Resource resource = resourceSet.getResource(uri, true);
Model model = (Model) resource.getContents().get(0);
// XPand
String outputFolder = "output";
OutputImpl output = new OutputImpl();
Outlet outlet = new Outlet(outputFolder);
outlet.setOverwrite(true);
output.addOutlet(outlet);
EmfMetaModel metaModel = new EmfMetaModel(IdlPackage.eINSTANCE);
XpandExecutionContextImpl executionContext = new XpandExecutionContextImpl(output, null);
executionContext.registerMetaModel(metaModel);
XpandFacade facade = XpandFacade.create(executionContext);
facade.evaluate("templates::Template::main", model.getInterfaces().get(0));
My template file Template.xpt:
«IMPORT org::xtext::example::idl::idl»
«EXTENSION templates::Extensions»
«DEFINE main FOR Interface-»
«REM» TODO only for non-local interfaces!! «ENDREM»
«EXPAND header FOR this»
«EXPAND proxy FOR this»
«EXPAND stub FOR this»
«ENDDEFINE»
«DEFINE header FOR Interface»
«FILE this.name + "_PS.h"-»
«EXPAND Header::header FOR this-»
«ENDFILE»
«ENDDEFINE»
«DEFINE proxy FOR Interface»
«FILE this.name + "_Proxy.c"-»
«EXPAND Proxy::proxy FOR this-»
«ENDFILE»
«ENDDEFINE»
«DEFINE stub FOR Interface»
«FILE this.name + "_Stub.c"-»
«EXPAND Stub::stub FOR this-»
«ENDFILE»
«ENDDEFINE»
I did some debugging and I stepped into:
XpandFacade.evaluate
-> XpandFacade.evaluate2
---> XpandExecutionContextImpl.findDefinition (public)
-----> XpandExecutionContextImpl.findDefinition (private) :
private XpandDefinition findDefinition(final XpandDefinition[] definitions, final String name, final Type target,
Type[] paramTypes, final XpandExecutionContext ctx) {
if (paramTypes == null) {
paramTypes = new Type[0];
}
final Set<Callable> features = new HashSet<Callable>();
for (int i = 0; i < definitions.length; i++) {
final XpandDefinition def = definitions[i];
if (def.getParams().length == paramTypes.length) {
final List<Type> defsParamTypes = new ArrayList<Type>();
Type t = null;
boolean complete = true;
for (int j = 0; j < paramTypes.length && complete; j++) {
t = ctx.getTypeForName(def.getParams()[j].getType().getValue());
if (t == null) {
complete = false;
}
defsParamTypes.add(t);
}
t = ctx.getTypeForName(def.getTargetType());
if (t == null) {
complete = false;
}
if (complete) {
features.add(new DefinitionOperationAdapter(def, def.getName(), t, defsParamTypes));
}
}
}
final DefinitionOperationAdapter defAdapter = (DefinitionOperationAdapter) PolymorphicResolver.getOperation(
features, XpandUtil.getLastSegment(name), target, Arrays.asList(paramTypes));
if (defAdapter != null)
return defAdapter.def;
return null;
}
I notice the following:
- first loop is taken 4 times since 4 definitions were found in my template (which is ok) main, header, proxy and stub For Interface
- 2nd loop is always false, since paramTypes.length is 0 for all definitions
- ctx.getTypeForName returns false every time (for Interface). So "complete" is always false, and features.add is never called
- defAdapter is null, so finddefinition returns null always resulting in the exception
I don't know which of the observations above are expected or not, but I do know that findDefinition should not return null ......
Any idea what is going wrong here?
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03544 seconds