本文介绍了使用Rascal从Eclipse项目中提取类依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rascal来分析Eclipse Java项目并确定此项目中的类依赖项。更确切地说:当且仅当类A具有(1)使用类型B的参数或(2)使用类型B的局部变量的方法时,类A依赖于类B.这里,我只对依赖感兴趣关系A - > B,其中A和B在我的项目中是两个类,并且都是不同的类。我已经从我的Eclipse项目创建了一个M3模型,并且能够使用以下内容识别所需的参数(类型B):

I am using Rascal to analyze an Eclipse Java project and identify the class dependencies in this project. To be more precise: class A depends on class B if and only if class A has a method which (1) uses a parameter of type B or (2) uses a local variable of type B. Here, I am only interested in dependency relations A -> B, where A and B are both classes within my project and are both different classes. I already created an M3 model from my Eclipse project and was able to identify the required parameters (of type B), using the following:

{ <e.name, f> | e <- model@declarations, e.name.scheme == "java+parameter", f <- model@typeDependency[e.name], !(/java/ := f.path), f.scheme != "java+primitiveType" }

在这里,我获取了我的M3模型的所有声明,过滤参数并使用 @typeDependency 注释我检索相应的类型(我不知道如何操作我使用 @types 代替)。最后,我使用最后两个语句来过滤掉类型与项目中的类无关的所有参数,如String和Integer参数。由于我对Rascal很新,我想不出另一种方法来实现这一点。

Here, I obtain all declarations of my M3 model, filter on parameters and using the @typeDependency annotation I retrieve the corresponding type (I didn't know how to manipulate the information I obtained when using @types instead). Finally, I use the last two statements to filter out all parameters of which the type is not related to a class in my project, like String and Integer parameters. As I am quite new to Rascal, I could not think of another way to achieve this.

我现在的问题是:如何检索B类型的局部变量我的项目?使用方案java + variable @typeDependency 结合使用是不够的,因为这还包括类型的变量Iterator或String与项目中的其他类有依赖关系,我对这些类型的变量不感兴趣。此外,结果集包含变量,如

My question now is: how can I retrieve the local variables of type B for my project? Using the scheme "java+variable" is not sufficient in combination with @typeDependency, as this also includes variables of type Iterator or String that have a dependency with other classes within my project and I am not interested in these types of variables. Moreover, the resulting set contains variables like

B b = field.method();

其中字段是其中的全局变量类。因此,从 b 到类本身存在类型依赖关系,我也想从我的结果中排除这些依赖关系。所以,我相信 @typeDependency 并不能提供我需要的信息。

where field is a global variable within that class. Because of this, a type dependency is present from b to the class itself and I would like to exclude these from my result as well. So, I believe that @typeDependency does not give me the information that I need.

我发现似乎有待处理使用Rascal的局部变量,但它使用AST而不是M3。我不知道如何在这个设置中使用AST(就像我说的,我只是Rascal的初学者)并且想知道是否可以用M3完成。我该怎么办?

I found this related post that seems to deal with local variables with Rascal, but it uses AST instead of M3. I am not sure how to use AST in this setting (like I said, I'm just a beginner in Rascal) and was wondering if it could be done with M3. How should I proceed?

编辑:为了清楚我想要达到的目标,我将举一个小例子。假设我的项目有两个类, A B B 包含一些方法( methodB1 methodB2 ,...)。
此外,类 A 具有以下结构:

in order to clearify what I want to achieve exactly, I will give a small example. Say that my project has two classes, A and B, and B contains some methods (methodB1, methodB2, ...).Moreover, class A has the following structure:

class A {

   void methodA1(){
        B importantVar;
        importantVar.methodB1();
        ...
        String someVar1 = importantVar.methodB2();
        int someVar2 = importantVar.methodB3();
   }

   void methodA2(){
        A someVar3;
        ...
   }

}

我希望能够检索的变量是 importantVar ,因为它是一个局部变量,它的类型是 B (我的项目中的一个类)。我对 someVar1 someVar2 不感兴趣,因为它们是具有与之无关的类型的局部变量我的项目。在我之前为参数提供的代码示例中,我使用最后两个语句过滤掉了这些类型。此外,我对 someVar3 也不感兴趣,因为它的类型为 A ,并且发生在同一个类中。

The variable that I would like to be able to retrieve is importantVar, because it is a local variable and it is of a type B (a class within my project). I am not interested in someVar1 and in someVar2, because they are local variables that have a type that is not related to my project. In the code example that I gave earlier for the parameters, I filtered these types out with the last two statements. Moreover, I am also not interested in someVar3, because it has type A and occurs within this same class.

我认为我应该能够使用 @types 检索我想要的局部变量,但我是不确定如何。当使用 @types 时,我得到一个 TypeSymbol ,我已经查看了这个数据类型的定义,看看我是怎么做的可以操纵它。 TypeSymbol 中的大多数数据类型都有一个名为 decl 的位置,我知道如何操作它。但问题是,我还收到了一些没有这个位置的类型,比如 \int() \array(.. )例如。在这些情况下,位置 decl 未定义。

I think that I should be able to retrieve the local variables that I want using @types, but I am not sure how. When using @types, I get a TypeSymbol and I already looked into the definition of this data type to see how I could manipulate it. Most of the data types within TypeSymbol have a location named decl, of which I know how to manipulate it. The problem is however, that I also receive some types that do not have this location, like \int() and \array(..) for instance. In these cases, the location decl is undefined.

推荐答案

如果你只想分析特定的 TypeSymbol ,使用模式匹配。这是一个例子。

If you only want to analyse a specific TypeSymbol, use pattern matching. Here is an example.

TypeSymbol t;
if (class(l,_) := t) {
  println(l);
}

或在理解中

{ l | <_,class(l,_)> <- m3@types};

或作为理解中查找的一部分:

or as part of a lookup in a comprehension:

{ <v, l> | <_,v> <- m3@containment, isVariable(v), class(l,_) <- m3@types[v]};

这篇关于使用Rascal从Eclipse项目中提取类依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:16