bump product version to 4.1.6.2
[LibreOffice.git] / odk / examples / java / Inspector / UnoMethodNode.java
blob69bb58e4d2d914218b05c01b1cc31ed4777f6ba8
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 import com.sun.star.reflection.ParamInfo;
36 import com.sun.star.reflection.ParamMode;
37 import com.sun.star.reflection.XIdlClass;
38 import com.sun.star.reflection.XIdlMethod;
39 import com.sun.star.uno.TypeClass;
41 public class UnoMethodNode extends UnoNode{
42 XIdlMethod m_xIdlMethod = null;
43 Object[] m_oParamObjects = null;
44 Object m_oUnoReturnObject = null;
45 boolean m_bisInvoked = false;
46 XUnoMethodNode m_xUnoMethodNode = null;
49 /** Creates a new instance of UnoMethodNode */
50 public UnoMethodNode(XIdlMethod _xIdlMethod, Object _oUnoObject, XUnoMethodNode _xUnoMethodNode) {
51 super(_oUnoObject);
52 m_xIdlMethod = _xIdlMethod;
53 m_oParamObjects = new Object[m_xIdlMethod.getParameterInfos().length];
54 m_xUnoMethodNode = _xUnoMethodNode;
57 protected boolean isFoldable(){
58 return ((!this.isPrimitive()) && (getTypeClass().getValue() != TypeClass.VOID_value));
61 protected boolean isInvokable(){
62 boolean bisFoldable = true;
63 XIdlClass[] xIdlClasses = m_xIdlMethod.getParameterTypes();
64 for (int i = 0; i < xIdlClasses.length; i++){
65 bisFoldable = Introspector.isPrimitive(xIdlClasses[i].getTypeClass());
66 if (!bisFoldable){
67 return false;
70 return bisFoldable;
73 public XIdlMethod getXIdlMethod(){
74 return m_xIdlMethod;
78 public String getAnchor(){
79 return getXIdlMethod().getName();
83 public String getName(){
84 return getXIdlMethod().getName();
88 public Object invoke(){
89 Object oUnoReturnObject = null;
90 if (!hasParameters()){
91 oUnoReturnObject = invokeParameterlessMethod();
92 m_bisInvoked = true;
94 else{
95 java.util.List<Object> oUnoMethodObjects = m_xUnoMethodNode.getMethodObjects();
96 if (oUnoMethodObjects != null){
97 for (int i = 0; i < getXIdlMethod().getParameterInfos().length; i++){
98 this.m_oParamObjects[i] = oUnoMethodObjects.get(i);
100 if (oUnoMethodObjects.size() == m_oParamObjects.length + 1){
101 oUnoReturnObject = oUnoMethodObjects.get(oUnoMethodObjects.size()-1);
103 m_bisInvoked = (oUnoReturnObject != null);
106 m_oUnoReturnObject = oUnoReturnObject;
107 return oUnoReturnObject;
111 public boolean isInvoked(){
112 return m_bisInvoked;
116 protected String getNodeDescription(){
117 String sNodeDescription = "";
118 String sParameters = getParameterDescription();
119 if (m_xIdlMethod.getParameterInfos().length > 0){
120 sNodeDescription = getStandardMethodDescription();
122 else{
123 TypeClass typeClass = getTypeClass();
124 if (typeClass != TypeClass.VOID){
125 sNodeDescription = getStandardMethodDescription();
127 else{
128 sNodeDescription = getStandardMethodDescription();
131 return sNodeDescription;
135 public String getStandardMethodDescription(){
136 String sNodeDescription = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + getParameterDescription() + " )";
137 if (isPrimitive()){
138 sNodeDescription += "";
140 return sNodeDescription;
144 public boolean hasParameters(){
145 return (m_xIdlMethod.getParameterInfos().length > 0);
149 public Object[] getLastParameterObjects(){
150 return m_oParamObjects;
154 public Object getLastUnoReturnObject(){
155 return m_oUnoReturnObject;
159 public String getParameterDescription(){
160 ParamInfo[] paramInfo = m_xIdlMethod.getParameterInfos();
161 String sParameters = "";
162 String sStandardMethodDisplayText = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + sParameters + " )";
163 if (Introspector.isValid(paramInfo)) {
164 // get all parameters with type and mode
165 for ( int i = 0; i < paramInfo.length; i++ ) {
166 XIdlClass xIdlClass = paramInfo[ i ].aType;
167 if ( i == 0 ) {
168 // the first parameter has no leading comma
169 sParameters += "[" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
171 else {
172 // all other parameters are separated with comma
173 sParameters += ", [" + getParamMode(paramInfo[ i ].aMode ) + "] " + xIdlClass.getName();
177 return sParameters;
181 // return the parameter mode (IN, OUT, INOUT)
182 private static String getParamMode(ParamMode paramMode) {
183 String toReturn = "";
184 if ( paramMode == ParamMode.IN ) {
185 toReturn = "IN";
187 if ( paramMode == ParamMode.OUT ) {
188 toReturn = "OUT";
190 if ( paramMode == ParamMode.INOUT ) {
191 toReturn = "INOUT";
193 return( toReturn );
196 public TypeClass getTypeClass(){
197 XIdlClass xIdlClass = m_xIdlMethod.getReturnType();
198 return xIdlClass.getTypeClass();
202 private Object invokeParameterlessMethod(){
203 try {
204 Object[][] aParamInfo = new Object[1][];
205 aParamInfo[0] = new Object[] {};
206 return getXIdlMethod().invoke(getUnoObject(), aParamInfo);
207 } catch (Exception ex) {
208 ex.printStackTrace(System.err);
209 return null;
213 public boolean isPrimitive(){
214 return Introspector.isObjectPrimitive(m_xIdlMethod.getClass(), getTypeClass());
218 protected Object invoke(Object _oUnoObject, Object[] oParameters) throws com.sun.star.uno.Exception{
219 Object[][] aParams = new Object[1][oParameters.length];
220 for ( int i = 0; i < oParameters.length; i++ ) {
221 aParams[0][i] = oParameters[i];
223 return m_xIdlMethod.invoke(_oUnoObject, aParams);