Why is this Groovy code trying to cast? - groovy

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.

Related

Possibility to disable the property syntax for accessing a getter in groovy?

Let's I have a groovy class like:
class SomeClass {
String myProperty = 'foo'
}
Usually in groovy is will be totally valid to access the value using the property name or the getter - which usually gives the same result for SomeClass:
SomeClass someClass = new SomeClass()
assert someClass.myProperty == 'foo'
assert someClass.getMyProperty() == 'foo'
However - due to a flaw in the Jenkins Pipeline implementation - sometimes(!) you are forced to use the getter - as the plain property access will not work (when using some class hierarchy), see: JENKINS-47143. Bad thing is that the same code may work for some jobs while it doesn't for others:
SomeClass someClass = new SomeClass()
assert someClass.myProperty == 'foo' // sometimes throws 'property not found' error
assert someClass.getMyProperty() == 'foo'
Now I already have couple of unit tests for our Jenkins shared library - but what is missing would be a way to detect a property access, in short: A way to prohibit the property access so the unit tests will already complain in advance.
The following code:
class SomeClass {
String myProperty = 'foo'
}
SomeClass.metaClass.getProperty = { String name ->
throw new RuntimeException("tried to get property ${name}, property access only allowed via getXX() methods")
}
def s = new SomeClass()
println(s.myProperty) // a
println(s.getMyProperty()) // b
will throw an exception for line a but not throw an exception for line b. I suspect this will not be possible if SomeClass was written in java, but assuming a groovy class this could be a way to accomplish what you want.
Running the above will result in:
─➤ groovy solution.groovy
Caught: java.lang.RuntimeException: tried to get property myProperty, property access only allowed via getXX() methods
java.lang.RuntimeException: tried to get property myProperty, property access only allowed via getXX() methods
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...

Groovy. How to ignore method call if parameter is null? Is it possible to use null safe operator for the method in my case?

My groovy code is bellow:
String start
String startParam
String end
String endParam
String rule
Period period
String exDates
PeriodList expected = new PeriodList(true)
// init variables...
VEvent event = new ContentBuilder().vevent {
dtstart(start, parameters: parameters() { value(startParam) })
dtend(end, parameters: parameters() { value(endParam) })
rrule(rule)
exdate(exDates)
}
It works correctly in general, but if some of parameters were not defined with non null value I get an exception:
java.lang.RuntimeException: Failed to create component for 'exdate' reason: java.lang.reflect.InvocationTargetException
at groovy.util.FactoryBuilderSupport.createNode(FactoryBuilderSupport.java:719)
at groovy.util.FactoryBuilderSupport.dispatchNodeCall(FactoryBuilderSupport.java:855)
at groovy.util.FactoryBuilderSupport.doInvokeMethod(FactoryBuilderSupport.java:779)
at groovy.util.FactoryBuilderSupport.invokeMethod(FactoryBuilderSupport.java:499)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeOnDelegationObjects(ClosureMetaClass.java:423)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:346)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at net.fortuna.ical4j.model.VEventRecurrenceParametrizedTest$_test_closure2.doCall(VEventRecurrenceTest.groovy:79)
Is it possible to protect the property call (ignore one if variable is null)? I checked documentation, there is null-safe-operator. How could I use one for this case for example?
exdate(exDates)
exdate should not be called if exDates is null.
if I try
exdate(this.?exDates)
it won't help because null safe operator will not protect method call if value is null, beside that this code exdate(this.?exDates) has compile error.
Just check for it being null or empty before making the call:
VEvent event = new ContentBuilder().vevent {
dtstart(start, parameters: parameters() { value(startParam) })
dtend(end, parameters: parameters() { value(endParam) })
rrule(rule)
if (exDates) exdate(exDates)
}

TypeInitializationException/ArgumentException when referencing initialized variable

I just received an exception when I try to reference a static variable in another class, which is also statically initialized. This worked before, and for some reason it fails now. The only changes I made were resetting Visual Studio (2010) to its default setting, which I can't imagine to be the reason for this. Any other code I added didn't touch any of the affected parts either.
This is my code
WinForms class 'MainForm':
partial class MainForm : Form
{
// ...
private RefClass convMan;
private Dictionary<EnumType, string> LogNames = RefClass.LogNames;
// ...
public MainForm() { .... }
}
Referenced class 'RefClass':
class RefClass
{
// ...
public enum EnumType { TypeOne = 0, TypeTwo = 1, TypeThree = 2 };
public static Dictionary<EnumType, string> LogNames = new Dictionary<EnumType, string>()
{
{ EnumType.TypeOne, "Text0" },
{ EnumType.TypeTwo, "Text1" },
{ EnumTypy.TypeThree, "Text2" }
};
}
The error I get now is (translated from German):
An unhandled exception of type "System.TypeInitializationException" occurred.
Additional information: The type initializer for "RefClass" threw an exception.
which has the InnerException
System.ArgumentException
So, as far as I'm concerned, my static dictionary should be initialized once it gets accessed, thus when my Form class references it. I tried debugging to see if the static dictionary is initialized before it gets referenced in the Form class, which is not the case. Also, when I stop at a breakpoint for the reference line, the variable LogNames is null.
I'm really confused as to why this happens, it all worked before.
I found my error, the exceptions I got were quite misleading though. It was a problem with a different dictionary than the one I referenced. It probably didn't get initialized in the first place because something before that failed (If someone can clear this up, please feel free to do so!). Basically what I did wrong was using a two-directional dictionary and adding a value twice. This should normally produce a normal exception, but since it was done statically it got wrapped into a TypeInitializationException. I had a deeper look into the exact stacktrace of the inner exception and found where the exception originated from. Maybe this helps someone in the future...
I had a simular issue getting the same exception. Found that my static constructor for my utility class was generating the exception. Took some time locating since the description of the exception was misleading.
As #Yeehaw mentioned, it appears that the exception gets wrapped, so the common denominator here I would say is that the class/object is static.

Groovy: WSClient throwing JAXBException

I am trying to call a simple public Web Service with WSClient in a Groovy script, but it explodes when initializing ...
TestService.groovy:
#Grab(group='org.codehaus.groovy.modules', module='groovyws', version='0.5.2')
import groovyx.net.ws.WSClient
def proxy = new WSClient("http://www.w3schools.com/webservices/tempconvert.asmx?WSDL", this.class.classLoader)
proxy.initialize();
def result = proxy.CelsiusToFahrenheit(0)
println "You are probably freezing at ${result} degrees Farhenheit"
The error message:
SEVERE: Could not compile java files for http://www.w3schools.com/webservices/tempconvert.asmx?WSDL.
Caught: java.lang.IllegalStateException: Unable to create JAXBContext for generated packages: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or jaxb.index
java.lang.IllegalStateException: Unable to create JAXBContext for generated pack
ages: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: jav
ax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or j
axb.index
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:343)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:196)
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:175)
at groovyx.net.ws.AbstractCXFWSClient.createClient(AbstractCXFWSClient.java:229)
at groovyx.net.ws.WSClient.initialize(WSClient.java:108)
at groovyx.net.ws.IWSClient$initialize.call(Unknown Source)
at TestService.run(TestService.groovy:5)
Caused by: javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or jaxb.index - with linked exception:
[javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.classor jaxb.index]
at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:340)
... 6 more
Caused by: javax.xml.bind.JAXBException: "org.tempuri" doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:197)
... 7 more
Any hint? Why should I have a jaxb.index?
Just discovered that the problem occurs with Java 1.7 (jdk1.7.0_21)... it's OK when running with Java 6 (jdk1.6.0_31)
Any hint to work with Java 7?
As noted on the GroovyWS page, GroovyWS is currently dormant. You could do the same thing (albeit with a wordier syntax) using the groovy-wslite library:
#Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='0.8.0')
import wslite.soap.*
def client = new SOAPClient('http://www.w3schools.com/webservices/tempconvert.asmx')
def response = client.send(SOAPAction:'http://tempuri.org/CelsiusToFahrenheit') {
body {
CelsiusToFahrenheit('xmlns':'http://tempuri.org/') {
Celsius('0')
}
}
}
def result = response.CelsiusToFahrenheitResponse.CelsiusToFahrenheitResult.text()
println "You are probably freezing at ${result} degrees Farhenheit"
Note that this requires you to look at the WSDL to get the SOAP message namespace, unlike the GroovyWS version of the code. But it works!

Gradle remembers old source code

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'.

Resources