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.
source share