Regex find static (not finite) variables

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

I tried various regular expressions, but they do not lead to any matches. Can someone suggest a regex that matches all lines containing static and not containing final , and does not end in { ?

The last part ending in { excludes static methods.

Example:

 public class FlagOffendingStatics { private static String shouldBeFlagged = "not ok"; private static final String ok = "this is fine"; public static void methodsAreOK() { } } 
+7
java eclipse regex
source share
7 answers

This model works:

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

Here is the 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 understand that private static x = "3"; is not valid syntax, but the template is still saved.)

The sample takes into account the fact that final can appear before or after static with [^(final)] static [^(final)] . The rest of the pattern [^(\})]*$ intended to prevent any { characters from appearing in the rest of the string.

This template will not work if someone likes to write their instructions like this:

 private static void blah() { //hi! } 
+7
source share

Eclipse should have a built-in built-in Java search in which you could specify this ... Otherwise, instead of writing one big monster regular expression, try combining a bunch of greps:

grep -r static . | grep -v final

in the 1st expression, -r causes grep to recurs over the directory tree, starting from the local directory, the results are in the second grep, which removes all final 's. Continue adding -v until all unnecessary ones are removed from the results. This is usually simpler - albeit less elegant - than calculating a complex regular expression to take care of everything.

+3
source share

Instead of checking for a missing bracket, I would look for a semicolon at the end:

 ^(?![ \t]*import\b)(?!.*\bfinal\b).*\bstatic\b.*;[ \t]*$ 
+3
source share

Enhanced @ matt-b pattern

 [^(final|import)] static [^(final|class|{|enum)][^(\})]*$ 
+3
source share

FindBugs will find static non-finite variables for you. (Along with many other interesting things.) I had good results using the standalone version. There is also an Eclipse plugin, but I have not used it.

+2
source share

One of the IntelliJ code inspections already does this. You can start the code browser yourself if you want and create a report (useful for nightly builds).

As the previous poster said, Find Bugs will do it, and I think other code checking tools will do it too. You are probably better off integrating one of these more comprehensive code checking tools, rather than a one-time script just for that purpose.

0
source share

This is not a regular expression, but a plugin called checkstyle that will do this for you, as well as many other source code checks. It even fixes many problems that it finds automatically.

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

0
source share

All Articles