我正在开发一个 Eclipse RCP 应用程序,我最近开始在其中使用 Groovy。所以我 99% 的代码仍然是 Java。
我读到可以使用 Groovy 覆盖和向 Java 类添加方法,并且我能够通过向 java.lang.String 添加方法来测试这一点。
但这仅在我在 Groovy 类中使用字符串时才有效。被覆盖的方法不被视为在 Java 类中被覆盖。
这是一些代码:
/*
* This is a Java class
*/
public class CTabItem {
...
private API
...
public void setControl(Control control){
private API
}
}
/*
* This is also a Java class
*/
public class Control {
...
private API
...
}
/*
* This is also a Java class
*/
public class OtherClass {
...
private API
...
private void someMethodIDontKnow(){
Control control = new Control();
CTabItem tab = new CTabItem();
tab.setControl(control);
}
}
/*
* This is a Groovy class
*/
public class MyViewPart extends org.eclipse.ui.part.ViewPart {
....
public void createPartControl(Composite parent) {
/* parent (or the parent of parent) is a Control
which is set somewhere in a CTabItem to which
I don't get access */
}
}
我需要从控件中获取选项卡。但由于实例化 MyViewPart 的不是我,而是一些私有 API,所以我无权访问它。Groovy 可以在这里为我做些什么吗?欢迎任何建议或代码。谢谢!