ObjectBrowser.java revision 13978:1993af50385d
1171802Sdelphij/* 2170808Sdelphij * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3182739Sdelphij * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4171802Sdelphij * 5170808Sdelphij * This code is free software; you can redistribute it and/or modify it 6170808Sdelphij * under the terms of the GNU General Public License version 2 only, as 7170808Sdelphij * published by the Free Software Foundation. 8170808Sdelphij * 9170808Sdelphij * This code is distributed in the hope that it will be useful, but WITHOUT 10170808Sdelphij * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11170808Sdelphij * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12170808Sdelphij * version 2 for more details (a copy is included in the LICENSE file that 13170808Sdelphij * accompanied this code). 14170808Sdelphij * 15170808Sdelphij * You should have received a copy of the GNU General Public License version 16170808Sdelphij * 2 along with this work; if not, write to the Free Software Foundation, 17170808Sdelphij * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18170808Sdelphij * 19170808Sdelphij * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20170808Sdelphij * or visit www.oracle.com if you need additional information or have any 21170808Sdelphij * questions. 22170808Sdelphij */ 23170808Sdelphijpackage org.netbeans.jemmy; 24170808Sdelphij 25170808Sdelphijimport java.lang.reflect.Field; 26170808Sdelphijimport java.lang.reflect.Method; 27170808Sdelphijimport java.lang.reflect.Modifier; 28170808Sdelphij 29170808Sdelphij/** 30170808Sdelphij * 31170808Sdelphij * Class to display information about object: fields, methods, ancestors and so 32170808Sdelphij * on. 33170808Sdelphij * 34170808Sdelphij * @author Alexandre Iline (alexandre.iline@oracle.com) 35170808Sdelphij */ 36170808Sdelphijpublic class ObjectBrowser implements Outputable { 37170808Sdelphij 38170808Sdelphij private Object object; 39170808Sdelphij 40170808Sdelphij private TestOut output; 41170808Sdelphij 42170808Sdelphij /** 43170808Sdelphij * Constructor. 44170808Sdelphij */ 45170808Sdelphij public ObjectBrowser() { 46197850Sdelphij } 47197850Sdelphij 48170808Sdelphij /** 49170808Sdelphij * Defines print output streams or writers. 50170808Sdelphij * 51170808Sdelphij * @param out Identify the streams or writers used for print output. 52170808Sdelphij * @see org.netbeans.jemmy.Outputable 53170808Sdelphij * @see org.netbeans.jemmy.TestOut 54170808Sdelphij * @see #getOutput 55170808Sdelphij */ 56170808Sdelphij @Override 57188929Salc public void setOutput(TestOut out) { 58170808Sdelphij output = out; 59170808Sdelphij } 60170808Sdelphij 61170808Sdelphij /** 62170808Sdelphij * Returns print output streams or writers. 63170808Sdelphij * 64170808Sdelphij * @return an object that contains references to objects for printing to 65170808Sdelphij * output and err streams. 66171069Sdelphij * @see org.netbeans.jemmy.Outputable 67170808Sdelphij * @see org.netbeans.jemmy.TestOut 68170808Sdelphij * @see #setOutput 69170808Sdelphij */ 70170808Sdelphij @Override 71170808Sdelphij public TestOut getOutput() { 72170808Sdelphij return output; 73170808Sdelphij } 74170808Sdelphij 75170808Sdelphij /** 76170808Sdelphij * Specifies the object value. 77170808Sdelphij * 78170808Sdelphij * @param obj Object to work with. 79170808Sdelphij * @see #getObject 80170808Sdelphij */ 81191990Sattilio public void setObject(Object obj) { 82170808Sdelphij object = obj; 83170808Sdelphij } 84170808Sdelphij 85170808Sdelphij /** 86170808Sdelphij * Returns the object value. 87170808Sdelphij * 88170808Sdelphij * @return Current object. 89170808Sdelphij * @see #setObject 90197953Sdelphij */ 91197953Sdelphij public Object getObject() { 92197953Sdelphij return object; 93197953Sdelphij } 94197953Sdelphij 95170808Sdelphij /** 96171799Sdelphij * Prints {@code toString()} information. 97171799Sdelphij */ 98176559Sattilio public void printToString() { 99171802Sdelphij output.printLine(object.toString()); 100175294Sattilio } 101170808Sdelphij 102171799Sdelphij /** 103191990Sattilio * Prints object fields names and values. 104170808Sdelphij */ 105175202Sattilio public void printFields() { 106171802Sdelphij Class<?> cl = object.getClass(); 107170808Sdelphij output.printLine("Class: " + cl.getName()); 108170808Sdelphij output.printLine("Fields: "); 109170808Sdelphij Field[] fields = cl.getFields(); 110170808Sdelphij for (Field field : fields) { 111170808Sdelphij output.printLine(Modifier.toString(field.getModifiers()) + " " 112188318Skib + field.getType().getName() + " " 113170808Sdelphij + field.getName()); 114170808Sdelphij Object value = "Inaccessible"; 115170808Sdelphij try { 116170808Sdelphij value = field.get(object); 117170808Sdelphij } catch (IllegalAccessException ignored) { 118170808Sdelphij } 119170808Sdelphij output.printLine(" Value: " + value.toString()); 120170808Sdelphij } 121170808Sdelphij } 122170808Sdelphij 123170808Sdelphij /** 124170808Sdelphij * Prints object methods names and parameters. 125170808Sdelphij */ 126170808Sdelphij public void printMethods() { 127170808Sdelphij Class<?> cl = object.getClass(); 128170808Sdelphij output.printLine("Class: " + cl.getName()); 129170808Sdelphij output.printLine("Methods: "); 130170808Sdelphij Method[] methods = cl.getMethods(); 131170808Sdelphij for (Method method : methods) { 132170808Sdelphij output.printLine(Modifier.toString(method.getModifiers()) + " " 133170808Sdelphij + method.getReturnType().getName() + " " 134170808Sdelphij + method.getName()); 135170808Sdelphij Class<?>[] params = method.getParameterTypes(); 136170808Sdelphij for (Class<?> param : params) { 137170808Sdelphij output.printLine(" " + param.getName()); 138170808Sdelphij } 139170808Sdelphij } 140170808Sdelphij } 141170808Sdelphij 142170808Sdelphij /** 143170808Sdelphij * Prints allsuperclasses names. 144170808Sdelphij */ 145170808Sdelphij public void printClasses() { 146170808Sdelphij Class<?> cl = object.getClass(); 147170808Sdelphij do { 148170808Sdelphij output.printLine(cl.getName()); 149170808Sdelphij } while ((cl = cl.getSuperclass()) != null); 150170808Sdelphij } 151170808Sdelphij 152170808Sdelphij /** 153170808Sdelphij * Prints everything. 154170808Sdelphij */ 155170808Sdelphij public void printFull() { 156170808Sdelphij printFields(); 157170808Sdelphij printMethods(); 158170808Sdelphij } 159170808Sdelphij} 160170808Sdelphij