How to call a variable Java method called getProperties () that overrides Groovy

I have a Java class with the .getProperties() method, but when I call this method in Groovy, it returns the LinkedHashMap properties from Groovy Beans magic instead of the getProperties method defined by my Java class.

How do I call the getProperties() method instead of Groovy one?

My Java code (simplified):

 import java.util.*; public class MyObject { private Collection<Property> properties; private static class Property { public String value; public Property(String value) { this.value = value; } public String toString() { return String.format("Property: %s", this.value); } } private static interface PropertyFilter { boolean passes(String value); } public static class StartsWithPropertyFilter implements PropertyFilter { public String prefix; public StartsWithPropertyFilter(String prefix) { this.prefix = prefix; } public boolean passes(String value) { if(value == null) return false; return value.startsWith(prefix); } } public MyObject() { this(new ArrayList<Property>()); } public MyObject(Collection<Property> myProperties) { this.properties = myProperties; } public void addProperty(String value) { this.properties.add(new Property(value)); } public Collection<Property> getProperties(PropertyFilter... filters) { Collection<Property> ret = new ArrayList<Property>(); for(Property prop : properties) { boolean passes = true; for(PropertyFilter filter : filters) { if(!filter.passes(prop.value)) { passes = false; break; } } if(passes) { ret.add(prop); } } System.out.println("Java getProperties()"); return ret; } public static void main(String[] args) { MyObject obj = new MyObject(); obj.addProperty("Fast"); obj.addProperty("Strong"); obj.addProperty("Furious"); System.out.println(obj.getProperties()); System.out.println(obj.getProperties(new MyObject.StartsWithPropertyFilter("F"))); } } 

My Groovy Code:

 MyObject obj = new MyObject() obj.addProperty("Fast") obj.addProperty("Strong") obj.addProperty("Furious") println obj.getProperties() println obj.getProperties(new MyObject.StartsWithPropertyFilter("F")) 

Run with this:

 javac MyObject.java && groovy Run.groovy && echo && java MyObject 

And I get this output:

 [class:class MyObject] Java getProperties() [Property: Fast, Property: Furious] Java getProperties() [Property: Fast, Property: Strong, Property: Furious] Java getProperties() [Property: Fast, Property: Furious] 

My version of Groovy is quite recent (2.0.1):

 $ groovy -version Groovy Version: 2.0.1 JVM: 1.6.0_27 Vendor: Sun Microsystems Inc. OS: Linux 

But I initially saw the problem on 1.8.5. I suppose it was.

+4
source share
2 answers

This is probably no longer relevant, but with one minor change, you can do this:

 obj.getProperties([] as MyObject.PropertyFilter[]) 

This change is that the PropertyFilter interface should not be closed.

+1
source

I do not see the same behavior. Using Groovy 1.8.5 if I compile this as Java:

 import java.util.*; public class MyClass { private Collection<String> myProperties; public MyClass() { this.myProperties = new ArrayList<String>(); } public MyClass(Collection<String> myProperties) { this.myProperties = myProperties; } public Collection<String> getProperties() { System.err.println("java getProperties"); return this.myProperties; } } 

and run this groovy script:

MyClass myclass = new MyClass () println myClass.getProperties ()

output:

 bash-3.2$ javac MyClass.java bash-3.2$ groovy Test.groovy java getProperties [] bash-3.2$ 
+1
source

Source: https://habr.com/ru/post/1434276/


All Articles