001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy;
048:
049: import java.lang.reflect.Field;
050: import java.lang.reflect.Modifier;
051: import java.lang.reflect.Method;
052:
053: /**
054: *
055: * Class to display information about object: fields, methods, ancestors and so on.
056: *
057: * @author Alexandre Iline (alexandre.iline@sun.com)
058: */
059:
060: public class ObjectBrowser implements Outputable {
061: private Object object;
062:
063: private TestOut output;
064:
065: /**
066: * Constructor.
067: */
068: public ObjectBrowser() {
069: }
070:
071: /**
072: * Defines print output streams or writers.
073: * @param out Identify the streams or writers used for print output.
074: * @see org.netbeans.jemmy.Outputable
075: * @see org.netbeans.jemmy.TestOut
076: * @see #getOutput
077: */
078: public void setOutput(TestOut out) {
079: output = out;
080: }
081:
082: /**
083: * Returns print output streams or writers.
084: * @return an object that contains references to objects for
085: * printing to output and err streams.
086: * @see org.netbeans.jemmy.Outputable
087: * @see org.netbeans.jemmy.TestOut
088: * @see #setOutput
089: */
090: public TestOut getOutput() {
091: return (output);
092: }
093:
094: /**
095: * Specifies the object value.
096: * @param obj Object to work with.
097: * @see #getObject
098: */
099: public void setObject(Object obj) {
100: object = obj;
101: }
102:
103: /**
104: * Returns the object value.
105: * @return Current object.
106: * @see #setObject
107: */
108: public Object getObject() {
109: return (object);
110: }
111:
112: /**
113: * Prints <code>toString()</code> information.
114: */
115: public void printToString() {
116: output.printLine(object.toString());
117: }
118:
119: /**
120: * Prints object fields names and values.
121: */
122: public void printFields() {
123: Class cl = object.getClass();
124: output.printLine("Class: " + cl.getName());
125: output.printLine("Fields: ");
126: Field[] fields = cl.getFields();
127: for (int i = 0; i < fields.length; i++) {
128: output.printLine(Modifier
129: .toString(fields[i].getModifiers())
130: + " "
131: + fields[i].getType().getName()
132: + " "
133: + fields[i].getName());
134: Object value = "Inaccessible";
135: try {
136: value = fields[i].get(object);
137: } catch (IllegalAccessException e) {
138: }
139: output.printLine(" Value: " + value.toString());
140: }
141: }
142:
143: /**
144: * Prints object methods names and parameters.
145: */
146: public void printMethods() {
147: Class cl = object.getClass();
148: output.printLine("Class: " + cl.getName());
149: output.printLine("Methods: ");
150: Method[] methods = cl.getMethods();
151: for (int i = 0; i < methods.length; i++) {
152: output.printLine(Modifier.toString(methods[i]
153: .getModifiers())
154: + " "
155: + methods[i].getReturnType().getName()
156: + " "
157: + methods[i].getName());
158: Class[] params = methods[i].getParameterTypes();
159: for (int j = 0; j < params.length; j++) {
160: output.printLine(" " + params[j].getName());
161: }
162: }
163: }
164:
165: /**
166: * Prints allsuperclasses names.
167: */
168: public void printClasses() {
169: Class cl = object.getClass();
170: do {
171: output.printLine(cl.getName());
172: } while ((cl = cl.getSuperclass()) != null);
173: }
174:
175: /**
176: * Prints everything.
177: */
178: public void printFull() {
179: printFields();
180: printMethods();
181: }
182: }
|