withDefault找不到类

时间:2012-09-13 14:49:15

标签: groovy

有:

class Task {
    Integer code
    String name
}

def classDefinition = """
            package untitled24

            class TasksCommand {
                List tasks = [].withDefault { new Task() }
            }
            TasksCommand
        """

def shell = new GroovyShell(this.class.classLoader)
Class<?> definedClass = shell.evaluate(classDefinition)

执行结果是:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 5: unable to resolve class Task 
 @ line 5, column 47.
   List tasks = [].withDefault { new Task()
                             ^

有什么想法吗? (groovy版本是1.8.8)

1 个答案:

答案 0 :(得分:0)

这是因为您的任务定义不在包中,因此untitled24中的代码无法找到该类...

如果您将上述内容更改为:

package woo

class Task {
  Integer code
  String name
}

def classDefinition = """package untitled24
                        |
                        |class TasksCommand {
                        |  List tasks = [].withDefault { new woo.Task() }
                        |}
                        |TasksCommand""".stripMargin()

def shell = new GroovyShell(this.class.classLoader)
Class<?> definedClass = shell.evaluate(classDefinition)

它应该有用......(为了可读性添加stripMargin