findbugs - Finding encoding issues in Java Project/Source -


i'm working on java project it's part of job watch on quality. tools use jenkins in combination sonar. these tools great , helped me track issues fast , continuously.

one issue don't under control people commit using other encoding utf-8.

when code this:

if (somestring == "something") {     resultstring = "string encoding problem: �"; } 

... gets committed, sonar me finding "string literal equality" issue. see in second line there issue encoding: "�" should "ΓΌ".

is there possibility find these kinds of problems sonar/findbugs/pmd...

please advice! thank you.

ps: of course i've tried explain issue co-developers in person via email. changed project/workspace encoding myself... somehow still succeed in committing code this.

i'm agree @bmargulies, it's valid utf-8 char (actually it's replacement character) after all, pmd rule help. here proof of concept rule hard-coded unallowed character list:

import net.sourceforge.pmd.abstractjavarule; import net.sourceforge.pmd.ast.astliteral;  import org.apache.commons.lang3.stringutils;  public class encodingrule extends abstractjavarule {      private static final string badchars = "\ufffd";      public encodingrule() {     }      @override     public object visit(final astliteral node, final object data) {         if (node.isstringliteral()) {             final string image = node.getimage();             if (stringutils.containsany(image, badchars)) {                 addviolationwithmessage(data, node, "disallowed char in '"                         + image + "'");             }         }         return super.visit(node, data);     }  } 

maybe useful invert condition , make allowedchars whitelist ascii characters , local chars well. (there more detail of custom pmd rules in answer.)


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -