title: Groovy Binding: Cannot cast object with class 'custompackage.CustomClass' to class 'custompackage.CustomClass'
body: <p>Using Groovy Binding to execute scripts from a main controller and attempting to pass a custom object, I get the error mentioned in the title.</p>
  
  <pre><code>Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'custompackage.CustomClass@60099951' with class 'custompackage.CustomClass' to class 'custompackage.CustomClass'
  </code></pre>
  
  <p>Here's the relevant code:</p>
  
  <pre><code>// Controller.groovy
  import custompackage.CustomClass
  CustomClass test = new CustomClass()
  
  def binding = new Binding()
  def engine = new GroovyScriptEngine('./src')
  
  binding.setProperty("test", test)
  
  engine.run("CustomScript.groovy", binding)
  </code></pre>
  
  <p>The file being run with above:</p>
  
  <pre><code>// CustomScript.groovy
  import custompackage.CustomClass
  CustomClass t 
  
  if(!binding.variables.containsKey("test")){
      t = new CustomClass()
  } else {        
      t = test
  }
  </code></pre>
  
  <p>I'm defining the <code>CustomClass t</code> at the beginning for the purpose of autocomplete in my IDE. When running as <code>def t</code> it works fine.</p>
  
  <p>I know the object is being passed correctly, due to the exception (and further printing of the object)</p>
  
  <p>The error occurs on <code>t = test</code></p>
  
  <p>Why is Groovy trying to cast an object of the same type to it's type, and then failing to do so? And is there a fix that will still allow me to keep the statically typed <code>t</code>?</p>
  
  <p>Thanks!</p>