IPP/IG: idea

Hi Dave,

I've also got one further idea:

It would be cool, if member variables could be detected, which are
references only in one method and the first operation is always a
write-access. The quick-fix should be to make the variable a local variable.

Example:

public void TestClass {

private final Object obj;

public TestClass(Object obj) {
this.obj = obj;
Utils.doSomethingElse(this.obj);
}
}

Thanks in advance.

Tom

6 comments
Comment actions Permalink

"Field may be local" would be very cool, and it's been on the list for a while (Oracle's CodeCoach product has it). Unfortunately, it's harder than it looks. It'll probably require some dataflow algorithms I've been avoiding. In any case, the quick fix you suggest isn't actually always safe. There are multi-threaded cases (including some well known patterns) where making such a variables local would break the algorithm.

--Dave Griffith

0
Comment actions Permalink

There are multi-threaded cases (including some well known patterns) where making such a variables local would break the algorithm.


Hmm, never though about posssible multi-threading problems before. Do
you have a possible problem in mind?

Tom

0
Comment actions Permalink


Something like this (which is somewhat horrible, but should give the idea).

class Bar
{
private boolean locked;

public lock()
{
locked = true;
//initialize some stuff
if(locked)
{
// do some other stuff;
}
}

public unlock()
{
locked = false;
}

}

Since "locked" is always written before being read, your proposed inspection would say to make "locked" a local variable. This would be incorrect, if lock and unlock could be done in separate threads.

That said, it certainly would be a valuable inspection. It's multi-threaded issues would simply need to be documented.

--Dave Griffith

0
Comment actions Permalink

No, this example is not legitimate, because it uses locked in two
different methods.

Tom

0
Comment actions Permalink

Got it. I misread your request, and was using the more general semantics for marking fields as may be local used by Oracle CodeCoach. I'm pretty sure there are still threading issues even with the one-method limitation.

--Dave Griffith

0
Comment actions Permalink

Hi Dave,

Tell IDEA to inline a synchronized method and it will do without warning
about the possible thread problem. So, I guess, it will be possible for
your plugin as well :)

Happy Easter,
Tom

0

Please sign in to leave a comment.