views:

481

answers:

7

I am trying to do a search in my Eclipse (Java) workspace to find all instances of static variables that are not final.

I tried various regex's but they do not result in any matches. Can someone suggest a regex that will match all lines containing 'static' and not containing 'final', and not ending in a '{'

The last part about not ending with a '{' will eliminate static methods.

An example

public class FlagOffendingStatics {
  private static String shouldBeFlagged = "not ok";
  private static final String ok = "this is fine";
  public static void methodsAreOK() {

  }
}

-- Thanks Parag

+3  A: 

Eclipse should have some sort of Java search built-in where you could specify that... Else, instead of writing one large monster regexp, try chaining together a bunch of greps:

grep -r static . | grep -v final

in the 1st statement, the -r causes the grep to recurse over a directory tree starting at the local directory, the results get piped to the 2nd grep which removes all the final's. Keep adding -v until everything superfluous is removed from the results. This is usually easier --albeit less elegant-- than figuring out a complicated regexp to take care of everything.

Yes this too might work, however I was not able to chain searches from Eclipse.Maybe I can try running this from a CygWin command prompt.--ThanksParag
A: 

Can you post a quick block of sample data for testing; I think I know what you're asking but I'm not sure.

Keng
I have edited the question with an example. The variable names are self explaining.
+2  A: 

FindBugs will find static non-final variables for you. (Along with many other interesting things.) I've had good results with using the standalone version. There is also an Eclipse plugin, but I haven't used that.

Zarkonnen
Yes FindBugs is a very good tool. I use it's Eclipse plugin, but it does not detect all instances of (non final) static attributes.More details in your edited comment...
Hmmm, I guess answers cannot be edited. FindBugs does not flag static attributes in static inner classes.
Also sometimes FindBugs does not report all static attributes in a class. I have a class with the following static attributes:public static int READ_ONLY = 0;public static int READ = 1;public static int WRITE = 2;FindBugs reported only the second and third attributes and not the first one.
+6  A: 

This pattern works:

[^(final)] static [^(final)][^(\})]*$

Here is a test:

$ cat test.txt
private int x = "3";
private static x = "3";
private final static String x = "3";
private static final String x = "3";
private static String x = "3";
public static void main(String args[]) {
        blah;
}

$ grep "[^(final)] static [^(final)][^(\})]*$" test.txt
private static x = "3";
private static String x = "3";

(I realize that private static x = "3"; isn't valid syntax, but the pattern still holds ok.)

The pattern accounts for the fact that final can appear before or after static with [^(final)] static [^(final)]. The rest of the pattern, [^(\})]*$, is meant to prevent any { characters from appearing in the remainder of the line.

This pattern will not work however if anyone likes to write their method statements like this:

private static void blah()
{
     //hi!
}
matt b
A: 

One of the IntelliJ code inspections already does this. You can actually run the code inspector stand alone if you want and have it generate a report (usefull for a nightly build).

As the previous poster said, Find Bugs will do this and I imagine other code inspection tools will do it as well. You're probably better off integrating one of those more complete code inspection tools rather than a one-off script just for this one thing.

Chris Kessel
+1  A: 

Instead of checking for the absence of a brace, I would look for a semicolon at the end:

^(?!.*\bfinal\b).*\bstatic\b.*;[ \t]*$
Alan Moore
A: 

This isn't a regex, but here is a plugin called checkstyle that will do that for you, as well as many other source checks. It even fixes many problems it finds automatically.

http://eclipse-cs.sourceforge.net/update/

Skip Head