简体   繁体   English

SonarQube。 为Java创建自定义规则

[英]SonarQube. Create custom rule for Java

anyone can help me with this case. 任何人都可以帮助我解决这个问题。

I have the next test file: 我有下一个测试文件:

AWSCredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
AmazonWebServiceClient client = new AmazonS3Client(provider); // Noncompliant 

Are there way to extract the particular implementation of interface from the variable provider when I explore the arguments of constructor AmazonS3Client? 探索构造函数AmazonS3Client的参数时,是否可以从变量提供程序中提取接口的特定实现? Now I can extract AWSCredentialsProvider for this variable only. 现在,我只能提取此变量的AWSCredentialsProvider。

As it's not that explicit, I'm guessing that you are trying to retrieve implementation type used to initialize th provider variable, which would be EnvironmentVariableCredientialsProvider . 由于不是那么明确,我猜您正在尝试检索用于初始化provider变量的实现类型,该类型将为EnvironmentVariableCredientialsProvider

There is currently no easy and straightforward way to achieve this. 当前,没有简单,直接的方法来实现这一目标。 The best way would be to proceed as follow: 最好的方法如下:

  • From the provider IdentifierTree , use the semantic API to get to the corresponding symbol. provider IdentifierTree ,使用语义API来获取相应的符号。
  • From the provider symbol, jump to the declaration tree (which will be null if the identifier is declared in another file). provider符号中跳到声明树(如果标识符在另一个文件中声明,则该树将为null)。
  • From the declaration , and if the tree is a VariableTree , look for the initializer expression. declaration ,如果树是VariableTree ,则查找初始化器表达式。
  • From the ExpressionTree , get the type of the symbol Type, which will give you the implementation type. ExpressionTree ,获取符号Type的类型,这将为您提供实现类型。

That's a first step which should help you to get further. 这是第一步,应该可以帮助您进一步发展。 Of course you will next probably need to handle cases where the variable is declared without initializer, and then assigned somewhere else. 当然,接下来您可能需要处理在没有初始化程序的情况下声明变量,然后将其分配到其他地方的情况。 To retrieve these expressions, look at the symbol usages, instead of declaration, and walk the trees. 要检索这些表达式,请查看符号用法,而不是声明,然后遍历树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM