0
public void visitToken(DetailAST aAST) {}

I am trying to write a custom checkstyle rule. I am interested in the TokenTypes.STRING_LITERAL. The problem with this approach is, A string might be a concatenated string, StringBuffer, StringBuilder or could be within a method.

Bear with me, as I am a newbie to the Checkstyle coding.

  1. How do I get a full string if it is concatenated. The aAST seems to be spitting them out as individual string literals.

  2. Is there another way to grab a complete string?

Any pointers, greatly appreciated.

AchuSai
  • 93
  • 1
  • 1
  • 6

1 Answers1

0

This is hard to do in Checkstyle, because Checkstyle works purely on the AST. It is no compiler, so it does not know about runtime types or syntactic meaning.

So, in order to do this using Checkstyle, you would have to analyze the AST manually and build your concatenated String by hand. If parts of the String are generated by, say, static methods, or by using a StringBuilder/StringBuffer, then I would say the task of finding the complete String by AST analysis becomes virtually impossible.

Instead, you might want to look at other static code analysis tools which might be better suited to your task. FindBugs, for instance, works on the compiled code and is generally able to perform quite sophisticated checks. However, it takes more resources to run than Checkstyle, and on older machines you may not be able to have FindBugs run automatically on save in your IDE.

barfuin
  • 16,865
  • 10
  • 85
  • 132
  • Thanks Thomas. I totally agree with the pain involved in parsing the AST. I managed to get the SQL String literal (concatenated). The run-time parameters cannot be captured still. I followed the pattern [link = http://grepcode.com/file/repo1.maven.org$maven2@com.qulice$qulice-checkstyle@0.1.9@com$qulice$checkstyle$StringLiteralsConcatenationCheck.java"]here.[/link] and adapted it to concatenate my sql string. This seems to work for StringBuilder/StringBuffer constructed strings as well. I will update if I find something unusual. – AchuSai Feb 06 '13 at 16:15