Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8204

@Delegate on arrays causes NPE during compilation

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.11
    • Fix Version/s: 2.4.12
    • Component/s: None
    • Labels:
      None

      Description

      I have stumbled upon this bug partly by accident, and I'm not convinced it's a valid use case, but either way it should not crash the compiler with a NPE.

      Trying to compile the following simple class:

      import groovy.lang.Delegate
      
      class BugsMe {
      
      	@Delegate
      	byte[] file
      }
      

      Results in the following exception:

      >>> a serious error occurred: BUG! exception in phase 'canonicalization' in source unit 'BugsMe.groovy' unexpected NullpointerException
      >>> stacktrace:
      BUG! exception in phase 'canonicalization' in source unit 'BugsMe.groovy' unexpected NullpointerException
      	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1070)
      	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
      	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:537)
      	at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:61)
      	at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:217)
      	at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompile(FileSystemCompiler.java:150)
      	at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompileWithErrorHandling(FileSystemCompiler.java:180)
      	at org.codehaus.groovy.tools.FileSystemCompiler.main(FileSystemCompiler.java:164)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
      	at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
      Caused by: java.lang.NullPointerException
      	at org.codehaus.groovy.ast.ClassHelper.getNextSuperClass(ClassHelper.java:454)
      	at org.codehaus.groovy.ast.ClassHelper.getNextSuperClass(ClassHelper.java:449)
      	at org.codehaus.groovy.ast.tools.GenericsUtils.extractSuperClassGenerics(GenericsUtils.java:444)
      	at org.codehaus.groovy.transform.DelegateASTTransformation.addDelegateMethod(DelegateASTTransformation.java:233)
      	at org.codehaus.groovy.transform.DelegateASTTransformation.visit(DelegateASTTransformation.java:129)
      	at org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:134)
      	at org.codehaus.groovy.transform.ASTTransformationVisitor$2.call(ASTTransformationVisitor.java:178)
      	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1065)
      	... 15 more
      

        Issue Links

          Activity

          Hide
          paulk Paul King added a comment -

          Is it a valid use case? Well, it certainly is an edge case! And correct, it shouldn't crash the compiler.

          Interestingly, a type of byte by itself is allowed though doesn't do anything useful since no methods are found. Also String[] causes a similar error, so it's not just primitive arrays causing grief. I'd be inclined to treat it just like the non-array primitive case. Allow it but not add any methods. Possibly you might expect a length property.

          Show
          paulk Paul King added a comment - Is it a valid use case? Well, it certainly is an edge case! And correct, it shouldn't crash the compiler. Interestingly, a type of byte by itself is allowed though doesn't do anything useful since no methods are found. Also String[] causes a similar error, so it's not just primitive arrays causing grief. I'd be inclined to treat it just like the non-array primitive case. Allow it but not add any methods. Possibly you might expect a length property.
          Hide
          githubbot ASF GitHub Bot added a comment -

          GitHub user paulk-asert opened a pull request:

          https://github.com/apache/groovy/pull/551

          GROOVY-8204: @Delegate on arrays causes NPE during compilation

          You can merge this pull request into a Git repository by running:

          $ git pull https://github.com/paulk-asert/groovy groovy8204

          Alternatively you can review and apply these changes as the patch at:

          https://github.com/apache/groovy/pull/551.patch

          To close this pull request, make a commit to your master/trunk branch
          with (at least) the following in the commit message:

          This closes #551


          commit 8af2cf43dcf41d1614e75c1bb17046351c048472
          Author: paulk <paulk@asert.com.au>
          Date: 2017-05-25T13:37:14Z

          GROOVY-8204: @Delegate on arrays causes NPE during compilation


          Show
          githubbot ASF GitHub Bot added a comment - GitHub user paulk-asert opened a pull request: https://github.com/apache/groovy/pull/551 GROOVY-8204 : @Delegate on arrays causes NPE during compilation You can merge this pull request into a Git repository by running: $ git pull https://github.com/paulk-asert/groovy groovy8204 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/551.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #551 commit 8af2cf43dcf41d1614e75c1bb17046351c048472 Author: paulk <paulk@asert.com.au> Date: 2017-05-25T13:37:14Z GROOVY-8204 : @Delegate on arrays causes NPE during compilation
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user jwagenleitner commented on a diff in the pull request:

          https://github.com/apache/groovy/pull/551#discussion_r118507014

          — Diff: src/main/org/codehaus/groovy/ast/ClassHelper.java —
          @@ -440,12 +440,15 @@ private static boolean hasUsableImplementation(ClassNode c, MethodNode m) {
          /**

          • Returns a super class or interface for a given class depending on a given target.
          • If the target is no super class or interface, then null will be returned.
            + * For a non=primitive array type, returns an array of the componentType's superclass.
              • End diff –

          non-primitive

          Show
          githubbot ASF GitHub Bot added a comment - Github user jwagenleitner commented on a diff in the pull request: https://github.com/apache/groovy/pull/551#discussion_r118507014 — Diff: src/main/org/codehaus/groovy/ast/ClassHelper.java — @@ -440,12 +440,15 @@ private static boolean hasUsableImplementation(ClassNode c, MethodNode m) { /** Returns a super class or interface for a given class depending on a given target. If the target is no super class or interface, then null will be returned. + * For a non=primitive array type, returns an array of the componentType's superclass. End diff – non-primitive
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user asfgit closed the pull request at:

          https://github.com/apache/groovy/pull/551

          Show
          githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/groovy/pull/551
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user paulk-asert commented on a diff in the pull request:

          https://github.com/apache/groovy/pull/551#discussion_r118622384

          — Diff: src/main/org/codehaus/groovy/ast/ClassHelper.java —
          @@ -440,12 +440,15 @@ private static boolean hasUsableImplementation(ClassNode c, MethodNode m) {
          /**

          • Returns a super class or interface for a given class depending on a given target.
          • If the target is no super class or interface, then null will be returned.
            + * For a non=primitive array type, returns an array of the componentType's superclass.
              • End diff –

          fixed the typo, thanks

          Show
          githubbot ASF GitHub Bot added a comment - Github user paulk-asert commented on a diff in the pull request: https://github.com/apache/groovy/pull/551#discussion_r118622384 — Diff: src/main/org/codehaus/groovy/ast/ClassHelper.java — @@ -440,12 +440,15 @@ private static boolean hasUsableImplementation(ClassNode c, MethodNode m) { /** Returns a super class or interface for a given class depending on a given target. If the target is no super class or interface, then null will be returned. + * For a non=primitive array type, returns an array of the componentType's superclass. End diff – fixed the typo, thanks
          Hide
          paulk Paul King added a comment - - edited

          Proposed PR merged. Arrays are now allowed though it isn't a very useful/interesting delegate. Thanks for spotting the issue.

          Show
          paulk Paul King added a comment - - edited Proposed PR merged. Arrays are now allowed though it isn't a very useful/interesting delegate. Thanks for spotting the issue.

            People

            • Assignee:
              paulk Paul King
              Reporter:
              kszafran Krzysztof SzafraƄski
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development