3434 * @author jborgers
3535 */
3636public class UselessSuppressionsHelper {
37- static final Pattern NEXT_METHOD_NAME_PATTERN = Pattern .compile ("\\ R*^\\ s*[\\ w\\ s<>]+\\ s+([ \\ w] +)\\ (" );
38- static final Pattern NEXT_FIELD_NAME_PATTERN = Pattern .compile ("\\ R*\\ s*[\\ w\\ s<>,?]+\\ s+([ \\ w] +)\\ s*[=;]" );
39- static final Pattern COMMENT_PATTERN = Pattern .compile ("(\\ / \\ /.*|\\ /\\ *(?s:.)*?\\ *\\ /)" ); // Matches single-line and multi-line comments
37+ static final Pattern NEXT_METHOD_NAME_PATTERN = Pattern .compile ("\\ R*^\\ s*[\\ w\\ s<>]+\\ s+(\\ w+)\\ (" );
38+ static final Pattern NEXT_FIELD_NAME_PATTERN = Pattern .compile ("\\ R*\\ s*[\\ w\\ s<>,?]+\\ s+(\\ w+)\\ s*[=;]" );
39+ static final Pattern COMMENT_PATTERN = Pattern .compile ("(/ /.*|/\\ *(?s:.)*?\\ */)" ); // Matches single-line and multi-line comments
4040 static final String NO_METHOD = "<nom>" ;
4141 final Map <String , Set <String >> classMethodToRuleNameOfSuppressedViolationsMap = new HashMap <>();
4242 final Map <String , Set <String >> classMethodToRuleNameOfViolationsMap = new HashMap <>();
@@ -55,7 +55,7 @@ public class UselessSuppressionsHelper {
5555
5656 void storeRuleNameForMethod (Report .SuppressedViolation suppressed ) {
5757 RuleViolation violation = suppressed .getRuleViolation ();
58- if (!violation .getMethodName ().isEmpty ()) {
58+ if (violation . getMethodName () != null && !violation .getMethodName ().isEmpty ()) {
5959 // store for method
6060 String methodKey = violation .getPackageName () + "-" + violation .getClassName () + "-" + violation .getMethodName ();
6161 Set <String > suppressedMethodRuleNames = classMethodToRuleNameOfSuppressedViolationsMap .get (methodKey );
@@ -136,8 +136,6 @@ private void addNodeIfUseless(List<PMDUselessSuppression> uselessSuppressions, P
136136 uselessSuppressions .add (new PMDUselessSuppression (pmdViolation , annotatedRuleName ));
137137 }
138138 }
139- } else {
140- // cannot deal with other, non-pmd suppressions
141139 }
142140 }
143141 }
@@ -179,18 +177,20 @@ ViolatingAnnotationHolder getAnnotationContext(PMDViolation annotationViolation)
179177 if (virtualFile != null ) {
180178 ApplicationManager .getApplication ().runReadAction (() -> {
181179 Document doc = FileDocumentManager .getInstance ().getDocument (virtualFile );
182- int startOffset = doc .getLineStartOffset (annotationViolation .getBeginLine () - 1 ) + annotationViolation .getBeginColumn ();
183- int endOffset = doc .getLineStartOffset (annotationViolation .getEndLine () - 1 ) + annotationViolation .getEndColumn () - 1 ;
184- String violatingAnnotation = doc .getText (new TextRange (startOffset , endOffset ));
185- String methodName ;
186- if (!annotationViolation .getMethodName ().isEmpty ()) { // an annotation inside a method
187- methodName = annotationViolation .getMethodName ();
188- } else {
189- int startAfter = doc .getLineStartOffset (annotationViolation .getEndLine ());
190- String after = doc .getText (new TextRange (startAfter , doc .getTextLength () - 1 ));
191- methodName = findMethodName (after );
180+ if (doc != null ) {
181+ int startOffset = doc .getLineStartOffset (annotationViolation .getBeginLine () - 1 ) + annotationViolation .getBeginColumn ();
182+ int endOffset = doc .getLineStartOffset (annotationViolation .getEndLine () - 1 ) + annotationViolation .getEndColumn () - 1 ;
183+ String violatingAnnotation = doc .getText (new TextRange (startOffset , endOffset ));
184+ String methodName ;
185+ if (!annotationViolation .getMethodName ().isEmpty ()) { // an annotation inside a method
186+ methodName = annotationViolation .getMethodName ();
187+ } else {
188+ int startAfter = doc .getLineStartOffset (annotationViolation .getEndLine ());
189+ String after = doc .getText (new TextRange (startAfter , doc .getTextLength () - 1 ));
190+ methodName = findMethodName (after );
191+ }
192+ annotationContextResult = new ViolatingAnnotationHolder (violatingAnnotation , methodName );
192193 }
193- annotationContextResult = new ViolatingAnnotationHolder (violatingAnnotation , methodName );
194194 });
195195 }
196196 return annotationContextResult ;
0 commit comments