#Builder in Groovy 2.3.0 - groovy

I'm trying to run the simple #Builder example shown on the Groovy 2.3 release page, using IntelliJ Idea 13.1.1.
Having created a vanilla groovy project and pasted the code, I get the following warnings where #Builder is declared on the Person class:
Missed attributes: forClass, prefix, builderClassName, buildMethodName, builderMethodName
(Essentially all the properties which don't have default values.)
The IDE is then also not seeing the builder() method which by default should have been added to the Person class, suggesting the AST hadn't been applied.
When the script is actually executed, it behaves as expected. So why do I see those errors in the IDE?
Update:
If I try to build from Gradle I get the following error during groovy compilation:
Can't load builderStrategy 'groovy.transform.builder.DefaultStrategy' java.lang.ClassCastException: groovy.transform.builder.DefaultStrategy cannot be cast to org.codehaus.groovy.transform.BuilderASTTransformation$BuilderStrategy
# line 3, column 1.
#Builder
If I try to switch to SimpleStrategy instead of the DefaultStrategy I get:
The builderStrategy class 'groovy.transform.builder.SimpleStrategy' on #Builder is not a builderStrategy
# line 4, column 1.
#Builder(builderStrategy = SimpleStrategy)

IntelliJ doesn't build/consider the Groovy AST. (This would be brittle and slow.) So until someone adds support for #Builder to IntelliJ, it won't understand it. I recommend to file a feature request.
The problem with #Builder and Gradle has been identified as a bug in the #Builder implementation. See my answer on the Gradle Forums.
Update: The Groovy bug filing was resolved with Groovy version 2.3.2.

Related

ConcurrentModificationException with ASTTransformation (Groovy)

I'm a contributor of Vert.x for Groovy (https://github.com/vert-x3/vertx-lang-groovy) and recently see a curious behaviour regarding AST Transformation.
Until Groovy 3.0.4 the VertxTransformation class (https://github.com/vert-x3/vertx-lang-groovy/blob/3.9/vertx-lang-groovy-gen/src/main/java/io/vertx/lang/groovy/VertxTransformation.java) was working just fine. When I tried to use Groovy 3.0.5 it started to appear some ConcurrentModificationException.
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
at java.util.ArrayList$Itr.next(ArrayList.java:859)
at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1044)
at io.vertx.lang.groovy.VertxTransformation.visit(VertxTransformation.java:82)
at io.vertx.lang.groovy.VertxTransformation.visit(VertxTransformation.java:77)
at io.vertx.lang.groovy.VertxTransformation.lambda$visit$0(VertxTransformation.java:68)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:647)
at io.vertx.lang.groovy.VertxTransformation.visit(VertxTransformation.java:68)
Below there are two links: the first one shows the project building fine with 3.0.4 and the second one failing with 3.0.5. The only thing different is the Groovy version.
https://travis-ci.org/github/vert-x3/vertx-lang-groovy/builds/712642226 (3.0.4)
https://travis-ci.org/github/aaloise/vertx-lang-groovy/builds/712638741 (3.0.5, the exception appears from line 1818 onwards)
Don't know if something changed in 3.0.5 regarding AST. I even raised an issue (https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-9666). If is not a bug, but an improvement, would like some suggestions for an workaround.

Get rid of import in Groovy Unit Test in IntelliJ

Is it possible to get rid of from import statements in JUnit tests written in Groovy? I know you could do it in groovy scripts in GroovyShell by changing compiler configuration or import customizer.
I like to do same thing but:
IntelliJ should still provide code highlight and auto complete (I think I could do this
with gdsl file feature of IntelliJ)
I could still use IntelliJ's JUnit window to see which tests failed and which are passed.

Could not instantiate global transform class org.spockframework.compiler.SpockTransform

I am trying to use a different location for external jar in soapUI. I updated the soapUI batch file by adding the below line.
set JAVA_OPTS=%JAVA_OPTS% -Dsoapui.ext.libraries="C:\Program Files\Groovy\Groovy-2.1.6\lib"
Now when i open soapUI and try to create an activexobject using scriptom(see below)
import org.codehaus.groovy.scriptom.*
def tdc = new ActiveXObject ('TDApiOle80.TDConnection')
I get the following error, the error seems weird because i know i am using groovy 2.1.6 as you can see from the path.
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Could not instantiate global transform class org.spockframework.compiler.SpockTransform specified at jar:file:/C:/Program%20Files/Groovy/Groovy-2.1.6/lib/spock-core-0.7-groovy-2.0-20120930.020057-22.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation because of exception org.spockframework.util.IncompatibleGroovyVersionException: The Spock compiler plugin cannot execute because Spock 0.7.0-groovy-2.0 is not compatible with Groovy 1.8.0. For more information, see http://versioninfo.spockframework.org Spock location: file:/C:/Program%20Files/Groovy/Groovy-2.1.6/lib/spock-core-0.7-groovy-2.0-20120930.020057-22.jar Groovy location: file:/C:/Program%20Files/SmartBear/soapUI-Pro-4.5.2/lib/groovy-all-1.8.0.jar 1 error
Does anyone know why i am getting this error and what i can do to fix it?
I believe SoapUI (at least 4.5.1) is bundled with Groovy 1.8.0
At least is was back in May this year (2013)
You could try the suggestion posted on the page to upgrade, or I guess you're stuck with 1.8.0 functionality (and the non-2.0 spock dependency)

Cucumber/gradle example not generating report?

I'm investigating using gradle and cucumber together, and found this lovely example in cucumber's github.
So, I cloned the repository and ran it myself. It failed, as it's configured to do, but I couldn't find the HTML or JSON report that it appears to be configured to output. I say appear because I'm brand new to cucumber, but this class would seem to indicate where it'll put it:
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"pretty", "html:build/cucumber-html-report", "json-pretty:build/cucumber-report.json"})
public class RunCukesTest {
}
However, it's not appearing in the build directory after running gradle cucumber.There's no cucumber-html-report directory, not is there a cucumber-report.json file. I'm running it with Java 7 and Gradle 1.6, if it matters.
Ideas? Is this a known issue with the Cucumber/Gradle integration?
The class name changed depending on the version of Cucumber you are using. It changed from json-pretty to json.
When running the 'cucumber' task on this example the generated cucumber report is located at 'build/cucumber-html-report/index.html'. Running the 'test' task fails as it seems that gradle has problems to create the test report for the cucumber created tests (file name contains spaces) I need to dig a bit into this to see how this can be fixed in gradle.
cheers,
René
The cucumber-jvm-example doesn't do reporting using gradle cucumber, but does do it with gradle test. However, gradle test will have a couple issues, namely showing a "null" test of sorts.
A workaround to this, if need be, is to add the formats to the args of the javaexec that runs cucumber. For example, in build.gradle:
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--format', 'html:cucumber-html-report', '-f', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
}
I had an error with that very same line (taken from this tutorial).
In order to resolve, had to change the third parameter from "json-pretty" to just "pretty"
So this is my final code line:
#CucumberOptions(format = {"pretty", "html:target/cucumber-html-report", "pretty:target/cucumber-report.json"})
BTW,
#Cucumber.Options is deprecated, we should use CucumberOptions

Groovy and IntelliJ - getting code compiled

I have IntelliJ 12 and some groovy code (along with a pile of java code) in a project.
In intelliJ, i can see class A's import of some groovy code, and i have also included the library that has that code.
However, while the package itself is in one colour (for the import), the actual class being imported is in red, which implies an issue of some sort. Hovering the mouse over it reveals no issue though.
When i run a "make" or a "rebuild project" is where the problems start - i get
Groovyc: unable to resolve class com.blah.blah.blah.A
How can i resolve this?
Currently, my project setup is like so:
Under "Libraries" in (Project Structure -> Project Settings -> Libraries) I have:
the jar file with all the groovy code
the src jar file with all the groovy code
In the "Modules" section i have the - well, i don't know what to call it, the column isn't labelled - the library name from the libraries section associated with the src and class files, and the little "export" button beside it is ticked.
Incidentally, opening the class in intelliJ never shows the source code, which given the source is included struck me as weird.
Is there anything else I should need to do?
I've worked this one out, but if anybody knows why groovy cannot be in the "Resource Patterns" list and wants an upvote, do chime in
Oh, right.
I removed the !?*.groovy entry from the list of, um, entries in the File : Settings -> Compiler -> Resource Patterns thingy.
It doesn't seem to matter if "use external build" is on or off for this, but the !?*.groovy; entry cannot be there.
I wonder if anybody knows why?
I had the same problem and had to Add Framework Support and add Groovy to the project to get round this problem.
I created the project using gradle.
I just got your question in my Google results as I had a similar issue. My problem was that I was able to get the groovy code in my IntelliJ 12 project to compile ok, but it wasn't getting wired in properly when I tried to run unit tests within the IDE.
After some investigation, I uncovered that groovy and logback libraries were all set up in the project to be available in the runtime stage of the Maven build of the project, but that resulted in them not being available in the test stage. To fix this, I ended up manually updating the groovy-all and the logback libraries scope from runtime to provided under File->Project Structure->Modules->Dependencies. This allowed me to both compile and test within the IDE while including the Groovy modules as well as the Java modules.
Perhaps you had something similar going on in your project?
Six years later, I also just got this question near the top of my search results.
In my project my Unable to load class 'groovy.text.SimpleTemplateEngine' problem was actually due to a codenarc issue. I was able to resolve the issue by adding the following to build.gradle:
// codenarc version issue work-around
configurations.codenarc {
resolutionStrategy.eachDependency { DependencyResolveDetails d ->
if (d.requested.group == 'org.codehaus.groovy') {
d.useVersion '2.4.7'
}
}
}

Resources