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

GroovyCastException on type handling

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.1
    • 2.3.2
    • None
    • None

    Description

      Executed in a Groovy console, in 2.3.1 version, the script bellow throws a org.codehaus.groovy.runtime.typehandling.GroovyCastException exception, but not with Groovy 2.3.0.

      The script:

      import java.security.AccessController 
      import java.security.PrivilegedAction 
      
      static void injectVariables(final def instance, def variables) { 
          instance.class.declaredFields.each { field -> 
              if (variables[field.name]) { 
                  AccessController.doPrivileged(new PrivilegedAction() { 
                      @Override 
                      public Object run() { 
                          boolean wasAccessible = field.isAccessible() 
                          try { 
                              field.accessible = true 
                              field.set(instance, variables[field.name]) 
                              return null; // return nothing... 
                          } catch (IllegalArgumentException | IllegalAccessException ex) { 
                              throw new IllegalStateException("Cannot set field: " + field, ex) 
                          } finally { 
                              field.accessible = wasAccessible 
                          } 
                      } 
                  }) 
              } 
          } 
      } 
      
      class Test { 
          def p 
      } 
      
      def t = new Test() 
      
      injectVariables(t, ['p': 'q'])
      

      The exception:

      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class ConsoleScript8' with class 'java.lang.Class' to class 'ConsoleScript8' 
          at ConsoleScript8$_injectVariables_closure1.doCall(ConsoleScript8:7) 
          at ConsoleScript8.injectVariables(ConsoleScript8:5) 
          at ConsoleScript8$injectVariables.callStatic(Unknown Source) 
          at ConsoleScript8.run(ConsoleScript8:32
      

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            blackdrag Jochen Theodorou
            bgoetzmann bgoetzmann
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment