【问题标题】:Referring to source code of Java project in xText参考xText中Java项目的源码
【发布时间】:2019-05-08 13:07:47
【问题描述】:

我正在 xText 中创建一个 DSL,用于对应用程序的功能行为进行建模。我的目标是将资源需求(例如 CPU 周期数、硬盘上的写入操作)与我想用 DSL 建模的功能行为结合起来。 DSL 是用 Eclipse IDE 用 xText 编写的。可以在下面找到包含 cmets 的 DSL 语法。

现在它是一个非常简单的 DSL 来模拟功能行为;结合 if/else 和 for 语句并向它们添加 libraryFunctions。我自己想出了后一个术语。它用于指代作为我的功能行为的基本步骤的操作(例如登录、加密、显示;您可以将它们视为编程语言中的方法)。现在我想扩展我的 DSL,使其能够引用 Java 项目的源代码。我创建了一个类似于带有登录屏幕和创建帐户屏幕的基本程序的小型 Java 程序(参见下面的类图)。为了使用 DSL 对该程序的功能行为进行建模,我希望能够参考 Java 程序源代码的某些细节,以便我可以直接从源代码中提取这些细节并在 DSL 中使用它。例如;假设我想引用 Java 程序中使用的某些方法。现在我的 DSL 中有简单的枚举“libraryFunctionsEnum”,但如果我能以某种方式直接引用 Java 程序源代码中使用的方法会很好(这样当我编译 DSL 并使用它时,xText编辑器会自动提供我可以参考的可用方法列表。

我尝试使用 ecore 模型来转换我的 Java 项目的类图并将它们集成到 xText 中,但我觉得我有点不知所措。我还研究了 xBase 和 xTend(旨在使 xText 与 Java 更具有互操作性的两种语言),但到目前为止,我发现它们更侧重于从 xText 模型自动生成 Java 源代码。我想用另一种方式来做(参考来自外部项目的 Java 源代码,以便我可以在我的 DSL 中使用这些引用)。我不知道我上面提到的方法(ecore、xBase、xTend)是否是实现我想要的正确方法。如果您有更好的想法或解释,我很高兴听到它!

顺便说一句,我还是 xText 和 DSL 建模/DSL 开发的新手。我可能忘记了一些重要的细节/解释。如果您遗漏了什么,请告诉我。

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 类图:

【问题讨论】:

    标签: java eclipse xtext


    【解决方案1】:

    我认为你想要达到的目标并不容易。尽管如此,还是有一些想法:

    • 您的“libraryFunctions”可以链接到通过 Java 反射 API 获得的 Java 元素。我想你会以某种方式导入一些“.java”或“.class”文件,并通过反射,引用它的 Method 或 Class 对象。
    • 相反,也许有可能,但我真的不确定,引用 Eclipse JDT 工具提供的元素,这样您就可以引用 Java 源文件中的元素并轻松链接到它(例如,在使用 ctrl + 左键单击)。或者,您可能需要了解如何从通过反射 API 检索的元素中链接到源 Java 代码(如果可能的话)。

    【讨论】:

    • 感谢您的回答。我还在 eclipse 论坛上问了这个问题(参见link,他们在那里给出了使用 xBase 的答案,这是一种似乎只是为此而创建的语言。我现在正在研究它,看看它是否有能力做我想做的事。
    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多