I try to dynamically change and add methods of a class defined in a groovy script from another groovy script but cannot figure out why it works if I use the classname directly in .metaClass. but not if I load the class using the GroovyClassLoader(which I need to do!).
In one file 'MyTest.groovy' I have
class MyTest {
public void setUp() {
println "SET UP"
}
public void tearDown() {
println "TEAR DOWN"
}
public void testA() {
println "testA"
}
}
and another file 'suite.groovy' contains
#!/usr/bin/env groovy
public class MyTestSuite {
// For debug
public static printClassLoader(Class cls) {
ClassLoader loader = cls.classLoader
while (loader) {
println loader.class
println loader.URLs.join("\n")
println "\n\n"
loader = loader.parent
}
}
public static void suite() {
// First method to define my class (change/addition of methods works)
//Class testClass = MyTest
// Second way to define my class (change/addition of methods doesn't work)
ClassLoader parent = MyTestSuite.class.getClassLoader();
GroovyClassLoader gcl = new GroovyClassLoader(parent);
Class testClass = gcl.parseClass(new File("MyTest.groovy"));
printClassLoader(testClass)
testClass.metaClass.setUp = {-> println "I'm your new setUp()" ;}
testClass.metaClass.newTest = {-> println "I'm your new newTest()" ; }
}
}
MyTestSuite.suite()
MyTest aTest = new MyTest()
aTest.setUp()
aTest.newTest()
With the first method I get the expected result:
I'm your new setUp()
I'm your new newTest()
But with the second one I get
SET UP
Caught: groovy.lang.MissingMethodException: No signature of method: MyTest.newTest() is applicable for argument types: () values: []
Possible solutions: testA(), getAt(java.lang.String), wait(), inspect(), every()
at suite.run(suite.groovy:34)
Interestingly modifying an existing method doesn't work but there is no exception but trying to add a new one throws one.
I reckon it has to do something with the class loaders used but couldn't figure what exactly and what to change!
For the first version here are the class loaders called:
class groovy.lang.GroovyClassLoader$InnerLoader
class groovy.lang.GroovyClassLoader
class org.codehaus.groovy.tools.RootLoader
class sun.misc.Launcher$AppClassLoader
class sun.misc.Launcher$ExtClassLoader
For the second version:
class groovy.lang.GroovyClassLoader$InnerLoader
class groovy.lang.GroovyClassLoader
class groovy.lang.GroovyClassLoader$InnerLoader
class groovy.lang.GroovyClassLoader
class org.codehaus.groovy.tools.RootLoader
class sun.misc.Launcher$AppClassLoader
class sun.misc.Launcher$ExtClassLoader
Any idea welcome!
Franck
The new GroovyClassLoader is not the Groovy classloader of the current thread.
It will be disposed after the suite() method finishes - along with its own Class and MetaClass associations.
However, this works:
Class testClass = gcl.parseClass(new File("MyTest.groovy"));
testClass = Class.forName(testClass.getName())
Related
Question
How can I overwrite a method in Groovy if the class implements an interface? If the class does not implement an interface I can overwrite the method, but if the class implements an interface the method does not overwrite.
Example without implement
interface IA {
void abc()
}
class A {
void abc() {
println "original"
}
}
x= new A()
x.abc()
x.metaClass.abc = {-> println "new" }
x.abc()
The output of this is
original
new
Example with implement
Consider the following example where class A implements interface IA
interface IA {
void abc()
}
class A implements IA {
void abc() {
println "original"
}
}
x= new A()
x.abc()
x.metaClass.abc = {-> println "new" }
x.abc()
The output in this case is
original
original
As mentioned in the comments, looks like this is a pretty embarrassing bug, unfortunately.
But depending on your actual scenario, it may be possible to work around the problem.
Use Groovy #Category
If you can control where the method will be called, then you can use a Groovy Category to replace the method within a block:
x= new A()
x.abc()
#Category(A)
class ReplaceAbc {
void abc() {
println 'new'
}
}
use(ReplaceAbc) {
x.abc()
}
Replace the instance with an anonymous sub-type of the original type
If you can re-assign the variable, then this is an obvious way to override the method:
x= new A()
x.abc()
x = new A() {
#Override
void abc() {
println 'new'
}
}
x.abc()
How can I overwrite a method in Groovy if the class implements an
interface?
There are a few ways to do it. One is with runtime extension methods.
See the project at github.com/jeffbrown/victorchoymetaprogramminginterface.
lib/src/main/groovy/victorchoymetaprogramminginterface/IA.groovy
package victorchoymetaprogramminginterface
interface IA {
String abc()
}
lib/src/main/groovy/victorchoymetaprogramminginterface/A.groovy
package victorchoymetaprogramminginterface
class A implements IA{
#Override
String abc() {
'A.abc'
}
}
lib/src/main/groovy/victorchoymetaprogramminginterface/extensions/SomeInterfaceExtensionMethods.groovy
package victorchoymetaprogramminginterface.extensions
import victorchoymetaprogramminginterface.IA
class SomeInterfaceExtensionMethods {
static String abc(IA ia) {
'SomeInterfaceExtensionMethods.abc'
}
}
lib/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
moduleVersion=1.0
moduleName=Demo Extensions
staticExtensionClasses=victorchoymetaprogramminginterface.extensions.SomeInterfaceExtensionMethods
The following test passes:
lib/src/test/groovy/victorchoymetaprogramminginterface/TestInterfaceExtensions.groovy
package victorchoymetaprogramminginterface
import spock.lang.Specification
class TestInterfaceExtensions extends Specification {
def "test interface extensions"() {
setup:
def obj = new A()
expect:
obj.abc() == 'SomeInterfaceExtensionMethods.abc'
}
}
I want to define a BarException inside of class Foo for namespacing reason. This is what my Foo class looks like:
package org.company.utils
class Foo {
class BarException extends RuntimeException { }
def raise() {
def r = new RuntimeException("BOOM")
throw new BarException(r)
}
}
This is how I would have liked it to work:
void testFoo() {
shouldFail(Foo.BarException) {
def foo = new Foo()
foo.raise()
}
}
But the test failed with:
1) testFoo(test.FooTestCase)java.lang.AssertionError: Closure
test.FooTestCase$_testFoo_closure1#3eb25e1a should have failed with an
exception of type org.company.utils.Foo$BarException, instead got
Exception groovy.lang.GroovyRuntimeException: Could not find matching
constructor for:
org.company.utils.Foo$BarException(org.company.utils.Foo,
java.lang.RuntimeException)
I have tried instantiating BarException using new BarException(this, r), and putting groovy.transform.InheritConstructors around BarException, but nothing works for the compiler.
What's the problem?
Try making BarException static and add #InheritConstructors.
import groovy.transform.InheritConstructors
class Foo {
#InheritConstructors
static class BarException extends Exception { }
def raise() {
Throwable r = new RuntimeException("BOOM")
throw new BarException(r)
}
}
From the groovy docs:
The usage of static inner classes is the best supported one. If you
absolutely need an inner class, you should make it a static one.
Recently encountered with this problem- when i had two class in one groovy class if main method class is not on the top i encountered this problem.
class Book {
private String name
void setName(String bookName) {
name=bookName
print "Book Name => "+bookName+"\n";
}
String getName(){
return name;
}
}
class TestClass {
static main(args) {
Book t = new Book();
t.setName("First Book");
print "book title=>"+t.getName()+"\n"
}
}
But if change the order of these two class than there is no error, does it mean main method class should be on top in Groovy ?
Yes, order of classes matter in a groovy script. If you parse a groovy script and check its class name, it would the top level class and not the one with main method or one that have same name as the name of the file. It could be a concrete class, an abstract class, an enum, an interface or trait.
Lets see your case. We are going to put your code inside a GString and then will try to parse it using our own GroovyClassLoader.
String script = """
class Book {
private String name
void setName(String bookName) {
name=bookName
print "Book Name => "+bookName+"\\n";
}
String getName(){
return name;
}
}
class TestClass {
static main(args) {
Book t = new Book();
t.setName("First Book");
print "book title=>"+t.getName()+"\\n"
}
}
"""
GroovyClassLoader loader = new GroovyClassLoader()
GroovyCodeSource codeSource = new GroovyCodeSource(script, "MyClass", GroovyShell.DEFAULT_CODE_BASE)
println loader.parseClass(codeSource)
When you will execute this code it will print class Book. Because this is the first available class in your script.
The exception you are getting is because that your top level class doesn't have a main method and neither your script have something to execute after you have loaded your classes. One solution is to move TestClass at top or just add another line at the end of the file TestClass.main() and it will execute without any issues.
I have a groovy script that defines a method which throws an exception.
Using AST Transformations I generate at compile time a new class.
Then I copy this method from the script to this new class and make the class available at runtime.
When at runtime I create a new object of the new class and call the method in stack trace I can see references to Script1 class instead of the new generated class.
Exception in thread "main" java.lang.RuntimeException
at MyGeneratedClass.myMethod(Script1.groovy:4)
at MyGeneratedClass$myMethod.call(Unknown Source)
at scripttest.ExTest.main(ExTest.groovy:35)
Is there anything I can do to change it so I don't see this Script1.groovy class in stack trace but the new class and line number within it?
My code:
class ExTest {
public static void main(String[] a) {
String script = '''
def myMethod() {
throw new RuntimeException()
}
'''
def config = new CompilerConfiguration()
config.addCompilationCustomizers(new MyCompilerConfiguration())
ClassLoader classLoaderToUse = new GroovyClassLoader()
GroovyShell shell = new GroovyShell(classLoaderToUse, new Binding(), config)
Script parsedScript = shell.parse(script)
def generatedClass = parsedScript.class.fields.find {it.name == 'myGeneratedClassField'}.type
def generated = generatedClass.newInstance()
generated.myMethod()
}
}
class MyCompilerConfiguration extends CompilationCustomizer {
MyCompilerConfiguration() {
super(CompilePhase.CONVERSION)
}
#Override
void call(SourceUnit source, GeneratorContext context, ClassNode currentClassNode) throws CompilationFailedException {
def newClassAst = new AstBuilder().buildFromSpec {
classNode('MyGeneratedClass', ClassNode.ACC_PUBLIC) {
classNode java.lang.Object
interfaces { classNode GroovyObject }
mixins { }
}
}
ClassNode myGeneratedClassNode = newClassAst[0]
source.getAST().addClass(myGeneratedClassNode)
currentClassNode.addField('myGeneratedClassField', Opcodes.ACC_PUBLIC, myGeneratedClassNode, new EmptyExpression())
MethodNode myMethodNode = source.getAST().methods.find {it.name == 'myMethod'}
myGeneratedClassNode.addMethod(myMethodNode.name, Opcodes.ACC_PUBLIC, myMethodNode.returnType, myMethodNode.parameters, myMethodNode.exceptions, myMethodNode.code)
}
}
Your stacktrace is correct, the Script1.groovy:4 you see is not the name of a class, but the name of the file which has generated this class, which is, in your case, a Groovy File.
This name come from the CodeSource instance present in the CompilationUnit associated with the ClassNode. You can change it by using a GroovyCodeSource in methods like GroovyShell.parse or GroovyClassLoader.parseClass, but I think it's a really bad idea ! (security, debugging and other thinks are related to this object)
I have a set of groovy scripts in package hierarchy. I have 1 main script, from which I want to call others. For example I have these scripts (with public classes/interfaces of the same name in them):
package.MainScript
package.MyInterface;
package.utils.MyInterfaceImpl1 //implements MyInterface
package.utils.MyInterfaceImpl2 //implements MyInterface
Is there a way to call one script from the other without knowing called class name at compile time? I mean to do something like dynamic class loading like:
class MainScript {
public static void main (String[] args) {
MyInterface instance = Class.forName("package.utils.Util1");
}
}
Yeah! Groovy is a dynamic language. You can create class instance dynamically.
package.MyInterface
class MyInterfaceImpl1 {
def greet() {
"Hello"
}
}
package.MyInterface
class MyInterfaceImpl2 {
def greet() {
"Hi!"
}
}
def name = 'MyInterfaceImpl1' // Choose whatever you want at runtime
def className = Class.forName("MyInterface.$name")
def instance = className.newInstance()
assert instance.greet() == 'Hello'