简体   繁体   中英

groovy not recognizing java getters/setters through field syntax

I have the following inheritance hierarchy defined in java.

public class BaseModel extends HashMap<String, Object> { 
    public String getString(String key) { 
        return (String)this.getOrDefault(key, "EMPTY"); 
    } 

} 

public class Entity extends BaseModel { 

    private String id; 
    private String name; 

    public String getName() { 
        return name; 
    } 

    public void setName(String name) { 
        this.name = name; 
    } 

    public String getId() { 
        return id; 
    } 

    public void setId(String id) { 
        this.id = id; 
    } 
} 

Now in a groovy script I try to do the following:

Entity entity = new Entity(); 
entity.id = "101"; 
entity.name = "Apple"

and "id" and "name" are not recognized. The funny thing is they are recognized if I do one of the following:

  1. not inherit Entity from BaseModel
  2. Rather than inherit BaseModel from HashMap, make HashMap a data member of BaseModel
  3. inherit Entity directly from HashMap

At first I thought that groovy is not recognizing the "id" and "name" syntax because of extending HashMap, but #3 above proves that incorrect. I am stumped as to why this is not being recognized at this point. Can someone help me out? It should be easy enough to copy paste this and try it out yourself.

The problem seems to be the setters and getters inside the Entity Class, everything in groovy is public and it creates all the getters and setters methods.

I tested the next code in the groovy console and it worked.

public class BaseModel extends HashMap<String, Object> { 
    public String getString(String key) { 
        return (String)this.getOrDefault(key, "EMPTY"); 
    } 

} 

public class Entity extends BaseModel { 

    private String id; 
    private String name; 
} 

Entity entity = new Entity(); 
entity.id = "101"; 
entity.name = "Apple"

println entity.id

It prints 101 in the groovyConsole output screen.

When Entity is extending from BaseModel or directly a HashMap, Entity becomes a Map. So, when we say entity.id, Groovy is trying to find an entry in the map whose key is 'id'. As there is no such entry, it prints out null.

public class Entity extends HashMap<String, String> {

    private String id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Entity entity = new Entity();
entity.id = "101";
entity.name = "Apple"

println entity.id   //prints null

But when Entity is not extending from BaseModel anymore, entity.id will be interpreted just as a member of Entity.

public class Entity  {

    private String id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Entity entity = new Entity();
entity.id = "101";
entity.name = "Apple"

println entity.id  //prints 101

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM