Static code analyzers, definition of code patterns

I have a number of entries in a new code base where there is a sequence of method calls, such as

object o = something.foo(); bar(o); something.foobar(); 

in consecutive lines. I would like to find / calculate the total number of such sequences in my code, where the name of the object "something" may be different, but I want to process the same.

I want to pull them out as a method and see all the locations where it needs to be reorganized. How do I do something like this?

+7
java static-analysis
source share
4 answers

You can use a regular expression - for example, in Netbeans, this is a regular expression:

 (?s)object o = (.*?)\.foo\(\);\s+bar\(o\);\s+(\1)\.foobar\(\); 

finds all lines of type:

 object o = xyz.foo(); bar(o); xyz.foobar(); 

where xyz can be anything if it matches the first and last line.

+1
source share

Intellij IDEA (including the free community version) duplicates code detection and replacement.

If you choose one of these cases in a method that uses its refactoring function, it goes through the code base and asks you if you want to replace it in other places.

+2
source share

There are tools that can do this ready, and they are called software conversion tools .

If you really want to recognize them as a serial line, with our DMS Software Reengineering Toolkit software conversion engine using your rule specification language (RSL). RSL allows you to write patterns in terms of the syntax of the language of the language whose grammar is already known to DMS:

 domain Java~v7; pattern odd_pattern(IDENTIFIER: o, qualifier: something, IDENTIFIER: foo IDENTIFIER: bar, IDENTIFIER: foobar, more: stmt_sequence): stmt_sequence = " Object \o = \something.\foo; \bar(\o); \something.\foobar(); \more"; 

This defines the pattern in terms of the surface syntax of the indicated notation ("domain Java ~ v7"). Each template has a name ("odd_pattern") so you can distinguish between many templates. The template has a set of formatted (pure grammar) syntactic categories specified by sub-templates, referred to as <(non) terminal: name>. This template has several different identifiers, name patterns o, foo, bar, foobar. I guessed that you meant that something was a kind of qualification way, but perhaps you meant that it was also an identifier.

The contents of the template are defined in metacites " to distinguish the text of the RSL template language from the text of the target language (Java) template. Inside meta-pictures \ baz means the syntax template variable baz category N, where baz was defined as N: baz in the template parameter list. One and the same however, a template variable that occurs in several places requires identical substitutions.

Given this pattern, you can ask DMS where the (Java) AST corresponds. Alignment with AST, as well as white space problems, comments, unnormalized strings / identifiers / numbers / etc. eliminated. Just by counting matches, you get the original desired OP score. [We basically completed the extension of RSL templates in which elements are connected by data streams, rather than code syntax].

Presumably, he wants to convert these instances into some other code that he can do by writing in RSL,

  rule rewrite_odd_pattern(parms): stmt_sequence odd_pattern(parms) -> replacement_pattern(parms); 

mention of some other "replacement_pattern" written in a similar way.

If the OP has only a small number of such patterns, it probably makes better use of the regular expression or just punches it rather than getting the DMS (or other program conversion tool) and tweaking it. If he does this a lot, or they are scattered across very large heaps of code, the templates are complex, or he has many similar things, then a compromise may overturn another way.

+2
source share

If your code is Java 1.6 or lower, you can use Eclipse MoDisco to create an EMF model , an example of your Java projects. You can then implement Model Query to search for the template that you are describing. Although the Java MoDisco model is based on Java 1.5, you can also use it for 1.6 because there were no syntax changes in 1.6.

+1
source share

All Articles