Question

I have following groovy program -

def people = sql.dataSet('players')
def d = people.findAll { true }
print d.rows()

I get an exception at the print statement.

groovy.lang.GroovyRuntimeException: DataSet unable to evaluate expression. AST not available for closure: in.kshitiz.pgnimport.dao.PlayerDao$_get_closure1. Is the source code on the classpath?
    at groovy.sql.DataSet.visit(DataSet.java:300)
    at groovy.sql.DataSet.getSqlWhereVisitor(DataSet.java:283)
    at groovy.sql.DataSet.getSqlWhere(DataSet.java:230)
    at groovy.sql.DataSet.getSql(DataSet.java:257)
    at groovy.sql.DataSet.rows(DataSet.java:332)
    at groovy.sql.DataSet$rows.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at in.kshitiz.pgnimport.dao.PlayerDao.get(PlayerDao.groovy:13)
    at in.kshitiz.pgnimport.dao.PlayerDao$get.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at in.kshitiz.pgnimport.dao.PlayerDaoTest.testGet(PlayerDaoTest.groovy:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

The exception is not very helpful. What does it mean?

Was it helpful?

Solution

Simply add your groovy DAO source to the classpath to fix the problem.

In this mailing list, Guillaume Laforge says:

There's a twist in the usage of the Sql class.

It needs your Groovy class source code available -- like when running groovy myScript.groovy.

So for instance, when a class is pre-compiled, the "class node" won't be available anymore, as it's not stored in the bytecode.

That may be the issue you're facing here.

The class node is needed for the findall {} part, where we're visiting the AST for creating an adhoc query.

From Groovy in Action (Chapter 10, page 343, para 4) -

DataSet implementation fetches Groovy’s internal representation of the closure’s code. This internal representation is called the Abstract Syntax Tree (AST) and was generated by the Groovy parser. By walking over the AST (with a Visitor pattern), the DataSet implementation emits the SQL equivalent of each AST node.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top