首页 > 解决方案 > 在 Java 包上使用“ls”命令时出现不支持的方案错误

问题描述

我正在开发一个函数,它为 Java M3 提供每个包的代码量列表。这个函数看起来像这样:

public list[int] calculateSizePerComponent(M3 model){
    set[loc] packages = packages(model);
    list[int] componentSizes = [];
    for(package <- packages){
        list[loc] classFiles = [x | x <- package.ls, endsWith(x.file, ".java")];
        if(size(classFiles)>0){
            int sourceSize = 0;
            for(classFile <- classFiles){
                sourceSize+=getLinesOfCode(classFile).linesOfCode;
            }
            componentSizes += sourceSize;
        }
    }
    return componentSizes;
}

我使用以下函数来计算 Java 编译单元(适用于其他示例)中的代码行数(体积):

public tuple[int linesOfCode,int blankLines,int commentLines] getLinesOfCode(loc location) {
    int linesOfCode = 0;
    int blankLines = 0;
    int commentLines = 0;
    bool incomment = false; 

    srcLines = readFileLines(location);     
    for (line <- srcLines) {
        switch(line){
            case /^\s*\/\/\s*\w*/: commentLines += 1; // Line preceded by '//'
            case /((\s*\/\*[\w\s]+\*\/)+[\s\w]+(\/\/[\s\w]+$)*)+/: linesOfCode += 1; // Line containing Java code containing any amount of comments. Example: code /**comment*/ code /**comment*/ code
            case /^\s*\/\*?[\w\s\?\@]*\*\/$/: commentLines += 1; // Line containing single line comment: /*comment*/
            case /\s*\/\*[\w\s]*\*\/[\s\w]+/: linesOfCode += 1; // Line containing a comment, but also code. Example: /**comment*/ code
            case /^[\s\w]*\*\/\s*\w+[\s\w]*/: {incomment = false; linesOfCode += 1;} // Line closing a multi-line comment, but also containing code. Example: comment*/ code
            case /^\s*\/\*\*?[^\*\/]*$/: {incomment = true; commentLines += 1;} // Line opening a multi-line comment, Example: /**comment
            case /\s*\*\/\s*$/: {commentLines += 1; incomment = false;} // Line closing a multi-line comment, Example: comment*/
            case /^\s*$/: blankLines += 1; // Blank line
            default: if (incomment) commentLines += 1; else linesOfCode += 1;
        }
    }
    return <linesOfCode,blankLines,commentLines>;
}

但是,package.ls似乎返回具有错误方案的结果。因此,我在readFileLines通话中收到以下错误:

|std:///IO.rsc|(14565,775,<583,0>,<603,43>): IO("Unsupported scheme java+package")
        at *** somewhere ***(|std:///IO.rsc|(14565,775,<583,0>,<603,43>))
        at readFileLines(|project://Software_Evolution/src/metrics/volume.rsc|(1911,8,<49,26>,<49,34>))
        at calculateSizePerComponent(|project://Software_Evolution/src/metrics/componentsize.rsc|(1996,38,<64,16>,<64,54>))
        at getComponentSize(|project://Software_Evolution/src/metrics/componentsize.rsc|(267,1112,<15,0>,<42,1>))
        at $root$(|prompt:///|(0,30,<1,0>,<1,30>))

当我打印位置时,我得到以下信息:

|java+package:///smallsql/database/language/Language.java|

这是不正确的,因为这是一个 java 编译单元而不是一个包。如何获取此文件中的代码行?

标签: javarascal

解决方案


Rascal 函数resolveLocation最终解决了这个问题。因此package.ls,我不得不使用resolveLocation(package).ls.


推荐阅读