Can I write annotations in Groovy? - groovy

I know I can annotate my classes in Groovy with annotations, but can I write the annotation itself in Groovy (as opposed to just using annotation written in Java)? If so, from what version?

You can define annotations in Groovy.
See more:
http://docs.groovy-lang.org/latest/html/documentation/#_annotation

Related

How to use #CompileStatic for pure groovy scripts in JMeter environment?

I'd like to take advantage of #CompileStatic annotation in my groovy scripts in jmeter environment. It helps a lot to discover issues in compile time.
I already started to use it in my classes but I don't know how to use it in case of plain groovy scripts. For example, I have the script below and there are the log and vars variables which are kind of global variables in JMeter environment. So, eventually they will be used.
If I add the #CompileStatic annotation to the method below IntelliJ paints red everything and compile will fail because the compiler doesn't know what these variables are.
So, the question is how to tell the compiler in a case of a script these variables has type and what type they have, and how an instance will be provided for the script?
I apologize, I'm not a groovy expert at all.
void checkingInputParameters() {
log.info("variable value:" + vars.get("some_variable_name"))
}
checkingInputParameters()
I think you are in the wrong path,
because CompileStatic is a groovy compiler option
let the Groovy compiler use compile time checks in the style of Java then perform static compilation, thus bypassing the Groovy meta object protocol.
JMeter (and I assume your tests in Intellij ) is using a java compiler
I don't think you should mix them for tests.
In JMeter use Cache compiled checkbox/feature
checking Cache compiled script if available

How can I use Groovy classes in other packages without JAR?

I'm pretty new to Groovy (coming from Java), so this may be a stupid question :-)
Nonetheless: I'd like to structure a couple of Groovy scripts using packages. And I'd like to import some general Groovy classes from some other package.
How can I make sure that my Groovy scripts finds the other classes in the other packages? The only classpath related files I can remember are JARs.
If you from java:
Groovy loads classes as java and also includes non-compiled with extension .groovy.
So, you have to place your classes relative to classpath according to their package name.
The command line should be something like this:
groovy -cp "path_to_classes_root" "path_and_name_of_main_groovy_script.groovy"

JAXB Class - XML not getting created as expected

I am using fasterxml-jaxb(2.3) module to generate XML but it seems its not reading Xmlns annotation while creating XML. I have the prefix defined in my package-info as below
#javax.xml.bind.annotation.XmlNs(prefix = "bla"....
Jackson does not apply the prefix however if i user I use JDK's JAXB for marshalling the prefix is applied to the XML
Jackson does not use Package annotations for anything; so while JAXB annotations are supported (if using JAXB annotations module), no annotations are read from package-info.java.
This because Jackson annotation module is based on inheritance (you can add all annotations on super-classes, and they are visible for sub-types).
You may be able to add a mix-in annotation for super-class (or regular annotation), and that would work.

How to understand Groovy's classes as first-class citizens

I am an experienced Java developer, but a novice Groovy programmer, which I am learning at the moment (and it's great so far). As a reference I am reading this document:
http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+Java+developers
That is all fine, except one thing I do not fully understand. The documentation says that classes are first-class citizens and the .class suffix is not needed in Groovy. But if that is the case how can I then refer to an object's type (i.e. class) in Groovy?
Consider the following example:
def o = new Object()
println("$o, ${o.class}")
Which gives me the following output:
java.lang.Object#da479dd, class java.lang.Object
This output is expected and makes sense. But what is the Groovy documentation than referring to when they say that the .class suffix is not needed?
In Groovy and many other dynamic languages everything is an object, including class itself.
Say you have a class Circle in java. You need to call Circle.getClass() for an class object do deal with. In many dynamic languages, class itself does not need to be specified. Say you have a class
class Miu {}
and each later reference to Miu will be referencing to the class object itself
Miu.class
Miu
will both evaluate to the very same object
In other words, Java and earlier in C++ had no eval(), so class definition itself cannot be made into class object directly. OO model in them is more like class oriented programming rather than true object oriented, as classes are subtly not objects. In more recent interpreted dynamic languages classes themselves are directly objects.
You're confusing two different things. You don't need .class when you're referring to some particular class, so if you have a class Foo and you want to refer to it you don't have to type Foo.class, you just type Foo. (That's what the article you're linking to describes.) But when you have some object and you want to know its class you would still use .class (where .class is actually short for invoking the getClass method). Note if you have a map and you want to know its class you have to type out getClass() so it won't think you aren't referring to a map key called 'class'.

Is there a "use strict" for Groovy?

I remember from my Perl days the "use strict" statement that cause the runtime to do extra validations. Is there an equivalent for Groovy?
I do not enjoy being bitten during runtime by what can be detected on compilation, like passing too few arguments to a constructor.
Groovy 2.0 now has optional static type checking. If you place a #groovy.transform.TypeChecked annotation on a class or method, groovy will use strict, Java-like static typing rules.
In addition, there's another annotation #groovy.transform.CompileStatic that is similar, except it goes a step further and actually compiles it without dynamic typing. The byte code generated for these classes or methods will be very similar to straight Java.
These annotations can be applied to an individual class or method:
import groovy.transform.TypeChecked
#TypeChecked
class MyClass {
...
}
You can also apply them globally to an entire project without adding annotations to the source files with a compiler config script. The config script should look something like this:
withConfig(configuration) {
ast(groovy.transform.TypeChecked)
}
Run groovy or groovyc with the -configscript command line option:
groovyc -configscript config.groovy MyClass.groovy
There's more information in the Groovy manual:
http://groovy-lang.org/semantics.html#static-type-checking
http://groovy-lang.org/semantics.html#_static_compilation
Is there an equivalent for Groovy?
Not that I know of.
I do not enjoy being bitten during
runtime by what can be detected on
compilation, like passing too few
arguments to a constructor.
Then Groovy is probably the wrong language for you and you should use something like Java or C# instead. Alternatively, there is a version of Groovy, known as Groovy++ which has much stronger type-checking, but I don't consider it sufficiently mature for production use.
IntelliJ (and possibly other IDEs) provides a lot of warnings about dodgy Groovy code. Although these warnings don't prevent compilation, they almost give you the best of both worlds, i.e. the safety of a static language and the flexibility of a dynamic language
No, there is no such thing, and there can't be. Perl's "use strict" only prevents the use of undeclared variables (and some very Perl-specific things that I don't think have equivalents in Groovy).
In dynamic languages like Groovy, "passing too few arguments to a constructor" is fundamentally not something the compiler can detect, because class definitions can be changed at runtime via metaprogramming. Also, you usually don't have the type information necessary to know what class to look at.
If you want maximum compile-time checks, use a statically typed language with no metaprogramming, i.e. Java.

Resources