code: public static void setMetaClass(GroovyObject self, MetaClass metaClass) {
          // this method was introduced as to prevent from a stack overflow, described in GROOVY-5285
          if (metaClass instanceof HandleMetaClass)
              metaClass = ((HandleMetaClass)metaClass).getAdaptee();
  
          self.setMetaClass(metaClass);
          disablePrimitiveOptimization(self);
      }
docs: Set the metaclass for a GroovyObject.
  @param self the object whose metaclass we want to set
  @param metaClass the new metaclass value
  @since 2.0.0