Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / java / Inspector / UnoMethodNode.java
bloba620fc81a119b3a97d77426aab00840fc5b8e047
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.reflection.ParamInfo;
37 import com.sun.star.reflection.ParamMode;
38 import com.sun.star.reflection.XIdlClass;
39 import com.sun.star.reflection.XIdlMethod;
40 import com.sun.star.uno.TypeClass;
42 public class UnoMethodNode extends UnoNode{
43 private XIdlMethod m_xIdlMethod = null;
44 private Object[] m_oParamObjects = null;
45 private Object m_oUnoReturnObject = null;
46 private boolean m_bisInvoked = false;
47 private XUnoMethodNode m_xUnoMethodNode = null;
50 /** Creates a new instance of UnoMethodNode */
51 public UnoMethodNode(XIdlMethod _xIdlMethod, Object _oUnoObject, XUnoMethodNode _xUnoMethodNode) {
52 super(_oUnoObject);
53 m_xIdlMethod = _xIdlMethod;
54 m_oParamObjects = new Object[m_xIdlMethod.getParameterInfos().length];
55 m_xUnoMethodNode = _xUnoMethodNode;
58 protected boolean isFoldable(){
59 return ((!this.isPrimitive()) && (getTypeClass().getValue() != TypeClass.VOID_value));
62 protected boolean isInvokable(){
63 boolean bisFoldable = true;
64 XIdlClass[] xIdlClasses = m_xIdlMethod.getParameterTypes();
65 for (int i = 0; i < xIdlClasses.length; i++){
66 bisFoldable = Introspector.isPrimitive(xIdlClasses[i].getTypeClass());
67 if (!bisFoldable){
68 return false;
71 return bisFoldable;
74 public XIdlMethod getXIdlMethod(){
75 return m_xIdlMethod;
79 @Override
80 public String getAnchor(){
81 return getXIdlMethod().getName();
85 public String getName(){
86 return getXIdlMethod().getName();
90 public Object invoke(){
91 Object oUnoReturnObject = null;
92 if (!hasParameters()){
93 oUnoReturnObject = invokeParameterlessMethod();
94 m_bisInvoked = true;
96 else{
97 java.util.List<Object> oUnoMethodObjects = m_xUnoMethodNode.getMethodObjects();
98 if (oUnoMethodObjects != null){
99 for (int i = 0; i < getXIdlMethod().getParameterInfos().length; i++){
100 this.m_oParamObjects[i] = oUnoMethodObjects.get(i);
102 if (oUnoMethodObjects.size() == m_oParamObjects.length + 1){
103 oUnoReturnObject = oUnoMethodObjects.get(oUnoMethodObjects.size()-1);
105 m_bisInvoked = (oUnoReturnObject != null);
108 m_oUnoReturnObject = oUnoReturnObject;
109 return oUnoReturnObject;
113 public boolean isInvoked(){
114 return m_bisInvoked;
118 protected String getNodeDescription(){
119 String sNodeDescription = "";
120 String sParameters = getParameterDescription();
121 if (m_xIdlMethod.getParameterInfos().length > 0){
122 sNodeDescription = getStandardMethodDescription();
124 else{
125 TypeClass typeClass = getTypeClass();
126 if (typeClass != TypeClass.VOID){
127 sNodeDescription = getStandardMethodDescription();
129 else{
130 sNodeDescription = getStandardMethodDescription();
133 return sNodeDescription;
137 public String getStandardMethodDescription(){
138 String sNodeDescription = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + getParameterDescription() + " )";
139 if (isPrimitive()){
140 sNodeDescription += "";
142 return sNodeDescription;
146 public boolean hasParameters(){
147 return (m_xIdlMethod.getParameterInfos().length > 0);
151 public Object[] getLastParameterObjects(){
152 return m_oParamObjects;
156 public Object getLastUnoReturnObject(){
157 return m_oUnoReturnObject;
161 public String getParameterDescription(){
162 ParamInfo[] paramInfo = m_xIdlMethod.getParameterInfos();
163 String sParameters = "";
164 if (Introspector.isValid(paramInfo)) {
165 // get all parameters with type and mode
166 for ( int i = 0; i < paramInfo.length; i++ ) {
167 XIdlClass xIdlClass = paramInfo[ i ].aType;
168 if ( i == 0 ) {
169 // the first parameter has no leading comma
170 sParameters += "[" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
172 else {
173 // all other parameters are separated with comma
174 sParameters += ", [" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
178 return sParameters;
182 // return the parameter mode (IN, OUT, INOUT)
183 private static String getParamMode(ParamMode paramMode) {
184 String toReturn = "";
185 if ( paramMode == ParamMode.IN ) {
186 toReturn = "IN";
188 if ( paramMode == ParamMode.OUT ) {
189 toReturn = "OUT";
191 if ( paramMode == ParamMode.INOUT ) {
192 toReturn = "INOUT";
194 return toReturn;
197 public TypeClass getTypeClass(){
198 XIdlClass xIdlClass = m_xIdlMethod.getReturnType();
199 return xIdlClass.getTypeClass();
203 private Object invokeParameterlessMethod(){
204 try {
205 Object[][] aParamInfo = new Object[1][];
206 aParamInfo[0] = new Object[] {};
207 return getXIdlMethod().invoke(getUnoObject(), aParamInfo);
208 } catch (Exception ex) {
209 ex.printStackTrace(System.err);
210 return null;
214 public boolean isPrimitive(){
215 return Introspector.isObjectPrimitive(m_xIdlMethod.getClass(), getTypeClass());
219 protected Object invoke(Object _oUnoObject, Object[] oParameters) throws com.sun.star.uno.Exception{
220 Object[][] aParams = new Object[1][oParameters.length];
221 for ( int i = 0; i < oParameters.length; i++ ) {
222 aParams[0][i] = oParameters[i];
224 return m_xIdlMethod.invoke(_oUnoObject, aParams);
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */