Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / UnoMethodNode.java
blob3f7decafab0729c8dd5119eb81bc417883f34957
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 private XIdlMethod m_xIdlMethod = null;
43 private Object[] m_oParamObjects = null;
44 private Object m_oUnoReturnObject = null;
45 private boolean m_bisInvoked = false;
46 private 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 @Override
79 public String getAnchor(){
80 return getXIdlMethod().getName();
84 public String getName(){
85 return getXIdlMethod().getName();
89 public Object invoke(){
90 Object oUnoReturnObject = null;
91 if (!hasParameters()){
92 oUnoReturnObject = invokeParameterlessMethod();
93 m_bisInvoked = true;
95 else{
96 java.util.List<Object> oUnoMethodObjects = m_xUnoMethodNode.getMethodObjects();
97 if (oUnoMethodObjects != null){
98 for (int i = 0; i < getXIdlMethod().getParameterInfos().length; i++){
99 this.m_oParamObjects[i] = oUnoMethodObjects.get(i);
101 if (oUnoMethodObjects.size() == m_oParamObjects.length + 1){
102 oUnoReturnObject = oUnoMethodObjects.get(oUnoMethodObjects.size()-1);
104 m_bisInvoked = (oUnoReturnObject != null);
107 m_oUnoReturnObject = oUnoReturnObject;
108 return oUnoReturnObject;
112 public boolean isInvoked(){
113 return m_bisInvoked;
117 protected String getNodeDescription(){
118 String sNodeDescription = "";
119 String sParameters = getParameterDescription();
120 if (m_xIdlMethod.getParameterInfos().length > 0){
121 sNodeDescription = getStandardMethodDescription();
123 else{
124 TypeClass typeClass = getTypeClass();
125 if (typeClass != TypeClass.VOID){
126 sNodeDescription = getStandardMethodDescription();
128 else{
129 sNodeDescription = getStandardMethodDescription();
132 return sNodeDescription;
136 public String getStandardMethodDescription(){
137 String sNodeDescription = m_xIdlMethod.getReturnType().getName() + " " + m_xIdlMethod.getName() + " (" + getParameterDescription() + " )";
138 if (isPrimitive()){
139 sNodeDescription += "";
141 return sNodeDescription;
145 public boolean hasParameters(){
146 return (m_xIdlMethod.getParameterInfos().length > 0);
150 public Object[] getLastParameterObjects(){
151 return m_oParamObjects;
155 public Object getLastUnoReturnObject(){
156 return m_oUnoReturnObject;
160 public String getParameterDescription(){
161 ParamInfo[] paramInfo = m_xIdlMethod.getParameterInfos();
162 String 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);