2

I want to replace my annotations from @RequestMapping to @GetMapping, @PutMapping ... annotations. When I looked at the Structural Find/Replace in IntelliJ looked like it could do the job.

I tried adding the following in the search

@RequestMapping( $key$ = $value$) Added a filter on the key. text=method.

Now I want to extract the from the value (RequestMethod.GET) , the word after . (period). and then in the replacement add

@[Word(TitleCase)]Mapping( [everything except the key,value that was extracted in the search])

Haven't been able to figure out how to go about this. Would be nice to know if this can't be done, or any suggestions on how to do this. Looked at some of the other questions here on SO, but didn't find anything that could help. Most of the answers are to use regex in those cases.

Before:

@RequestMapping(
    value = "/channels/{channel_name}",
    method = RequestMethod.POST,
    produces = MediaType.APPLICATION_JSON_VALUE,
    consumes = MediaType.APPLICATION_JSON_VALUE)
public Channel updateChannel(
    @PathVariable("channel_name") String channelName,
    @Valid @RequestBody Channel channel) {
  return channelService.updateChannel(channelName, channel);
}

@RequestMapping(
    value = "/channels/{channel_name}",
    method = RequestMethod.DELETE,
    produces = MediaType.APPLICATION_JSON_VALUE)
public Channel deleteChannel(
    @PathVariable("channel_name") String channelName) {
  return channelService.deleteChannel(channelName);
}

After

@PostMapping(value = "/channels/{channel_name}",
    produces = MediaType.APPLICATION_JSON_VALUE,
    consumes = MediaType.APPLICATION_JSON_VALUE)
public Channel updateChannel(
    @PathVariable("channel_name") String channelName,
    @Valid @RequestBody Channel channel) {
  return channelService.updateChannel(channelName, channel);
}

@DeleteMapping(
    value = "/channels/{channel_name}",
    produces = MediaType.APPLICATION_JSON_VALUE)
public Channel deleteChannel(
    @PathVariable("channel_name") String channelName) {
  return channelService.deleteChannel(channelName);
}
Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
Imran
  • 413
  • 5
  • 14

1 Answers1

0

I would do this dirty, with regex:

  1. Replace RequestMethod.(.)(.+)(?=,) into RequestMethod.\U$1\L$2 (\L would turn the text into lowercase.)
  2. Replace @RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(.+?), into @$5Mapping\($1$2$3.

Then simplify this replacement chain:

Replace @RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(\S)(.+?), into @\U$5\L$6\EMapping\($1$2

Update: Noticed the first parameter value is not specified whether in the line of @Mapping or a standalone line.

  • If you need it in the line of @Mapping, replace @RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(\S)(.+?),\s into @\U$5\L$6\EMapping\($2$3.
  • If you need it to a standalone line, replace @RequestMapping\((\s+)(.+)(\s+?)(.+)RequestMethod.(\S)(.+?), into @\U$5\L$6\EMapping\($1$2.
Geno Chen
  • 4,916
  • 6
  • 21
  • 39