Patrick Patrick - 1 year ago 110
Java Question

Getting Method Signatures from Java Files with Select-String and a regex

the project i work on at the moment is a restful Webbackend. I use Spring MVC for this.

However i want to use powershell to generate a small documentation of the rest api. I want to give this to the frontend developer. He is not a java gui, so i don't want to just throw a java doc at his feet.

So to cut a long story short. I'm trying to write script, that looks for my controller files, and reads the signatures of methods which are annotated with @RequestMapping...


@RequestMapping(value="/account", method=RequestMethod.GET)
public @ResponseBody List<AccountIF> getAll() {
return service.getAll();

I'm able to read the line with the @ pretty easily. But when i try to get both lines with an regex, i get nothing.

This is what i have so far:

ls -r -i * | Select-String -Pattern '@RequestMapping.+\n\t+.+'

What am i doing wrong?

Answer Source

Your regex doesn't match: there is no newline + tab characters in your method.


Assuming that you want to get every method signature decorated with @RequestMapping this should work:

Get-ChildItem -Path '.\*' | Get-Content -Raw | ForEach-Object {
    [regex]::Matches($_, '\s*(@RequestMapping.+\))\s+(.+\))').Value.Trim()

P.S. On a side note, it seems that Select-String is processing file line-by-line, so multiline regex wouldn't work anyway.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download