Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Add additional code to inferred code in a method body(How to add additional Java Code in a Method Body 'around' the Java Code inferred for XBlockExpression)
Add additional code to inferred code in a method body [message #1725372] Thu, 03 March 2016 06:24 Go to next message
Shrinivas Kini is currently offline Shrinivas KiniFriend
Messages: 3
Registered: March 2016
Junior Member
I am developing a simple DSL which allows you create a type and all methods to it. I am using an XBlockExpression for the body of the method. When infering the Java Code using JvmModelInferrer I want to add some additional code around the 'inferred code' of the expression.

E.g. I want to want to nest the inferred code of the expression in a try-catch block. But I dont want the try-catch code to be expressed in the DSL.

grammar com.study.InterfaceDefinition with org.eclipse.xtext.xbase.Xbase

import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types

InterfaceModel:
package=PackageDeclaration
importSection=XImportSection?
definition=InterfaceDef;

PackageDeclaration:
'package' name=QualifiedName;

InterfaceDef:
'Interface' name=ID
'Type' ':' (types+=ServiceType) (',' types+=ServiceType)*
newDef=NewDefinition);

NewDefinition:
'{'
(newMethods+=NewMethodDefinition)+
'}';

NewMethodDefinition:
commonModifier=CommonModifier
returnType=JvmTypeReference name=ValidID '('
(parameters+=Parameter (',' parameters+=Parameter)*)? ')'
expression=XBlockExpression;

Parameter:
parameterType=JvmTypeReference name=ValidID;

In the code for the inferrer I am inferring the code for method as follows

members += newMethod.toMethod(newMethod.name, newMethod.returnType ?: inferredType)
[
for (param : newMethod.parameters) {
parameters += newMethod.toParameter(param.name, param.parameterType)}

body = newMethod.expression
]


Instead of infering the code for method body as above, can I do something like this.


body = [
append("try { ")

--- append the inferred java code the expression here ----

append("catch(Exception e) { <some more code> }")
]

In the output I need the method body to look like this

{
try
{
// << Inferred Java Code for XExpression>>
}
catch (Exception ex)
{
ex.printStackTrace();
logger.error(ex.getMessage(), ex);
return generateInternalServerErrorResponse();
}
return generateNoContentResponse();
}

Any suggestions will be very helpful.

Re: Add additional code to inferred code in a method body [message #1725460 is a reply to message #1725372] Thu, 03 March 2016 15:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
This is currently not possible

But you can inferr 2 methods

(1) body=model.body

(2) body = ''' try .. <<nameof1>>(.....'''

(2 contains the code you want to wrap as string and calls 1)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add additional code to inferred code in a method body [message #1725461 is a reply to message #1725372] Thu, 03 March 2016 15:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
See https://www.eclipse.org/forums/index.php/t/1074824/ for a similar
usecase


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add additional code to inferred code in a method body [message #1725509 is a reply to message #1725461] Fri, 04 March 2016 04:16 Go to previous messageGo to next message
Shrinivas Kini is currently offline Shrinivas KiniFriend
Messages: 3
Registered: March 2016
Junior Member
Christian, thank you very much. I will certainly try that. I am curious to know if this capability will be considered in the next release?
Re: Add additional code to inferred code in a method body [message #1725512 is a reply to message #1725509] Fri, 04 March 2016 05:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i cannot answer this but feel free to vote for

https://bugs.eclipse.org/bugs/show_bug.cgi?id=481992


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Add additional code to inferred code in a method body [message #1725663 is a reply to message #1725512] Sun, 06 March 2016 04:17 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
I would love to see this feature as well. What is the recommended way to "vote" on bugzilla? By leaving a comment?
Re: Add additional code to inferred code in a method body [message #1725666 is a reply to message #1725663] Sun, 06 March 2016 09:24 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can comment on the bug. and you can use bugzillas vote feature
(there is a vote link under importance)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Content is not allowed in prolog
Next Topic:Using XPath for AST querying
Goto Forum:
  


Current Time: Thu Apr 25 03:48:48 GMT 2024

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

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

Back to the top