hi i have added the following groovy code in the Post-build Actions of jenkins.
import java.util.*
import hudson.model.*
upstreamBuilds = manager.build.getUpstreamBuilds();
upstreamJob = upstreamBuilds.keySet().iterator().next();
lastUpstreamBuild = upstreamJob.getLastBuild();
if(lastUpstreamBuild.getResult().isBetterThan(manager.build.result)) {
lastUpstreamBuild.setResult(manager.build.result);
}
but i get the following error when i execute the above code:
ERROR: Failed to evaluate groovy script.
java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:897)
at java.util.HashMap$KeyIterator.next(HashMap.java:928)
at java_util_Iterator$next.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at Script1.run(Script1.groovy:5)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
at org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:276)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:19)
at hudson.model.AbstractBuild$AbstractRunner.perform(AbstractBuild.java:710)
at hudson.model.AbstractBuild$AbstractRunner.performAllBuildSteps(AbstractBuild.java:685)
at hudson.model.Build$RunnerImpl.post2(Build.java:162)
at hudson.model.AbstractBuild$AbstractRunner.post(AbstractBuild.java:632)
at hudson.model.Run.run(Run.java:1463)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:239)
Recording fingerprints
Since i am new to groovy i have no clue how to proceed?
I believe that is because you are calling next() on an iterator with no elements in it.
I believe this does the same thing, but handles the upStreamBuilds map being empty:
upstreamJob = manager.build.upstreamBuilds.find()
if( upstreamJob != null ) {
lastUpstreamBuild = upstreamJob.key.lastBuild
if( lastUpstreamBuild.result.isBetterThan( manager.build.result ) ) {
lastUpstreamBuild.result = manager.build.result
}
}
PS: You don't need to import java.util.* with groovy
Related
I am trying to define a DSL for building a domain specific query. The DSL is implemented using FactoryBuilderSupport so it looks something like below (abbreviated):
class QueryBuilder extends FactoryBuilderSupport {
{
registerFactory('query', new QueryFactory())
registerFactory('match', new QueryMatchFactory())
registerFactory('obj', new ObjFactory())
}
def propertyMissing(String name) {
//give user meaningful message about the bad 'name' here
}
}
This works fine if I just run it as a groovy script (in IntelliJ or whatever) The script looks something like below:
def bldr = new QueryBuilder()
def queryBuilder = bldr.query() {
match {
obj {
//More stuff in here
}
}
}
Now what I really want is to get the script from a web interface and run it. So my first shot at this is to create a Delegating script. For whatever reason I had to say delegate.query() rather than just query or it ignores my delegate. This is contradictory to the documentation I read.
class DslEvaluator {
def evaluateDsl (dsl) {
QueryBuilder queryBuilder = new QueryBuilder()
def compilerConfiguration = new CompilerConfiguration()
compilerConfiguration.scriptBaseClass = DelegatingScript.class.name
def shell = new GroovyShell(this.class.classLoader, new Binding(), compilerConfiguration)
Script script = shell.parse("""
delegate.query() {
${dsl}
}
""")
script.setDelegate queryBuilder
script.run()
}
}
This works just fine so long as I do not have the propertyMissing Method defined. If I do define it, it gets called for some reason and this is the error I see:
at com.xxx.xxx.site.dsl.QueryBuilder.propertyMissing(QueryBuilder.groovy:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaClassImpl.invokeMissingProperty(MetaClassImpl.java:778)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1731)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3458)
at groovy.util.DelegatingScript.getProperty(DelegatingScript.java:102)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at Script1.run(Script1.groovy:2)
at Script1$run.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)
Just for completeness here is the test case.
#Test
void testEvaluateDsl() {
DslEvaluator dslEvaluator = new DslEvaluator()
dslEvaluator.evaluateDsl("""
match {
obj {
//more stuff in here
}
}
""")
}
So how can I handle adding the propertyMissing() method without losing my ability to build and execute it as a script at runtime?
I guess I can answer my own question at this point.
After looking at things a little closer, I noticed a few things.
FactoryBuilderSupport extends Binding. So to bind extra variables to the script I simply need to call setVariable() on my builder instance. I can get access to the bound variable through the getVariable() method in any of the AbstractFactory methods as they all are provided with an instance of the builder.
I was trying to figure out how to set the resolve strategy on the builder so I did not need delegate.query in my dsl. This was the wrong approach. The FactoryBuilderSupport takes the script in its build method, and handles all of this for me. Once doing things this way it also fixed the issue of not being able to provide the propertyMissing() method. The builder also will call run on the provided script.
At the end of the day this is what I have now.
def evaluateDsl (dsl) {
QueryBuilder queryBuilder = new QueryBuilder()
def shell = new GroovyShell(this.class.classLoader, queryBuilder)
Script script = shell.parse("""
query() {
${dsl}
}
""")
Query query = queryBuilder.build(script)
query.toQueryString()
}
RunWith(PowerMockRunner.class)
#PrepareForTest(StaticCallInvoke.class)
#ContextConfiguration(locations = "file:test/spring/Beans.xml")
class TestClass extends Specification{
#Test
def "Testing staticMocking"() {
setup:
def someObject=new SomeObject();
someObject.someValue=100
PowerMockito.mockStatic(StaticCallInvoke.class)
when:
ClassUnderTest.executeSomething(someObject)
then:
someObject.someValue=110 /*Wrong Value,It says assertion failed. Thats absolutely fine becuase the value should be 100*/
}
}
When I try to change it to the correct value i.e 100, it throws this Exception,
java.lang.NullPointerException: Cannot invoke method leaveScope() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at com.cognizant.awcoe.gamification.rules.helper.executors.RuleExecTest.$spock_feature_0_0(RuleExecTest.groovy:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
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) )
If I change it back to 100, it's all good again(But the assertion fails)
Above test is run using PowerMock 1.5,Spock-0.6 for groovy 1.8.
Not sure what's happening here.
Any help is much appreciated :)
Looks like PowerMock is installing its own JUnit runner, overriding Spock's one. In other words, Spock is no longer in charge of executing this test, and PowerMock obviously can't execute it correctly. Perhaps PowerMock doesn't support custom JUnit runners, in which case it won't work with Spock.
Spock and PowerMock work together nicely if done right, see my repo https://github.com/kriegaex/Spock_PowerMock. You need to do something like
#PrepareForTest([StaticCallInvoke.class])
#ContextConfiguration(locations = "file:test/spring/Beans.xml")
class TestClass extends Specification {
#Rule PowerMockRule rule = new PowerMockRule();
def "Testing staticMocking"() {
// ...
}
}
Im getting a cast exception thrown at the line "handDetailList.each". I don't understand why my code is trying to cast a list to a "Hand" class? It seems to me that sometimes Groovy does strange things with casting....?
private Hand buildHands(List handDetailList) {
def parsedHand = new Hand()
parsedHand.setTableName(handDetailList.get(1))
handDetailList.each {
}
}
I get the following exception (I have edited the exception, line 70 is "handDetailList.each {"):
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object <details of the list, omitted> with class 'java.util.ArrayList' to class 'gameMechanics.Hand' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: gameMechanics.Hand(java.lang.String,........
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:358)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:599)
at advisor.HistoryParser.buildHands(HistoryParser.groovy:70)
at advisor.HistoryParser.this$2$buildHands(HistoryParser.groovy)
at advisor.HistoryParser$this$2$buildHands.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at advisor.HistoryParser.parse(HistoryParser.groovy:57)
at advisor.HistoryParser$parse.call(Unknown Source)
each returns the list that each was called on.
You have said the function returns an object of type Hand, and as Groovy automatically returns the last statement in a method, it is trying to convert the list to an instance of Hand and failing...
What is it you want to return? The parsedHand variable?
Maybe try:
private Hand buildHands(List handDetailList) {
def parsedHand = new Hand()
parsedHand.setTableName(handDetailList.get(1))
handDetailList.each {
}
parsedHand
}
if so.
I want to create gradle custom task. The problem is that sometimes when I change something in source code, gradle shows error that in my code is error with that non existing code. For example I've created local variable timestamp and compiled project. Everything worked fine. But later I changed that variable to final ... TIMESTAMP. After that change gradle shows me error Could not find property 'timestamp'.... How to solve this problem? I assume that gradle has got cache?
Code
class MyCustomTask extends DefaultTask {
private final String TIMESTAMP = System.currentTimeMillis().toString()
public MyCustomTask() {
}
#TaskAction
def build() {
// do something
}
}
Exception
* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating project ':customer'.
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.ja
va:54)
at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:127)
at org.gradle.configuration.BuildScriptProcessor.execute(BuildScriptProcessor.java:36)
at org.gradle.configuration.BuildScriptProcessor.execute(BuildScriptProcessor.java:23)
at org.gradle.configuration.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)
at org.gradle.configuration.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:55)
at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:465)
at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:76)
at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:31)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:142)
at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:113)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:81)
at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.ja
va:64)
at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)
at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:35)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)
at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)
at org.gradle.api.internal.Actions$RunnableActionAdapter.execute(Actions.java:171)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:201)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:174)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:170)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:139)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
at org.gradle.launcher.Main.doAction(Main.java:48)
at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
at org.gradle.launcher.Main.main(Main.java:39)
at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:50)
at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:32)
at org.gradle.launcher.GradleMain.main(GradleMain.java:26)
Caused by: org.gradle.api.tasks.TaskInstantiationException: Could not create task of type 'MyCustomTask'.
at org.gradle.api.internal.project.taskfactory.TaskFactory$1.call(TaskFactory.java:115)
at org.gradle.api.internal.project.taskfactory.TaskFactory$1.call(TaskFactory.java:110)
at org.gradle.api.internal.AbstractTask.injectIntoNewInstance(AbstractTask.java:141)
at org.gradle.api.internal.project.taskfactory.TaskFactory.createTaskObject(TaskFactory.java:110)
at org.gradle.api.internal.project.taskfactory.TaskFactory.createTask(TaskFactory.java:70)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory.createTask(AnnotationProcessingTaskF
actory.java:98)
at org.gradle.api.internal.project.taskfactory.DependencyAutoWireTaskFactory.createTask(DependencyAutoWireTaskFacto
ry.java:39)
at org.gradle.api.internal.tasks.DefaultTaskContainer.create(DefaultTaskContainer.java:53)
at org.gradle.api.internal.project.AbstractProject.task(AbstractProject.java:908)
at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:216)
at org.gradle.api.internal.BeanDynamicObject.invokeMethod(BeanDynamicObject.java:122)
at org.gradle.api.internal.CompositeDynamicObject.invokeMethod(CompositeDynamicObject.java:147)
at org.gradle.groovy.scripts.BasicScript.methodMissing(BasicScript.java:83)
at build_1i1akohldagki7rlpcas5oct58.run(C:\workspace-eclipse-juno\Is2k8\customer\build.gradle:19)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.ja
va:52)
... 30 more
Caused by: groovy.lang.MissingPropertyException: Could not find property 'timestamp' on task ':customer:is2k8Test'.
at org.gradle.api.internal.AbstractDynamicObject.propertyMissingException(AbstractDynamicObject.java:43)
at org.gradle.api.internal.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:35)
at org.gradle.api.internal.CompositeDynamicObject.getProperty(CompositeDynamicObject.java:94)
at pl.ydp.gradle.MyCustomTask_Decorated.getProperty(Unknown Source)
at pl.ydp.gradle.MyCustomTask.<init>(MyCustomTask.groovy:15)
at pl.ydp.gradle.MyCustomTask_Decorated.<init>(Unknown Source)
at org.gradle.api.internal.DependencyInjectingInstantiator.newInstance(DependencyInjectingInstantiator.java:62)
at org.gradle.api.internal.ClassGeneratorBackedInstantiator.newInstance(ClassGeneratorBackedInstantiator.java:36)
at org.gradle.api.internal.project.taskfactory.TaskFactory$1.call(TaskFactory.java:113)
... 44 more
At the end you will find Caused by: groovy.lang.MissingPropertyException: Could not find property 'timestamp' on task ':customer:is2k8Test'.
I am trying to use groovy swing builder's fileChooser with no luck.
When I copy the following example from groovy website:
def openExcelDialog = SwingBuilder.fileChooser(dialogTitle:"Choose an excel file",
id:"openExcelDialog", fileSelectionMode : JFileChooser.FILES_ONLY,
//the file filter must show also directories, in order to be able to look into them
fileFilter: [getDescription: {-> "*.xls"}, accept:{file-> file ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter) {
}
But I got an error message:
groovy.lang.MissingMethodException: No signature of method: static groovy.swing.SwingBuilder.fileChooser() is applicable for argument types: (java.util.LinkedHashMap, ConsoleScript19$_run_closure1) values
You can't use the fileChooser like that outside a SwingBuilder. Instead, you need to just use a normal, non-swingBuilder JFileChooser. Here's a complete working example:
import javax.swing.filechooser.FileFilter
import javax.swing.JFileChooser
def openExcelDialog = new JFileChooser(
dialogTitle: "Choose an excel file",
fileSelectionMode: JFileChooser.FILES_ONLY,
//the file filter must show also directories, in order to be able to look into them
fileFilter: [getDescription: {-> "*.xls"}, accept:{file-> file ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter)
openExcelDialog.showOpenDialog()
Note that the new JFileChooser just ends with the closing parentheses - there's no trailing closure.
OverZealous' solution fails with NPE.
Exception in thread "Basic L&F File Loading Thread" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Unknown Source)
at java.util.regex.Matcher.reset(Unknown Source)
at java.util.regex.Matcher.<init>(Unknown Source)
at java.util.regex.Pattern.matcher(Unknown Source)
at org.codehaus.groovy.runtime.InvokerHelper.matchRegex(InvokerHelper.java:335)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.matchRegex(ScriptBytecodeAdapter.java:722)
at ExcelChooser2$_run_closure2.doCall(ExcelChooser2.groovy:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:884)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at FileFilter_groovyProxy.accept(Script1.groovy:7)
at javax.swing.JFileChooser.accept(Unknown Source)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run0(Unknown Source)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run(Unknown Source)
Solution:
OLD: fileFilter: [getDescription: {-> "*.xls"}, accept:{file -> file ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter
NEW: fileFilter: [getDescription: {-> "*.xls"}, accept:{file -> file.toString() ==~ /.*?\.xls/ || file.isDirectory() }] as FileFilter
[ Win7(x64) Groovy Version: 1.8.3 JVM: 1.6.0_29) ]
I think you need to create a SwingBuilder object first. This worked for me:
def swing = new SwingBuilder()
def dialog = swing.fileChooser(dialogTitle: "Open A File")
if (dialog.showOpenDialog() == JFileChooser.APPROVE_OPTION) {
println dialog.selectedFile
}
Look here for a complete list of properties.