You are here

Přidat komentář

Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

Věříte výsledkům z FindBugs?

Nástroje statické analýzy mohou nalézt pravděpodobná problémová místa. Občas ale překvapí tím, že některé triviální chyby jim uniknou mezi prsty. Například při kompilaci Java 6.0 nebo v Eclipse 3.4 nenalezne FindBugs v1.3.8 v následující metodě nic podezřelého:

public String testStringBuilder( final String what, final int times ) {
  String result = "";
  for ( int i = 0; i < times; i++ )
    result += what;
  return result;
}

Měl by ovšem detekovat SBSC_USE_STRINGBUFFER_CONCATENATION (Method concatenates strings using + in a loop). Proč to nefunguje? FindBugs pracuje na úrovni byte kódu, podívejme se tedy na (dekompilovaný) bytekód generovaný z Java 6 a Eclipse:

String result = "";
for(int i = 0; i < times; i++)
    result = (new StringBuilder(String.valueOf(result))).append(what).toString();

return result;

Bytekód generovný z Java 5 je odlišný (zvláštní, StringBuilder(String s) byl už v Java 5, proč ho nepoužili?) a na tomto bytekódu FindBugs problém nalezne:

String s1 = "";
for(int j = 0; j < i; j++)
    s1 = (new StringBuilder()).append(s1).append(s).toString();

return s1;

Chybu jsem zareportoval.

Plain text

  • No HTML tags allowed.
  • Řádky a odstavce se zalomí automaticky.