This is an automated email from the ASF dual-hosted git repository.

ppisl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 82040cd  Fixing NPE in Spock code completion tests
     new ddc319d  Merge pull request #3840 from ppisl/NPE_in_spock_tests
82040cd is described below

commit 82040cddeff2064fd76ee02a3dad3a6cfa9cfcca
Author: Petr Pisl <[email protected]>
AuthorDate: Wed Mar 23 15:07:45 2022 +0100

    Fixing NPE in Spock code completion tests
---
 .../editor/api/parser/GroovyParserResult.java      |  5 +++-
 .../completion/SpockMethodParamCompletion.java     | 15 ++++++-----
 .../groovy/editor/completion/SpockUtils.java       | 31 +++++++++++++---------
 3 files changed, 32 insertions(+), 19 deletions(-)

diff --git 
a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/parser/GroovyParserResult.java
 
b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/parser/GroovyParserResult.java
index ae29892..877939a 100644
--- 
a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/parser/GroovyParserResult.java
+++ 
b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/api/parser/GroovyParserResult.java
@@ -82,7 +82,10 @@ public class GroovyParserResult extends ParserResult {
             return null;
         }
         LookupResult lr = unit.getClassNodeResolver().resolveName(className, 
unit);
-        return lr.getClassNode();
+        if (lr != null) {
+            return lr.getClassNode();
+        }
+        return null;
     }
 
     // FIXME remove this
diff --git 
a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockMethodParamCompletion.java
 
b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockMethodParamCompletion.java
index 13c58b5..f18d43d 100644
--- 
a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockMethodParamCompletion.java
+++ 
b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockMethodParamCompletion.java
@@ -131,12 +131,15 @@ public class SpockMethodParamCompletion  extends 
BaseCompletion {
                 ParserResult pr = request.getParserResult();
                 if(pr != null && pr instanceof GroovyParserResult) {
                     GroovyParserResult gpr = 
(GroovyParserResult)request.getParserResult();
-                    List<AnnotationNode> annotations = 
methodNode.getAnnotations();
-                    if (annotations != null && !annotations.isEmpty()) {
-                        for (AnnotationNode annotation : annotations) {
-                            if 
(annotation.getClassNode().isDerivedFrom(gpr.resolveClassName(UNROLL_CLASS))) {
-                                unroll = annotation;
-                                break;
+                    ClassNode unrollCN = gpr.resolveClassName(UNROLL_CLASS);
+                    if (unrollCN != null) {
+                        List<AnnotationNode> annotations = 
methodNode.getAnnotations();
+                        if (annotations != null && !annotations.isEmpty()) {
+                            for (AnnotationNode annotation : annotations) {
+                                if 
(annotation.getClassNode().isDerivedFrom(gpr.resolveClassName(UNROLL_CLASS))) {
+                                    unroll = annotation;
+                                    break;
+                                }
                             }
                         }
                     }
diff --git 
a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockUtils.java
 
b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockUtils.java
index 6a713d9..218b838 100644
--- 
a/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockUtils.java
+++ 
b/groovy/groovy.editor/src/org/netbeans/modules/groovy/editor/completion/SpockUtils.java
@@ -18,7 +18,6 @@
  */
 package org.netbeans.modules.groovy.editor.completion;
 
-
 import java.util.HashSet;
 import java.util.Set;
 import org.codehaus.groovy.ast.ClassNode;
@@ -35,26 +34,34 @@ import 
org.netbeans.modules.groovy.editor.api.parser.GroovyParserResult;
  * @author Petr Pisl
  */
 public class SpockUtils {
-    
+
     public static boolean isInSpecificationClass(CompletionContext context) {
         
-        Set<String> visited = new HashSet<>();
-        String name;
         ClassNode classNode = context.declaringClass;
         ParserResult pr = context.getParserResult();
         if (pr != null && pr instanceof GroovyParserResult) {
-            GroovyParserResult gpr = (GroovyParserResult)pr;
-            while (classNode != null && !visited.contains(name = 
classNode.getName())) {
-                if 
(classNode.isDerivedFrom(gpr.resolveClassName("spock.lang.Specification"))) {   
//NOI18N
+            GroovyParserResult gpr = (GroovyParserResult) pr;
+            ClassNode specCN = 
gpr.resolveClassName("spock.lang.Specification");   //NOI18N
+            if (specCN != null) {
+                if (classNode.isDerivedFrom(specCN)) {   
                     return true;
                 }
-                visited.add(name);
-                classNode = classNode.getSuperClass();
+            } else {
+                // this branch is mainly for tests, which doesn't have Spock 
on classpath
+                String name;
+                Set<String> visited = new HashSet<>();
+                while (classNode != null && !visited.contains(name = 
classNode.getName())) {
+                    if ("spock.lang.Specification".equals(name)) {  //NOI18N
+                        return true;
+                    }
+                    visited.add(name);
+                    classNode = classNode.getSuperClass();
+                }
             }
         }
         return false;
     }
-    
+
     static boolean isFirstStatement(final CompletionContext request) {
         TokenSequence<GroovyTokenId> ts = 
LexUtilities.getGroovyTokenSequence(request.doc, 1);
 
@@ -63,7 +70,7 @@ public class SpockUtils {
             if (ts.movePrevious()) {
                 while (ts.isValid() && ts.movePrevious() && ts.offset() >= 0) {
                     Token<GroovyTokenId> t = ts.token();
-                    if (!(t.id() == GroovyTokenId.NLS || t.id() == 
GroovyTokenId.WHITESPACE 
+                    if (!(t.id() == GroovyTokenId.NLS || t.id() == 
GroovyTokenId.WHITESPACE
                             || t.id() == GroovyTokenId.SH_COMMENT || t.id() == 
GroovyTokenId.SL_COMMENT
                             || t.id() == GroovyTokenId.BLOCK_COMMENT || t.id() 
== GroovyTokenId.LINE_COMMENT)) {
                         return false;
@@ -75,5 +82,5 @@ public class SpockUtils {
 
         return false;
     }
-    
+
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to