Compilador louco

pessoal,

Declarei st como String e o compilador esta aceitando o comando abaixo. Parece loucura :?

System.out.printf("%d",st);

mas na hora de gerar o resultado ocorre erro :lol:

Voce que eh louco, nao o compilador :). Ele nao valida o que esta dentro das strings.

Rafael

:twisted:

auhauhauahuahua

essa eh boa… compilador louco hauhauahuahauha

vc ta caregando algum dado na string???

tentei, mas quando fiz isso deu excecao :evil:

Faça uma RFE (Request for Enhancement) para o pessoal que escreve o javac na Sun, e o jikes na IBM (Eclipse).

Peça para eles validarem os parâmetros do printf em tempo de compilação. (Se você olhar a declaração do printf, vai ver que é

void printf (String format, Object… args);

ou algo parecido, ou seja, o compilador pode aceitar qualquer objeto.
Atualmente isso não é feito.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4984918

[quote=Description]When the format string of printf/format is constant, the compiler should check
it against the remaining argument strings.[/quote]

Mesmo que é problemático esse negócio de só checar os tipos ao rodar, fica difícil não concordar com a avaliação.

Será que alguém pode escrever uma regra para o PMD ou CheckStyle que efetue esse teste então?

Ótima idéia. Falta tempo pelo menos no momento, mas adicionei um Feature Request para o projeto PMD:

http://sourceforge.net/tracker/index.php?func=detail&aid=1685750&group_id=56262&atid=479924

Bom, algum desenvolvedor do PMD aparentemente vai tentar criar a regra:

https://sourceforge.net/tracker/?func=detail&atid=479924&aid=1685750&group_id=56262

[quote]
Sami

This is an interesting rule. As is typical with rules like this, there is
a falability that the rule may not be able to detect. If the formatted
String were passed in as in your example, I think the rule would be pretty
straightforward. However, this code may be difficult to detect:

String st = “test”;
String ftm = “%d”;
System.out.printf(fmt, st);

Imagine now that fmt isn’t a String declared in the method but rather
passed in from outside the method. I’m going to give this one a shot, just
letting you know what may be missed.

Allan[/quote]