new reformat code option
Hi,
I was just wading through a lot of code with things like
String bla = new String("bla");
in it. Do you think it would be a good idea to have an option that can
automatically remove these (or highlight them ? I can see hardly any use
for that way of creating a String object. Right now, I can't start up
IDEA (simply doesn't run for some reason) so that I can't check if it is
already covered by some function.
Dirk Dittert
请先登录再写评论。
Use Search -> Replace...?
Use Search -> Replace...?
woops... sorry about the repost :s
Could be made an inspection?
String constants are shared through a pool. If you write a code like this:
... you will get:
...which means that s1 and s2 are not only lexicographically equal, but hold reference to the same object.
If you write a code like this:
... you will get:
There are rare cases when you really need these variables to reference different objects.
Still, these cases exist and I had to take care of this once or twice in the past.
Having said that, I would not like these cases reformatted automatically, but an inspection would be nice (or an "on-the-fly" check in the editor).
Sergei S. Ivanov wrote:
Could you give an example?
Thanks,
Gordon
--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919
I can picture odd circumstances where you need multiple copies of the same String objects, each thread-local to a different thread. That way operations which might lock on a String (what? don't know. said it was odd) will be able to run without blocking one another.
Unfortunately, no. Though I definitely remember that I had to take care of it (perhaps in a context of object serialization???).
Dave Griffith <dave.griffith@trilogy.com> wrote:
Ok, so what's the bottom line now? File a feature request for an
inspection? Using two distinct String Objects for synchronization is one
of the few reasons why you would want to have two objects. IMHO this is
not a good approach.
Dirk Dittert
>Ok, so what's the bottom line now? File a feature request for an inspection?
Sounds about right. An inspection with an auto-fix button would cover this nicely.
The inspection looks quite trivial, it is equivalent to finding usages of java.lang.String(java.lang.String) constructor.
The inspection could be doubled with an automatic check in the editor, which will generate a warning and an intention to remove unnecessary constructor.
New editor warning/intention: usages of "new String(String)"
http://www.intellij.net/tracker/idea/viewSCR?publicId=12413
New inspection: find usages of "new String(String)"
http://www.intellij.net/tracker/idea/viewSCR?publicId=12412
I had to do this just yesterday, because I was using Strings as keys in a
WeakHashMap.
"Gordon Tyler" <gordon.tyler@quest.com> wrote in message
news:ba3fci$4nf$2@is.intellij.net...
reference different objects.
>
>
>
I can think of only one example. The same code was consistently giving different results for for the same data under the "same" conditions. In production - one result, for a small test case - a totally different one. It turned out that the root of the problem was using == for comparing strings instead of equals(). A small failing test case used "new String(string)" to show that sometimes "string" != new String("string").
Dave Griffith wrote:
Synchronizing on an immutable object? Very odd indeed.
Ciao,
Gordon
--
Gordon Tyler (Software Developer)
Quest Software <http://java.quest.com/>
260 King Street East, Toronto, Ontario M5A 4L5, Canada
Voice: 416-643-4846 | Fax: 416-594-1919
Sergei S. Ivanov <jiveadmin@jetbrains.com> wrote:
well, that happens if you are out having fun for a complete day --
someone submits your feature requests...
Thanks for for submitting these requests ;)
Dirk Dittert