无法从 Java class 访问域 属性(在 src 文件夹中)
Cannot access domain property from Java class (in src folder)
我无法访问域 class 中的漏洞 属性 - 场景,来自我的 java class - MatchScenario,位于 Grails src 文件夹中。
已经尝试过:
显式方法:
我试过明确地创建 get;set;但是我得到了 Whosebug 错误,因为 setExploits() 由于某种原因被自身无限调用。
服务于return漏洞领域,
尽管创建了该服务,但从未在我的 fork-debug 集成测试中调用它,因此测试无一例外地挂起
编译错误->
Error:(59, 44) java: cannot find symbol
symbol: variable exploits
location: variable scenario of type core.Scenario
Java class,内循环错误 ->
public class MatchScenario implements Callable{
private static final Logger LOG = Logger.getLogger(MatchScenario.class.getCanonicalName());
private List<Scenario> scenarioList
@Override
public List<Scenario> call() throws Exception {
LOG.debug( "*********************** schedule matcher called *****************************" );
if (scenarioList==null) {
LOG.debug("scenarioList not initialized ");
return null;
}
List<Scenario> scenarioAvailable = new ArrayList<Scenario>();
for (Scenario scenario : scenarioList){
for (Exploit exploit : scenario.exploits){
//println 'exploit -> '+exploit
if (!match( exploit.getExpression() ) ){
break;
}
}
//happens only when all scenario are available ( no break issued )
scenarioAvailable.add(scenario);
}
return scenarioAvailable;
}
private boolean match(String expression) {
return true;
}
}
场景域对象->
package core
class Scenario {
String name
static belongsTo = [ Exploit ]
static hasMany = [ exploits : Exploit ]
static constraints = {
name nullable: false , maxSize: 32
}
}
您混淆了字段和属性。当您在 Groovy class 中声明 属性 时,例如String name
,Groovy 编译器将其转换为私有字段并添加一个 getter 和一个 setter(除非您已经定义了其中一个或两个 - 它不会覆盖), 在这种情况下类似于
private String name
public void setName(String name) { this.name = name }
public String getName() { return name }
只有在没有范围修饰符的情况下才会这样做,因此 public String name
和 protected String name
都将保持原样。
这样做的一个好处是您可以稍后向 getter and/or setter 添加逻辑来修改值,进行一些验证检查或计算等,并在Groovy 你仍然会读取和写入 name
属性 因为 属性 访问总是调用底层的 setters 和 getters,并且因为像这样的属性是 Groovy 唯一的东西,Java 无法访问,你会一直从 Java 调用 setter 和 getter,所以你不需要重新编译使用 class.
的 Java classes
像您这样声明 hasMany
会有效地创建 exploits
属性
Set<Exploit> exploits
并且 属性(通过 Grails AST 转换添加)同样被转换为具有 getter 和 setter 的私有字段。所以要从 Java 开始工作,请使用 getter:
for (Exploit exploit : scenario.getExploits()) {
我无法访问域 class 中的漏洞 属性 - 场景,来自我的 java class - MatchScenario,位于 Grails src 文件夹中。
已经尝试过:
显式方法: 我试过明确地创建 get;set;但是我得到了 Whosebug 错误,因为 setExploits() 由于某种原因被自身无限调用。
服务于return漏洞领域, 尽管创建了该服务,但从未在我的 fork-debug 集成测试中调用它,因此测试无一例外地挂起
编译错误->
Error:(59, 44) java: cannot find symbol
symbol: variable exploits
location: variable scenario of type core.Scenario
Java class,内循环错误 ->
public class MatchScenario implements Callable{
private static final Logger LOG = Logger.getLogger(MatchScenario.class.getCanonicalName());
private List<Scenario> scenarioList
@Override
public List<Scenario> call() throws Exception {
LOG.debug( "*********************** schedule matcher called *****************************" );
if (scenarioList==null) {
LOG.debug("scenarioList not initialized ");
return null;
}
List<Scenario> scenarioAvailable = new ArrayList<Scenario>();
for (Scenario scenario : scenarioList){
for (Exploit exploit : scenario.exploits){
//println 'exploit -> '+exploit
if (!match( exploit.getExpression() ) ){
break;
}
}
//happens only when all scenario are available ( no break issued )
scenarioAvailable.add(scenario);
}
return scenarioAvailable;
}
private boolean match(String expression) {
return true;
}
}
场景域对象->
package core
class Scenario {
String name
static belongsTo = [ Exploit ]
static hasMany = [ exploits : Exploit ]
static constraints = {
name nullable: false , maxSize: 32
}
}
您混淆了字段和属性。当您在 Groovy class 中声明 属性 时,例如String name
,Groovy 编译器将其转换为私有字段并添加一个 getter 和一个 setter(除非您已经定义了其中一个或两个 - 它不会覆盖), 在这种情况下类似于
private String name
public void setName(String name) { this.name = name }
public String getName() { return name }
只有在没有范围修饰符的情况下才会这样做,因此 public String name
和 protected String name
都将保持原样。
这样做的一个好处是您可以稍后向 getter and/or setter 添加逻辑来修改值,进行一些验证检查或计算等,并在Groovy 你仍然会读取和写入 name
属性 因为 属性 访问总是调用底层的 setters 和 getters,并且因为像这样的属性是 Groovy 唯一的东西,Java 无法访问,你会一直从 Java 调用 setter 和 getter,所以你不需要重新编译使用 class.
像您这样声明 hasMany
会有效地创建 exploits
属性
Set<Exploit> exploits
并且 属性(通过 Grails AST 转换添加)同样被转换为具有 getter 和 setter 的私有字段。所以要从 Java 开始工作,请使用 getter:
for (Exploit exploit : scenario.getExploits()) {