12

For example, if I execute a Groovy script, which modifies the String meta class, adding a method foo()

GroovyShell shell1 = new GroovyShell();
shell1.evaluate("String.metaClass.foo = {-> delegate.toUpperCase()}");

when I create a new shell after that and execute it, the changes are still there

GroovyShell shell2 = new GroovyShell();
Object result = shell2.evaluate("'a'.foo()");

Is there a way to undo all meta class changes after executing the GroovyShell? I tried

shell1.getClassLoader().clearCache();

and

shell1.resetLoadedClasses();

but that did not make a change.

cretzel
  • 19,864
  • 19
  • 58
  • 71

3 Answers3

12

You can use

GroovySystem.metaClassRegistry.removeMetaClass(String.class);

to revert all changes made to the String meta class.

Alternatively you could only change the meta class of a certain String instance, thus not all instances of String would be affected.

Christoph Metzendorf
  • 7,968
  • 2
  • 31
  • 28
  • 1
    Thanks. But what, if I don't know what meta class changes have been made. Think of an application like the Groovy Web Console, where different users execute Groovy scripts. I want these users not to interfere. – cretzel Nov 04 '09 at 12:53
  • 1
    See my answer below, which solves your problem of not knowing which metaclasses have changed. – Sudhir N Feb 12 '16 at 14:29
3

You can use MetaClassRegistryCleaner too.

Before doing some metaclass changes, you can do

MetaClassRegistryCleaner registryCleaner = MetaClassRegistryCleaner.createAndRegister()
GroovySystem.metaClassRegistry.addMetaClassRegistryChangeEventListener(registryCleaner)

And when you want to reset the metaclass changes to the state they were earlier.

You can do

registryCleaner.clean()  
GroovySystem.metaClassRegistry.removeMetaClassRegistryChangeEventListener(registryCleaner)

This way you can reset all the metaclass changes made during the duration.

Sudhir N
  • 4,008
  • 1
  • 22
  • 32
1

I realise that this is a somewhat older question, but it's the first result on Google when I was searching for exactly the same issue.

The solution I chose was to put groovy into a new classloader (by using plexus-classworlds), so when the script is finished, the classloader is disposed (and so any changes to the metaclass are also disposed).

user340535
  • 663
  • 2
  • 6
  • 15