Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何知道OpenOffice Calc UNO对象支持的Java接口(通过queryInterface)_Java_Openoffice Calc_Uno - Fatal编程技术网

如何知道OpenOffice Calc UNO对象支持的Java接口(通过queryInterface)

如何知道OpenOffice Calc UNO对象支持的Java接口(通过queryInterface),java,openoffice-calc,uno,Java,Openoffice Calc,Uno,我正在为OpenOffice Calc开发一个“宏”。作为语言,我选择了Java,以便在Eclipse中获得代码帮助。我甚至编写了一个小型的antbuild脚本,编译“宏”并将其嵌入到*.ods文件中。总的来说,这很好,速度惊人;我已经非常成功地使用了一些简单的东西 但是 我经常陷入困境,因为使用UNO,我需要“查询”任何给定非平凡对象的接口,以便能够访问该对象的数据/调用方法。也就是说,我确实需要猜测一个给定对象可能提供哪些接口。这一点在Java开发过程中根本不明显,甚至不可见(通过某种元信息

我正在为OpenOffice Calc开发一个“宏”。作为语言,我选择了Java,以便在Eclipse中获得代码帮助。我甚至编写了一个小型的
ant
build脚本,编译“宏”并将其嵌入到
*.ods
文件中。总的来说,这很好,速度惊人;我已经非常成功地使用了一些简单的东西

但是

我经常陷入困境,因为使用
UNO
,我需要“查询”任何给定非平凡对象的接口,以便能够访问该对象的数据/调用方法。也就是说,我确实需要猜测一个给定对象可能提供哪些接口。这一点在Java开发过程中根本不明显,甚至不可见(通过某种元信息、反射等),而且文档也很少(我下载了很多东西,但是我没有找到我正在使用的接口的源代码或者JavaDoc,比如
XButton
XPropertySet
,等等-
XButton
setLabel
,但没有
getLabel
-什么??)

有在线文档(对于最基本的概念,这一点也不错!),但它缺少我所面临的许多细节。它总是神奇地停在我需要解决的地方

我愿意通过查看
C++
代码来了解对象的接口(例如,我当前遇到的按钮/事件)可能会提供。令人困惑的是,
C++
类和文件名与Java接口不完全匹配。这几乎就是我要寻找的,但在Java中,我并没有找到等价的,或者对给定对象调用
queryInterface
返回
null
。这变得有点令人沮丧

UNO Java接口是如何生成的?代码中是否有某种文档作为生成的(Java)代码的来源


我想我真的需要知道在哪一点上有哪些接口可用,以便在Java UNO宏开发过程中变得更加流畅。

对于任何严肃的UNO项目,使用自省工具

例如,我在Calc中创建了一个按钮,然后使用Java浏览到该按钮。 右键单击并选择“添加到源代码”生成以下内容

import com.sun.star.awt.XControlModel;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.drawing.XControlShape;
import com.sun.star.drawing.XDrawPage;
import com.sun.star.drawing.XDrawPageSupplier;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;

//...
public void codesnippet(XInterface _oUnoEntryObject){
try{
    XSpreadsheetDocument xSpreadsheetDocument =  (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, _oUnoEntryObject);
    XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
    XNameAccess xNameAccess =  (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xSpreadsheets);
    Object oName = xNameAccess.getByName("Sheet1");
    XDrawPageSupplier xDrawPageSupplier =  (XDrawPageSupplier) UnoRuntime.queryInterface(XDrawPageSupplier.class, oName);
    XDrawPage xDrawPage = xDrawPageSupplier.getDrawPage();
    XIndexAccess xIndexAccess =  (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xDrawPage);
    Object oIndex = xIndexAccess.getByIndex(0);
    XControlShape xControlShape =  (XControlShape) UnoRuntime.queryInterface(XControlShape.class, oIndex);
    XControlModel xControlModel = xControlShape.getControl();
    XPropertySet xPropertySet =  (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
    String sLabel = AnyConverter.toString(xPropertySet.getPropertyValue("Label"));
}catch (com.sun.star.beans.UnknownPropertyException e){
    e.printStackTrace(System.out);
    //Enter your Code here...
}catch (com.sun.star.lang.WrappedTargetException e2){
    e2.printStackTrace(System.out);
    //Enter your Code here...
}catch (com.sun.star.lang.IllegalArgumentException e3){
    e3.printStackTrace(System.out);
    //Enter your Code here...
}
}
//...

Python UNO可能比Java更好,因为它不需要查询特定的接口。另外,XrayTool和MRI比Java对象检查器更易于使用。

这太好了!非常感谢。Python提示也很好,我已经怀疑非Java/-C++方法允许更容易的访问。谢谢!