Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3292

Expose as API utility methods resolving expression as constant

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.1
    • Component/s: Java API
    • Labels:

      Description

      SonarSecurity relies on custom rules API to write Security rules. However, not everything that we are using in checks is available to be used by Custom Rules. In particular, the ConstantUtils class is requiring implementation class from the semantic layer to access data.

      This can not be performed in Custom Rules, as it relies at the end on ECJ classes, which are not exposed.

      The following methods would be interesting to expose in our API, potentially at ExpressionTree level.

      org.sonar.java.checks.helpers.ConstantUtils.resolveAsConstant(ExpressionTree)
      org.sonar.java.checks.helpers.ConstantUtils.resolveAsIntConstant(ExpressionTree)
      org.sonar.java.checks.helpers.ConstantUtils.resolveAsLongConstant(ExpressionTree)
      org.sonar.java.checks.helpers.ConstantUtils.resolveAsStringConstant(ExpressionTree)
      

      The proposed solution is to enrich the ExpressionTree API by adding two new methods:

      org.sonar.plugins.java.api.tree.ExpressionTree.asConstant(): java.util.Optional<java.lang.Object>
      org.sonar.plugins.java.api.tree.ExpressionTree.asConstant(Class<T>): java.util.Optional<T>
      

        Attachments

          Activity

            People

            Assignee:
            michael.gumowski Michael Gumowski
            Reporter:
            michael.gumowski Michael Gumowski
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Due:
              Created:
              Updated:
              Resolved: