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

Intercepting an indirectly called method doesn't work.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.6.3, 1.6.4, 1.7-beta-1
    • 4.0.0-beta-1
    • groovy-runtime
    • None
    • Windows XP SP2, JDK 1.5

    Description

      In the code below, the method to be intercepted, callMeToo, is called internally by another method callMe that is explicitly invoked. When a call to callMe is made, only callMe is intercepted, but the internally called method callMeToo is not intercepted.

      InterceptMe.groovy
      class InterceptMe { 
          String callMe(String input) { 
              input = "<<" + input + ">>" 
              callMeToo(input) 
          } 
          
          String callMeToo(String input) { 
              input += "...//\\\\..." 
              println "The input is: $input" 
              
              input 
          } 
          
          static void main(args) { 
              InterceptMe.metaClass.invokeMethod = { name, arg -> 
                  if(name == "callMeToo") { 
                      println "Arguments passed to this method: $arg" 
                  }
                  
                  def actualMethod = InterceptMe.metaClass.getMetaMethod(name, arg) 
                  actualMethod?.invoke(delegate, arg) 
              } 
              
              InterceptMe ime = new InterceptMe() 
              ime.callMe("bbbb") // <-- Doesn't print the arguments 
              ime.callMeToo("bbbb") // <-- Prints the arguments
          } 
      }
      

      Please refer to the following discussion on the Nabble Groovy User Forum for more details:

      http://www.nabble.com/How-to-intercept-an-implicitly-called-Method--td25076161.html

      -Kodeninja

      Attachments

        Activity

          People

            emilles Eric Milles
            kodeninja Robin Sharma
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: