简体   繁体   中英

Referring to source code of Java project in xText

I am creating a DSL in xText for modeling the functional behaviour of an application. My goal for this is to couple resource demands (eg number of CPU cycles, write actions on hard drive) with the functional behaviour I want to model with the DSL. The DSL is written in xText with the Eclipse IDE. The DSL syntax including comments can be found below.

For now it's a really simple DSL to model functional behaviour; combining if/else and for statements and adding libraryFunctions to them. I came up with this latter term myself; it is used to refer to actions which are the basic steps of my functional behaviour (eg login, encrypt, display; you can think of them as methods in a programming language). Now I would like to extent my DSL with the option to be able to refer to the source code of a Java Project. I created a small Java Program which resembles a basic program with a login screen and create account screen (see class diagram below). In order to model the functional behaviour of this program with the DSL, I want to be able to refer to certain details of the Java Program source code so that I can pull these details right from the source code and use it in the DSL. For example; suppose that I want to refer to certain methods used in the Java Program. Right now I have simple enumeration 'libraryFunctionsEnum' in my DSL, but it would be nice if I could somehow directly refer to the methods used in the source code of the Java Program (so that when I compile the DSL and use it, the xText editor automically provides a list of available methods I can refer to).

I tried to use ecore models to convert class diagrams of my Java Project and integrate them in xText, but I have the feeling that I am a bit lost on what to do. I have also looked at xBase and xTend (two languages directed towards making xText more interoperable with Java), but so far I have found that these are more focussed on automatically generating Java source code from xText models. I want to do it the other way around (refer to Java source code from an external project so that I can use these references in my DSL). I don't know if the methods I mentioned above (ecore, xBase, xTend) are even the right methods to achieve what I want. If you have some better idea or explation, then I am glad to hear it!

By the way, I am still new to xText and DSL modelling/DSL development. I might have forgotten some important details/explanations. Please let me know if you miss something.

grammar org.xtext.example.mydsl.FinalDsl with org.eclipse.xtext.common.Terminals

generate finalDsl "http://www.xtext.org/example/mydsl/FinalDsl"

Model:
    'functionName' name = STRING
    functions += FunctionElements*
;

// Function elements of which the model exists. The model can contain
// library functions, for loops, and if/else statements.
  FunctionElements:
        (   
            functions += libraryFunctionsEnum |
            forLoops += ForLoops |
            ifElseStatements += IfElseStatements
        )
; 

// IfElse Statements requiring if statements and optionally followed by
// one else statement.
IfElseStatements: 
    ifStatements += IfStatements
    (elseStatement = ElseStatement)?
;

// If statements requiring conditions and optionally followed by
// library functions or for loops.
IfStatements:
    'if'
    conditions = Conditions
    (ifFunctions += libraryFunctionsEnum | forLoops += ForLoops)
;

// Else statement requiring one or multiple library functions.
ElseStatement:
        'else' elseFunctions += libraryFunctionsEnum
;

// For loops requiring one condition and followed by zero or more
// library functions
ForLoops:
    'for'
    conditions = Conditions
    libraryFunctions += libraryFunctionsEnum*

;

//*Eventually filled with details from class diagram, but for now we manually fill it for the sake of testing.
enum libraryFunctionsEnum:
createAccount='createInstance'|
login='login'|
hasCode= 'encrypt'|
display='display'
;



Conditions:
    STRING
    operator=logicalOperators
    STRING
;

enum logicalOperators:
greaterThan='>'|
smallerThan='<'|
greaterOrEqualThan='=>'|
smallerOrEqualThan='<='|
equalTo='=='
;

Java Class Diagram:

Java类图

I think what you want to achieve is not easy. Still, a few thoughts:

  • Your "libraryFunctions" could link to Java elements gotten through the Java Reflection API. I imagine you would somehow import some ".java" or ".class" file, and through reflection, reference its Method or Class objects.
  • Instead, maybe it is possible, but I am really not sure, to reference elements provided by the Eclipse JDT tooling, such that you could reference elements from a Java source file and easily link to it (eg to open the editor on it with a ctrl + left click). Or you might have to find out how, from an element retrieved via the Reflection API, you will be able to link to the source Java code (if it is even possible).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM