1 /*
2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation. Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package org.netbeans.jemmy;
26 
27 import java.lang.reflect.Field;
28 import java.lang.reflect.Method;
29 import java.lang.reflect.Modifier;
30 
31 /**
32  *
33  * Class to display information about object: fields, methods, ancestors and so
34  * on.
35  *
36  * @author Alexandre Iline (alexandre.iline@oracle.com)
37  */
38 public class ObjectBrowser implements Outputable {
39 
40     private Object object;
41 
42     private TestOut output;
43 
44     /**
45      * Constructor.
46      */
ObjectBrowser()47     public ObjectBrowser() {
48     }
49 
50     /**
51      * Defines print output streams or writers.
52      *
53      * @param out Identify the streams or writers used for print output.
54      * @see org.netbeans.jemmy.Outputable
55      * @see org.netbeans.jemmy.TestOut
56      * @see #getOutput
57      */
58     @Override
setOutput(TestOut out)59     public void setOutput(TestOut out) {
60         output = out;
61     }
62 
63     /**
64      * Returns print output streams or writers.
65      *
66      * @return an object that contains references to objects for printing to
67      * output and err streams.
68      * @see org.netbeans.jemmy.Outputable
69      * @see org.netbeans.jemmy.TestOut
70      * @see #setOutput
71      */
72     @Override
getOutput()73     public TestOut getOutput() {
74         return output;
75     }
76 
77     /**
78      * Specifies the object value.
79      *
80      * @param obj Object to work with.
81      * @see #getObject
82      */
setObject(Object obj)83     public void setObject(Object obj) {
84         object = obj;
85     }
86 
87     /**
88      * Returns the object value.
89      *
90      * @return Current object.
91      * @see #setObject
92      */
getObject()93     public Object getObject() {
94         return object;
95     }
96 
97     /**
98      * Prints {@code toString()} information.
99      */
printToString()100     public void printToString() {
101         output.printLine(object.toString());
102     }
103 
104     /**
105      * Prints object fields names and values.
106      */
printFields()107     public void printFields() {
108         Class<?> cl = object.getClass();
109         output.printLine("Class: " + cl.getName());
110         output.printLine("Fields: ");
111         Field[] fields = cl.getFields();
112         for (Field field : fields) {
113             output.printLine(Modifier.toString(field.getModifiers()) + " "
114                     + field.getType().getName() + " "
115                     + field.getName());
116             Object value = "Inaccessible";
117             try {
118                 value = field.get(object);
119             } catch (IllegalAccessException ignored) {
120             }
121             output.printLine("    Value: " + value.toString());
122         }
123     }
124 
125     /**
126      * Prints object methods names and parameters.
127      */
printMethods()128     public void printMethods() {
129         Class<?> cl = object.getClass();
130         output.printLine("Class: " + cl.getName());
131         output.printLine("Methods: ");
132         Method[] methods = cl.getMethods();
133         for (Method method : methods) {
134             output.printLine(Modifier.toString(method.getModifiers()) + " "
135                     + method.getReturnType().getName() + " "
136                     + method.getName());
137             Class<?>[] params = method.getParameterTypes();
138             for (Class<?> param : params) {
139                 output.printLine("    " + param.getName());
140             }
141         }
142     }
143 
144     /**
145      * Prints allsuperclasses names.
146      */
printClasses()147     public void printClasses() {
148         Class<?> cl = object.getClass();
149         do {
150             output.printLine(cl.getName());
151         } while ((cl = cl.getSuperclass()) != null);
152     }
153 
154     /**
155      * Prints everything.
156      */
printFull()157     public void printFull() {
158         printFields();
159         printMethods();
160     }
161 }
162