Custom Sonarqube Rule Construction

Asked

Viewed 266 times

1

hello!

I am creating a plugin in Sonarqube with custom Rules and as my first Rule, I want to do a validation in If declarations where an alarm is triggered every time a literal String enters as parameter.

I am following the example structure that is in software documentation, but I cannot find any documentation on possible alternatives in the construction of Rules.

To test, I’m using a code from an Android DIVA screen:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class HardcodeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hardcode);
    }

    public void access(View view) {
        EditText hckey = (EditText) findViewById(R.id.hcKey);

        if (hckey.getText().toString().equals("vendorsecretkey")) { // Noncompliant
            Toast.makeText(this, "Access granted! See you on the other side :)", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "Access denied! See you in hell :D", Toast.LENGTH_SHORT).show();
        }
    }
}

Having said that, the comparison is being made by my Rule in the following excerpt:

    @Override
  public void visitNode(Tree tree){
    IfStatementTree ifStatement = (IfStatementTree) tree;
    ifStatement.condition().kind();
    if (ifStatement.condition().kind() == (Kind.STRING_LITERAL)){
        reportIssue(ifStatement.ifKeyword(), "Comparação if sendo feita com string literal!");
    }
  }

The problem is occurring due to the factors being used to compare the rule, but I can’t find any method or attribute that can help me in this comparison (I also couldn’t find any documentation explaining more about Ifstatementtree, the most I could find was that).

Could someone please help me with this issue or point me to some content that has more information?

Thank you for your attention!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.