Java 通过使用Sonar Api传递函数名来返回函数级度量(如圈复杂度),需要对下面代码进行哪些修改
我已经编写了代码,但它返回项目的平均功能级别指标。 需要修改代码以通过使用Sonar Api传递函数名/方法名来返回函数级度量,如圈复杂度Java 通过使用Sonar Api传递函数名来返回函数级度量(如圈复杂度),需要对下面代码进行哪些修改,java,web-services,sonarqube,Java,Web Services,Sonarqube,我已经编写了代码,但它返回项目的平均功能级别指标。 需要修改代码以通过使用Sonar Api传递函数名/方法名来返回函数级度量,如圈复杂度 import java.util.List; import org.sonar.wsclient.Sonar; import org.sonar.wsclient.services.Measure; import org.sonar.wsclient.services.ResourceQuery; public class SonarApi_1 {
import java.util.List;
import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.services.Measure;
import org.sonar.wsclient.services.ResourceQuery;
public class SonarApi_1 {
static String resourceKey = "Project_Name";
//Measures which will be returned
static String[] MEASURES_TO_GET = new String[]{"file_complexity", "class_complexity","function_complexity","lcom4","complexity","ncloc","functions","files","classes","packages"};
public static Sonar localSonar;
public static void main(String[] args)
{
try {
// To Create Connection Using Resource Key which is Project name , user Name and Password
localSonar = Sonar.create("http://illin018:8000", "User_name", "Password");
ResourceQuery query = ResourceQuery.createForMetrics(resourceKey, MEASURES_TO_GET);
query.setIncludeTrends(true);
Object resource = localSonar.find(query);
//To get measures
List<Measure> allMeasures = ((org.sonar.wsclient.services.Resource) resource).getMeasures();
for (Measure measure : allMeasures) {
System.out.println("Statements : " + measure.getMetricKey()
+ " === " + measure.getFormattedValue());
}
System.out.println("lcom4 means Lack of Cohesion of Functions");
System.out.println("ncloc means Non Commenting Lines of Code");
} catch(Exception e){
e.printStackTrace();
}
}
}
SonarQube不存储类和方法的度量,而只存储文件、目录、模块和项目的度量。我们开发了一个定制插件,可以为您提供这些指标,名为。目前支持的SonarQube版本有v3.7.4、v4.0和v4.1。但是我们几乎完成了插件的新版本(版本6.0),它支持从SQ4.2到4.3.2的版本。你可以找到一个在线演示 更新:
如果希望通过获取类或方法级别的度量,那么应该调用ResourceQuery对象的方法。对于JAVA类,方法的限定符是“MET”和“JavaClass”。您可以在浏览器中尝试,只需将URL栏更改为
{localhost:9000}/api/resources?metrics=ncloc&qualifiers=MET
。例如,在我们的在线演示中:不清楚您在问什么。问题是什么?当前运行代码时会发生什么情况?当前它返回特定项目的项目级指标。语句:ncloc==129919语句:类===823语句:文件===821语句:包===86语句:函数===5205语句:复杂度===25393语句:类复杂度===30.9语句:函数复杂度==4.9语句:文件复杂度===30.9语句:lcom4==1.1 lcom4表示缺少函数的内聚性ncloc意味着无注释的代码行。我想知道,当我传递方法/函数名时,我能够获得度量。我想知道我如何传递方法/函数名,以及如何为传递的函数返回度量值function@WarrenFaith我写的代码很好用。在评论中发布问题时,您编辑了一些错误。谢谢您的回复。但是请推荐我需要编写的额外代码,以获得函数级度量,如循环复杂度。我的代码目前返回一个特定项目中所有函数循环复杂度的sim-total。Hi@L.Lango谢谢你的回复,我将尝试使用这个。若我有文件名或目录名,你们能告诉我如何使用SonarAPI和java调用返回值吗coding@MukulBhasin,不清楚你想要什么。这取决于您希望返回什么值以及返回到何处?:)但我认为你应该为此提出一个新问题,并提供更多细节。是否要通过Web服务API获取SourceMeter的函数和类级别度量?是的,通过Web服务API。我想通过传递方法名作为输入来返回方法级别度量的值。更新了答案。我希望这对你有帮助。
Statements : ncloc === 129,919
Statements : classes === 823
Statements : files === 821
Statements : packages === 86
Statements : functions === 5,205
Statements : complexity === 25,393
Statements : class_complexity === 30.9
Statements : function_complexity === 4.9
Statements : file_complexity === 30.9
Statements : lcom4 === 1.1
lcom4 means Lack of Cohesion of Functions
ncloc means Non Commenting Lines of Code