- bluespring
- 等级: 初级会员
- 文章: 32
- 积分: 50
- 来自: ...
|
备份一个有用的工具类.TypeUtil
它的typeToString(String scope, Object obj)方法,采用java的reflect机制,可以打印出任何对象的内容.
这对调试程序非常有用.
使用方法:
如果你有一个对象(比如testClassObject),想打印它的内容,可用如下方法:
System.out.println(TypeUtil.typeToString("yourClassObjectName",testClassObject));
这个方法,对调试那些对容器依赖的ejb程序很有用,特此备份.
以下为TypeUtil源程序:
java 代码
-
-
-
-
-
-
-
-
-
-
-
- import java.util.*;
- import java.lang.reflect.*;
-
-
- public class TypeUtil {
-
-
-
-
-
-
-
-
-
-
-
- private static String complexTypeToString(String scope, Object parentObject,List visitedObjs) {
-
- StringBuffer buffer = new StringBuffer("");
-
- try {
-
-
-
-
-
- Class cl = parentObject.getClass();
- while ( cl != null ) {
-
- processFields(cl.getDeclaredFields(),
- scope,
- parentObject,
- buffer,
- visitedObjs );
-
- cl = cl.getSuperclass();
- }
- } catch (IllegalAccessException iae) {
- buffer.append(iae.toString());
- }
-
- return (buffer.toString());
- }
-
-
-
-
-
-
-
-
-
-
- private static void processFields( Field[] fields,
- String scope,
- Object parentObject,
- StringBuffer buffer,
- List visitedObjs ) throws IllegalAccessException {
-
- for (int i = 0; i < fields.length; i++) {
-
-
-
-
- if (fields[i].getName().equals("__discriminator")
- || fields[i].getName().equals("__uninitialized")) {
- continue;
- }
-
-
-
-
-
- fields[i].setAccessible(true);
-
- if (Modifier.isStatic(fields[i].getModifiers())) {
-
-
-
-
-
- } else {
- buffer.append(
- typeToString(scope + "." + fields[i].getName(), fields[i].get(parentObject), visitedObjs));
- }
- }
-
- }
-
-
-
-
-
-
- public static boolean isCollectionType(Object obj) {
-
- return( obj.getClass().isArray()||
- (obj instanceof Collection)||
- (obj instanceof Hashtable)||
- (obj instanceof HashMap)||
- (obj instanceof HashSet)||
- (obj instanceof List)||
- (obj instanceof AbstractMap ) );
- }
-
-
-
-
-
-
- public static boolean isComplexType(Object obj) {
-
- if ( obj instanceof Boolean ||
- obj instanceof Short ||
- obj instanceof Byte ||
- obj instanceof Integer ||
- obj instanceof Long ||
- obj instanceof Float ||
- obj instanceof Character ||
- obj instanceof Double ||
- obj instanceof String ) {
-
- return false;
- }
- else {
-
- Class objectClass = obj.getClass();
-
- if (objectClass == boolean.class
- || objectClass == Boolean.class
- || objectClass == short.class
- || objectClass == Short.class
- || objectClass == byte.class
- || objectClass == Byte.class
- || objectClass == int.class
- || objectClass == Integer.class
- || objectClass == long.class
- || objectClass == Long.class
- || objectClass == float.class
- || objectClass == Float.class
- || objectClass == char.class
- || objectClass == Character.class
- || objectClass == double.class
- || objectClass == Double.class
- || objectClass == String.class ) {
-
- return false;
-
- }
-
- else {
- return true;
- }
- }
- }
-
-
-
-
-
-
-
-
-
- private static String collectionTypeToString(String scope, Object obj, List visitedObjs) {
-
- StringBuffer buffer = new StringBuffer("");
-
- if (obj.getClass().isArray()) {
- if (Array.getLength(obj) > 0) {
-
- for (int j = 0; j < Array.getLength(obj); j++) {
-
- Object x = Array.get(obj, j);
-
- buffer.append(typeToString(scope + "[" + j + "]", x, visitedObjs));
- }
-
- } else {
- buffer.append(scope + "[]: empty\n");
- }
- } else {
- boolean isCollection = (obj instanceof Collection);
- boolean isHashTable = (obj instanceof Hashtable);
- boolean isHashMap = (obj instanceof HashMap);
- boolean isHashSet = (obj instanceof HashSet);
- boolean isAbstractMap = (obj instanceof AbstractMap);
- boolean isMap = isAbstractMap || isHashMap || isHashTable;
-
- if (isMap) {
- Set keySet = ((Map) obj).keySet();
- Iterator iterator = keySet.iterator();
- int size = keySet.size();
-
- if (size > 0) {
-
- for (int j = 0; iterator.hasNext(); j++) {
-
- Object key = iterator.next();
- Object x = ((Map) obj).get(key);
-
- buffer.append(typeToString(scope + "[\"" + key + "\"]", x, visitedObjs));
- }
- } else {
- buffer.append(scope + "[]: empty\n");
- }
- } else
- if (
- isCollection || isHashSet
- ) {
-
- Iterator iterator = null;
- int size = 0;
-
- if (obj != null) {
-
- if (isCollection) {
- iterator = ((Collection) obj).iterator();
- size = ((Collection) obj).size();
- } else
- if (isHashTable) {
- iterator = ((Hashtable) obj).values().iterator();
- size = ((Hashtable) obj).size();
- } else
- if (isHashSet) {
- iterator = ((HashSet) obj).iterator();
- size = ((HashSet) obj).size();
- } else
- if (isHashMap) {
- iterator = ((HashMap) obj).values().iterator();
- size = ((HashMap) obj).size();
- }
-
- if (size > 0) {
-
- for (int j = 0; iterator.hasNext(); j++) {
-
- Object x = iterator.next();
- buffer.append(typeToString(scope + "[" + j + "]", x, visitedObjs));
- }
- } else {
- buffer.append(scope + "[]: empty\n");
- }
- } else {
-
-
- buffer.append(scope + "[]: null\n");
- }
- }
- }
-
- return (buffer.toString());
-
- }
-
-
-
-
-
-
-
- private static String typeToString(String scope, Object obj, List visitedObjs) {
-
- if (obj == null) {
- return (scope + ": null\n");
- }
- else if (isCollectionType( obj ) ) {
- return collectionTypeToString( scope, obj, visitedObjs );
- }
- else if (isComplexType( obj ) ) {
- if( ! visitedObjs.contains(obj)) {
- visitedObjs.add(obj);
- return complexTypeToString( scope, obj, visitedObjs ) ;
- }
- else {
- return(scope + ": \n" );
- }
- }
- else {
- return ( scope + ": " + obj.toString() + "\n");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static String typeToString(String scope, Object obj) {
-
- if (obj == null) {
- return (scope + ": null\n");
- }
- else if (isCollectionType( obj ) ) {
- return collectionTypeToString( scope, obj, new ArrayList());
- }
- else if (isComplexType( obj ) ) {
- return complexTypeToString( scope, obj, new ArrayList() ) ;
- }
- else {
- return ( scope + ": " + obj.toString() + "\n");
- }
- }
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- ming500
- 等级:
- 性别:
- 文章: 22
- 积分: 207
- 来自: 北京
|
非常好的工具。我也曾经写过了一个。当时对 数组的数据值出来太麻烦,实现的不好。
学习了。
|
返回顶楼 |
|
|