Groovy static methods with dots - groovy

Why do I have a compilation error for this code:
class SomeList {
final String field
SomeList(String field) {
this.field = field
}
static SomeList "Regular name of method"() {
return new SomeList("Regular name of method")
}
static SomeList "Name with.dot"() {
return new SomeList("Name with.dot")
}
}
class SomeListTests {
#Test
def "some list test"() {
//given
SomeList list = SomeList()
//when
list."Regular name of method"()
//then
//compilation error
}
}
Error message:
Error:Groovyc: While compiling tests of example-project_test: BUG! exception in phase 'semantic analysis' in source unit '/home/alex/Projects/example-project/src/test/groovy/SomeListTests.groovy' Problem loading class SomeList
I did not find any restrictions in the documentation for method names as String.
When i create main method with GroovyShell and try to start script with this method it compiles.
class Main {
public static void main(String[] args) {
GroovyShell shell = new GroovyShell()
shell.run(new File("path/to/Script.groovy"), Collections.emptyList())
println "Everything is cool"
}
}
And this is Script.groovy:
SomeList."Regular name of method"()
SomeList."Name with.dot"()

I create Test.groovy file as Groovy Script file in IDEA:
import org.junit.Test
class SomeList {
final String field
SomeList(String field) {
this.field = field
}
static SomeList "Regular name of method"() {
return new SomeList("Regular name of method")
}
static SomeList "Name with.dot"() {
return new SomeList("Name with.dot")
}
}
class SomeListTests {
#Test
void "some list test"() {
//given
SomeList list = new SomeList()
//when
println list."Regular name of method"()
//then
println list."Name with.dot"()
}
}
And it works fine for me. I'm using Groovy 2.4.15.
The output is:
SomeList#cb51256
SomeList#59906517

Related

How to override Groovy variable and method using anonymous class?

I have the following code. I have an abstract JobParams, a class extending that abstract GradleJobParams, and a gjp variable with value using anonymous class declaration.
I want to test the overriding behavior of groovy. I can override the method setupRoot() but not the property testVar, why is that?
Tested on: https://groovyconsole.appspot.com/script/5146436232544256
abstract class JobParams {
int root
def testVar=1
def setupRoot () {
println("The root");
}
def printTestVar () {
println("The testVar:" + testVar);
}
}
class GradleJobParams extends JobParams {
}
def gjp = [
testVar:3,
setupRoot:{
println("Override root");
}
] as GradleJobParams;
println("Starting");
gjp.printTestVar();
gjp.setupRoot();
The result is:
Starting
The testVar:1
Override root
Java (and thus Groovy) does not support overriding fields from the parent class with subclassing. Instead, it uses a mechanism called hiding fields:
Hiding Fields
Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super, which is covered in the next section. Generally speaking, we don't recommend hiding fields as it makes code difficult to read.
Source: https://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html
It can be simply illustrated with the following example in Java:
final class SubclassHiddingFieldExample {
static abstract class A {
int value = 10;
void printValue1() {
System.out.println(value);
}
void printValue2() {
System.out.println(this.value);
}
void printValue3() {
System.out.println(((B)this).value);
}
}
static class B extends A {
int value = 12;
}
public static void main(String[] args) {
final B b = new B();
b.printValue1();
b.printValue2();
b.printValue3();
}
}
Output:
10
10
12
As you can see, only printValue3 prints out 3, because it cast this explicitly to B class.
Now, if you look at the decompiled bytecode of your JobParams class, you can see that the printTestVar method code is an equivalent of the following Java code:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import org.codehaus.groovy.runtime.callsite.CallSite;
public abstract class JobParams implements GroovyObject {
private int root;
private Object testVar;
public JobParams() {
CallSite[] var1 = $getCallSiteArray();
byte var2 = 1;
this.testVar = Integer.valueOf(var2);
MetaClass var3 = this.$getStaticMetaClass();
this.metaClass = var3;
}
public Object setupRoot() {
CallSite[] var1 = $getCallSiteArray();
return var1[0].callCurrent(this, "The root");
}
public Object printTestVar() {
CallSite[] var1 = $getCallSiteArray();
return var1[1].callCurrent(this, var1[2].call("The testVar:", this.testVar));
}
public MetaClass getMetaClass() {
MetaClass var10000 = this.metaClass;
if (var10000 != null) {
return var10000;
} else {
this.metaClass = this.$getStaticMetaClass();
return this.metaClass;
}
}
public void setMetaClass(MetaClass var1) {
this.metaClass = var1;
}
public Object invokeMethod(String var1, Object var2) {
return this.getMetaClass().invokeMethod(this, var1, var2);
}
public Object getProperty(String var1) {
return this.getMetaClass().getProperty(this, var1);
}
public void setProperty(String var1, Object var2) {
this.getMetaClass().setProperty(this, var1, var2);
}
public int getRoot() {
return this.root;
}
public void setRoot(int var1) {
this.root = var1;
}
public Object getTestVar() {
return this.testVar;
}
public void setTestVar(Object var1) {
this.testVar = var1;
}
}
You can see that the line that prints out the value of the testVar field is represented by:
return var1[1].callCurrent(this, var1[2].call("The testVar:", this.testVar));
It means that no matter what value of testVar your subclass defines, the printTestVar method uses testVar field defined in the JobParams class. Period.
Using Groovy auto getter methods
There is one way you to implement the expected behavior. Every class field in Groovy has a getter method associated with that field compiled by Groovy for you. It means that you can access testVar by calling the getTestVar() method generated by the Groovy compiler. You can use it to override the value returned by a getter method for any field from the subclass. Consider the following example:
abstract class JobParams {
int root
def testVar=1
def setupRoot () {
println("The root");
}
def printTestVar () {
println("The testVar:" + getTestVar()); // <-- using a getTestVar() method instead a testVar field
}
}
class GradleJobParams extends JobParams {
}
def gjp = [
getTestVar: 3, // <-- stubbing getTestVar() method to return a different value
setupRoot:{
println("Override root");
}
] as GradleJobParams;
println("Starting");
gjp.printTestVar();
gjp.setupRoot();
Output:
Starting
The testVar:3
Override root

How to properly call GroovyScriptEngine?

I'm testing Groovy but I can't figure out how to properly call GroovyScriptEngine. It keeps producing an error below.
org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack
Song.Groovy
class Song {
def args;
{
println "Song has been called." + args;
}
String getArtist(){
return "sdfsdf";
}
public String toString(){
return "Hey!";
}
}
Java Main ->
String[] paths = { "C:\\Users\\User\\workspace\\GroovyTest\\src\\groovy" };
GroovyScriptEngine gse = new GroovyScriptEngine(paths);
Binding binding = new Binding();
Object s = "Default...";
binding.setVariable("args", s);
gse.run("Song.groovy", binding);
the args variable also produce null..
What to do ?
You are loading a class!
If you want to test your class, try something like this in the end of your Song.groovy:
// Instantiate an object of your class and use some methods!
def song = new Song()
println song.getArtist();
When you run
gse.run("Song.groovy", binding);
You are basically loading your class, but you are not doing anything with it.
See this example here
(Posted on behalf of the OP):
Working code:
Test1.java
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
public class Test1 {
public static void main(String[] args) throws Exception {
String[] paths = { "C:\\Users\\User\\workspace\\GroovyTest\\src\\groovy" };
GroovyScriptEngine gse = new GroovyScriptEngine(paths);
Binding binding = new Binding();
binding.setVariable("args", "Test Data");
String result = (String) gse.run("File1.groovy", binding);
System.out.println("Groovy Result: " + result);
}
}
File1.groovy
package groovy;
class Greeter {
String sayHello(String data) {
def greet = data;
return greet
}
}
static void main(String[] args) {
def greeter = new Greeter()
return greeter.sayHello(args);
}

What's an elegant way to have a reusable metaclass code in Groovy?

I would like to apply a meta-programming transformation to some of my classes, let's say by adding printXxx methods, like this:
class Person {
String name
}
def p = new Person()
p.printName() // does something
I have a rough idea how this can be done once I have a metaclass:
Person.metaClass.methodMissing = { name, args ->
delegate.metaClass."$name" = { println delegate."${getPropName(name)}" }
delegate."$name"(*args)
}
Now how do I turn this code into a reusable "library"? I would like to do something like:
#HasMagicPrinterMethod
class Person {
String name
}
or
class Person {
String name
static {
addMagicPrinters()
}
}
Define the behaviour you want to add as a trait
trait MagicPrinter {
void printProperties() {
this.properties.each { key, val ->
println "$key = $val"
}
}
}
Then add this trait to a class
class Person implements MagicPrinter {
String name
}
Now use it!
new Person(name: 'bob').printProperties()
You can go for a mixin approach:
class AutoPrint {
static def methodMissing(obj, String method, args) {
if (method.startsWith("print")) {
def property = (method - "print").with {
it[0].toLowerCase() + it[1..-1]
}
"${obj.getClass().simpleName} ${obj[property]}"
}
else {
throw new NoSuchMethodException()
}
}
}
You can mix it with a static block:
class Person {
static { Person.metaClass.mixin AutoPrint }
String name
}
def p = new Person(name: "john doe")
try {
p.captain()
assert false, "should've failed"
} catch (e) {
assert true
}
assert p.printName() == "Person john doe"
Or with expandoMetaClass:
class Car {
String model
}
Car.metaClass.mixin AutoPrint
assert new Car(model: "GT").printModel() == "Car GT"
Because 7 months later is the new 'now' :-)

How to get all the variables of a groovy object or class?

To see list of methods in a class I can do this -
String.methods.each {println it}
How do I list all the variables of an instance or all the static variables of a class?
Edit1:
Edit2:
HoneyBadger.java
public class HoneyBadger {
public int badassFactor;
protected int emoFactor;
private int sleepTime;
}
test.groovy -
HoneyBadger.metaClass.properties.each {println it.name }
Output -
class
You could do:
String.metaClass.properties.each { println it.name }
An alternative (given your new example) would be:
import java.lang.reflect.Modifier
HoneyBadger.declaredFields
.findAll { !it.synthetic }
.each { println "${Modifier.toString( it.modifiers )} ${it.name} : ${it.type}" }

How to make a "StringWriterMarkupBuilder"

How can I make a type which is a MarkupBuilder but has default constructor which is initialized with a StringWriter and overrides toString() to call the toString() on the StringWriter?
The idea is like the following, but of course initializing instance variables before super constructor calls is not allowed:
class StringWriterMarkupBuilder extends MarkupBuilder {
final def sw = new StringWriter()
StringWriterMarkupBuilder() {
super(sw)
}
#Override String toString() {
sw.toString()
}
}
Sorry, I finally figured out a solution by digging into the Groovy source code for MarkupBuilder!:
class StringWriterMarkupBuilder extends MarkupBuilder {
StringWriterMarkupBuilder() {
//MarkupBuilder.this(new IndentPrinter(new PrintWriter(writer)))
super(new StringWriter())
}
#Override String toString() {
//IndentPrinter.PrintWriter.StringWriter
return super.getPrinter().out.out.toString()
}
}
You could also do this with the metaClass like so:
import groovy.xml.MarkupBuilder
MarkupBuilder createSWMB() {
new StringWriter().with { sw ->
new MarkupBuilder( sw ).with { mb ->
mb.metaClass.toString = { -> sw.toString() }
mb
}
}
}
MarkupBuilder mb = createSWMB()
mb.root {
names {
name( first:'tim', second:'yates' )
}
}
println mb.toString()

Resources