简体   繁体   English

groovy无法通过字段语法识别java getter / setter

[英]groovy not recognizing java getters/setters through field syntax

I have the following inheritance hierarchy defined in java. 我在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. 和“ id”和“ name”无法识别。 The funny thing is they are recognized if I do one of the following: 有趣的是,如果我执行以下任一操作,它们就会被识别:

  1. not inherit Entity from BaseModel 不从BaseModel继承Entity
  2. Rather than inherit BaseModel from HashMap, make HashMap a data member of BaseModel 而不是从HashMap继承BaseModel,而是使HashMap成为BaseModel的数据成员
  3. inherit Entity directly from HashMap 直接从HashMap继承Entity

At first I thought that groovy is not recognizing the "id" and "name" syntax because of extending HashMap, but #3 above proves that incorrect. 起初我以为Groovy由于扩展了HashMap而无法识别“ id”和“ name”语法,但是上面的#3证明了这一点是不正确的。 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. 问题似乎出在Entity Class内的setter和getters,groovy中的所有内容都是公共的,它创建了所有的getters和setters方法。

I tested the next code in the groovy console and it worked. 我在groovy控制台中测试了下一个代码,它可以正常工作。

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. 它在groovyConsole输出屏幕上打印101。

When Entity is extending from BaseModel or directly a HashMap, Entity becomes a Map. 当Entity从BaseModel扩展或直接从HashMap扩展时,Entity成为Map。 So, when we say entity.id, Groovy is trying to find an entry in the map whose key is 'id'. 因此,当我们说entity.id时,Groovy试图在映射中查找键为“ 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. 但是,当Entity不再从BaseModel扩展时,entity.id将被解释为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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