Update git submodules
[LibreOffice.git] / qadevOOo / runner / util / dbg.java
blob5416e92f6d58e5805836f0f8abfc641d90ebf602
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package util;
21 import com.sun.star.uno.XInterface;
22 import com.sun.star.uno.UnoRuntime;
23 import com.sun.star.uno.Type;
24 import com.sun.star.beans.XPropertySet;
25 import com.sun.star.beans.XPropertySetInfo;
26 import com.sun.star.beans.Property;
27 import com.sun.star.beans.PropertyAttribute;
28 import com.sun.star.beans.PropertyValue;
29 import com.sun.star.lang.XTypeProvider;
30 import com.sun.star.lang.XServiceInfo;
31 import java.io.PrintWriter;
32 import java.io.OutputStreamWriter;
33 import java.io.UnsupportedEncodingException;
34 import java.lang.reflect.Method;
36 /**
37 * This class accumulates all kinds of methods for accessing debug information
38 * from UNO implementations.
40 public class dbg {
42 /**
43 * Prints information about the supported interfaces of an implementation
44 * to standard out.
45 * @param xTarget The implementation which should be analysed.
46 * @see com.sun.star.uno.XInterface
48 public static void printInterfaces(XInterface xTarget) {
49 printInterfaces(xTarget, false);
52 /**
53 * Prints information about the supported interfaces of an implementation
54 * to standard out. Extended information can be printed.
55 * @param xTarget The implementation which should be analysed.
56 * @param extendedInfo Should extended information be printed?
57 * @see com.sun.star.uno.XInterface
59 private static void printInterfaces(XInterface xTarget,
60 boolean extendedInfo){
61 Type[] types = getInterfaceTypes(xTarget);
62 if( null != types ) {
63 int nLen = types.length;
64 for( int i = 0; i < nLen ; i++ ) {
65 System.out.println(types[i].getTypeName());
66 if (extendedInfo) {
67 printInterfaceInfo(types[i]);
68 System.out.println();
74 /**
75 * Returns all interface types of an implementation as a type array.
76 * @param xTarget The implementation which should be analyzed.
77 * @return An array with all interface types; null if there are none.
78 * @see com.sun.star.uno.XInterface
80 private static Type[] getInterfaceTypes(XInterface xTarget) {
81 Type[] types = null;
82 XTypeProvider xTypeProvider = UnoRuntime.queryInterface( XTypeProvider.class, xTarget);
83 if( xTypeProvider != null )
84 types = xTypeProvider.getTypes();
85 return types;
90 /**
91 * Prints information about an interface type.
93 * @param aType The type of the given interface.
94 * @see com.sun.star.uno.Type
96 private static void printInterfaceInfo(Type aType) {
97 try {
98 Class<?> zClass = aType.getZClass();
99 Method[] methods = zClass.getDeclaredMethods();
100 for (int i=0; i<methods.length; i++) {
101 System.out.println("\t" + methods[i].getReturnType().getName()
102 + " " + methods[i].getName() + "()");
105 catch (Exception ex) {
106 System.out.println("Exception occurred while printing InterfaceInfo");
107 ex.printStackTrace();
112 * Prints a string array to standard out.
114 * @param entries : The array to be printed.
116 public static void printArray( String [] entries ) {
117 for ( int i=0; i< entries.length;i++ ) {
118 System.out.println(entries[i]);
123 * Print all information about the property <code>name</code> from
124 * the property set <code>PS</code> to standard out.
125 * @param PS The property set which should contain a property called
126 * <code>name</code>.
127 * @param name The name of the property.
128 * @see com.sun.star.beans.XPropertySet
130 public static void printPropertyInfo(XPropertySet PS, String name) throws UnsupportedEncodingException {
131 printPropertyInfo(PS, name, new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")));
135 * Print all information about the property <code>name</code> from
136 * the property set <code>PS</code> to a print writer.
137 * @param PS The property set which should contain a property called
138 * <code>name</code>.
139 * @param name The name of the property.
140 * @param out The print writer which is used as output.
141 * @see com.sun.star.beans.XPropertySet
143 public static void printPropertyInfo(XPropertySet PS, String name,
144 PrintWriter out) {
145 try {
146 XPropertySetInfo PSI = PS.getPropertySetInfo();
147 PSI.getProperties();
148 Property prop = PSI.getPropertyByName(name);
149 out.println("Property name is " + prop.Name);
150 out.println("Property handle is " + prop.Handle);
151 out.println("Property type is " + prop.Type.getTypeName());
152 out.println("Property current value is " +
153 PS.getPropertyValue(name));
154 out.println("Attributes :");
155 short attr = prop.Attributes;
157 if ((attr & PropertyAttribute.BOUND) != 0)
158 out.println("\t-BOUND");
160 if ((attr & PropertyAttribute.CONSTRAINED) != 0)
161 out.println("\t-CONSTRAINED");
163 if ((attr & PropertyAttribute.MAYBEAMBIGUOUS) != 0)
164 out.println("\t-MAYBEAMBIGUOUS");
166 if ((attr & PropertyAttribute.MAYBEDEFAULT) != 0)
167 out.println("\t-MAYBEDEFAULT");
169 if ((attr & PropertyAttribute.MAYBEVOID) != 0)
170 out.println("\t-MAYBEVOID");
172 if ((attr & PropertyAttribute.READONLY) != 0)
173 out.println("\t-READONLY");
175 if ((attr & PropertyAttribute.REMOVABLE) != 0)
176 out.println("\t-REMOVABLE");
178 if ((attr & PropertyAttribute.TRANSIENT) != 0)
179 out.println("\t-TRANSIENT");
180 } catch(com.sun.star.uno.Exception e) {
181 out.println("Exception!!!!");
182 e.printStackTrace(out);
189 * Print the names and the values of a sequence of <code>PropertyValue</code>
190 * to a print writer.
191 * @param ps The property which should displayed
192 * @param out The print writer which is used as output.
193 * @see com.sun.star.beans.PropertyValue
195 private static void printPropertyValueSequencePairs(PropertyValue[] ps, PrintWriter out){
196 for( int i = 0; i < ps.length; i++){
197 printPropertyValuePairs(ps[i], out);
204 * Print the name and the value of a <code>PropertyValue</code> to a print writer.
205 * @param ps The property which should displayed
206 * @param out The print writer which is used as output.
207 * @see com.sun.star.beans.PropertyValue
209 private static void printPropertyValuePairs(PropertyValue ps, PrintWriter out){
211 if (ps.Value instanceof String[] ){
212 String[] values = (String[]) ps.Value;
213 StringBuilder oneValue = new StringBuilder("value is an empty String[]");
214 if (values.length > 0){
215 oneValue.append("['");
216 for( int i=0; i < values.length; i++){
217 oneValue.append(values[i]);
218 if (i+1 < values.length) oneValue.append("';'");
220 oneValue.append("']");
222 out.println("--------");
223 out.println(" Name: '" + ps.Name + "' contains String[]:");
224 out.println(oneValue.toString());
225 out.println("--------");
227 } else if (ps.Value instanceof PropertyValue){
228 out.println("--------");
229 out.println(" Name: '" + ps.Name + "' contains PropertyValue:");
230 printPropertyValuePairs((PropertyValue)ps.Value, out);
231 out.println("--------");
233 } else if (ps.Value instanceof PropertyValue[]){
234 out.println("--------");
235 out.println(" Name: '" + ps.Name + "' contains PropertyValue[]:");
236 printPropertyValueSequencePairs((PropertyValue[])ps.Value, out);
237 out.println("--------");
239 } else {
240 out.println("Name: '" + ps.Name + "' Value: '" + ps.Value.toString() + "'");
245 * Print the names of all properties inside this property set
246 * @param ps The property set which is printed.
247 * @see com.sun.star.beans.XPropertySet
249 public static void printPropertiesNames(XPropertySet ps) {
250 XPropertySetInfo psi = ps.getPropertySetInfo();
251 Property[] props = psi.getProperties();
252 for (int i = 0; i < props.length; i++)
253 System.out.println(i + ". " + props[i].Name);
257 * Print the supported services of a UNO object.
258 * @param aObject A UNO object.
260 public static void getSuppServices (Object aObject) {
261 XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class,aObject);
262 printArray(xSI.getSupportedServiceNames());
263 StringBuilder str = new StringBuilder("Therein not Supported Service");
264 boolean notSupportedServices = false;
265 for (int i=0;i<xSI.getSupportedServiceNames().length;i++) {
266 if (! xSI.supportsService(xSI.getSupportedServiceNames()[i])) {
267 notSupportedServices = true;
268 str.append("\n").append(xSI.getSupportedServiceNames()[i]);
271 if (notSupportedServices)
272 System.out.println(str.toString());