Structural Replace - need advise how to use

Here's a real-world example of a use case for structural search/replace, which I cannot figure out how to make work.

We have many methods that take both a HttpServletRequest parameter, and a HttpServletResponse parameter.

The goal: Redeclare any method taking both of these params to take instead a MyRequestContext parameter named "context" (preserving whatever other arguments they also take). All calling code would be updated. All method bodies would replace the usages of these paramters with context.getRequest() and context.getResponse().

This is a very powerful refactoring. I have the sense that it should be possible, but I didn't get very far trying it. Perhaps it needs to be done in several stages.

Thanks!

2 comments

Anyone out there with any tips?

0

Hi,

I see many variants applying the source code change you want to
accomplish in many steps. For instance:
- You can update all methods to add the additional parameter
MyRequestContext via change method signature refactoring, replace all
access to HttpServletRequest/HttpServletResponse with structural search
and drop the parameters with change method signature again.

It is possible to do it only with SSR as illustrated in following sketch:
1.

Replace
class $class$ {
$OtherContent$
public void doGet(HttpServletRequest $request$,HttpServletResponse
$response$) { $GetContent$; }
}

with
class $class$ {
$OtherContent$
public void doGet(HttpServletRequest $request$,HttpServletResponse
$response$, MyContext myContext) { $GetContent$; }
}

constraints:
min,max for OtherContent 0...100000
min,max for GetContent 0...100000

2.

Find all affected methods
class $class$ {
$OtherContent$
public void $doGet$(HttpServletRequest $request$,HttpServletResponse
$response$, MyContext myContext) { $GetContent$; }
}

doGet target of the search

Replace in previous search results scope

$a$.$anymethod$($params$) with
$a$.getRequest().$anymethod($params$)

$a$ expression type for is HttpServletRequest (or HttpServletResponse)

3. Replace the extra method parameters

class $class$ {
$OtherContent$
public void doGet(HttpServletRequest $request$,HttpServletResponse
$response$, MyContext myContext) { $GetContent$; }
}

with
class $class$ {
$OtherContent$
public void doGet(MyContext myContext) { $GetContent$; }
}

4. update the calls with
$a$.doGet($params$) with
$a$.doGet(myContext)

david wrote:

Here's a real-world example of a use case for structural search/replace, which I cannot figure out how to make work.

We have many methods that take both a HttpServletRequest parameter, and a HttpServletResponse parameter.

The goal: Redeclare any method taking both of these params to take instead a MyRequestContext parameter named "context" (preserving whatever other arguments they also take). All calling code would be updated. All method bodies would replace the usages of these paramters with context.getRequest() and context.getResponse().

This is a very powerful refactoring. I have the sense that it should be possible, but I didn't get very far trying it. Perhaps it needs to be done in several stages.

Thanks!



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Please sign in to leave a comment.