我试图从Java M3模型中提取有关类型声明的一些事实。从一组M3文件中,我尝试使用一种理解方法,例如
> [type(m) | m <- models];
虽然我得到:未声明的变量:类型
然后,我只是尝试使用以下方法从方法中获取事实:
> [methods(m) | m <- models];
如文档所述。不过,我得到了类似的东西:
|std:///lang/java/m3/Core.rsc|(8877,1,<186,52>,<186,53>): NoSuchAnnotation("declarations")
那么,在一组M3模型上进行导航的正确方法是什么?如何获得有关M3模型的类和接口的信息?
我已经使用函数createM3FromProjectJars构建了M3文件。
最佳答案
好问题;关于此的文档至今还很少。最佳示例代码如下:http://tutor.rascal-mpl.org/Recipes/Recipes.html#/Recipes/Metrics/MeasuringJava/MeasuringJava.html
M3模型的源代码可以在这里解释很多:
https://github.com/usethesource/rascal/blob/master/src/org/rascalmpl/library/analysis/m3/Core.rsc
https://github.com/usethesource/rascal/blob/master/src/org/rascalmpl/library/lang/java/m3/Core.rsc
例如。后者包含以下定义:
anno rel[loc from, loc to] M3@extends; // classes extending classes and interfaces extending interfaces
anno rel[loc from, loc to] M3@implements; // classes implementing interfaces
anno rel[loc from, loc to] M3@methodInvocation; // methods calling each other (including constructors)
anno rel[loc from, loc to] M3@fieldAccess; // code using data (like fields)
anno rel[loc from, loc to] M3@typeDependency; // using a type literal in some code (types of variables, annotations)
anno rel[loc from, loc to] M3@methodOverrides; // which method override which other methods
anno rel[loc declaration, loc annotation] M3@annotations;
前者包含以下内容:
anno rel[loc name, loc src] M3@declarations; // maps declarations to where they are declared. contains any kind of data or type or code declaration (classes, fields, methods, variables, etc. etc.)
anno rel[loc name, TypeSymbol typ] M3@types; // assigns types to declared source code artifacts
anno rel[loc src, loc name] M3@uses; // maps source locations of usages to the respective declarations
anno rel[loc from, loc to] M3@containment; // what is logically contained in what else (not necessarily physically, but usually also)
anno list[Message] M3@messages; // error messages and warnings produced while constructing a single m3 model
anno rel[str simpleName, loc qualifiedName] M3@names; // convenience mapping from logical names to end-user readable (GUI) names, and vice versa
anno rel[loc definition, loc comments] M3@documentation; // comments and javadoc attached to declared things
anno rel[loc definition, Modifier modifier] M3@modifiers; // modifiers associated with declared things
这些定义完全一起记录了Java M3的模型。我不知道如果直接从jar文件生成M3模型,那么会显示多少信息。从Eclipse源项目中,所有这些表都已填充。
要实现您的查询,您可以:
[ m@types | m <- models]
;生成一个列表[rel [loc name,TypeSymbol typ]]{ *m@types | m <- models}
;所有模型中所有类型表的rel [loc name,TypeSymbol typ]联合{ t | m <- models, t <- m@types}
;之前的定义不同关于rascal - 从M3模型中提取事实,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40865558/