Home > Enterprise >  Automatically fix sonar rule
Automatically fix sonar rule

Time:03-08

I'm looking for automaticaly fix this rule :

checkstyle:javadoc.SummaryJavadocCheck

I have over 1000 javadoc that don't have point at the end of the first line. I don't want to edit file one by one but perhaps in bash i can do a replace of the first line after /** that don't have point at the end?

Can someone help me for this or another idea?

EDIT1:

Invalid javadoc :

/**
 * Javadoc comment
 *
 * @return object an object
 */

Correct javadoc

/**
 * Javadoc comment.
 *
 * @return object an object
 */

Sonar want a point at the end of the line. It's not critical, just want to know if it's possible.

thanks

CodePudding user response:

You may use awk or sed, or pure bash : match exactly /** then add a dot on next line when that line does not have one. sed and bash versions below :

sed version:

sed -i.bak '/^\/\*\*$/{n;s/\([^\.]\)$/\1./;}' file1 file2 ...

Explanation :

-i.bak edit files inline, and create a backup (.bak).

/^\/\*\*$/ matches only lines which contain exactly /**.

{...} sed command block

n append next line in pattern space.

s/\([^\.]\)$/\1./ replace non "." (\([^\.]\)$) last character with itself and a dot (\1.).

Pure bash solution:

#!/bin/bash

while read -r line; do
    if [[ $line =~ ^/\*\*$ ]]; then
        read -r line2
        [[ $line2 =~ \.$ ]] || line2="$line2."
        printf -v line "%s\n%s" "$line" "$line2"
    fi
    printf "%s\n" "$line"
done

To do the same as the sed's -i.bak, you can use the bash version with something like:

for file in *.java; do
   cp -p "$file" "$file.bak"
   ./script.bash < "$file.bak" > "$file"
done
  • Related