Created
November 7, 2011 23:11
-
-
Save bmchild/1346495 to your computer and use it in GitHub Desktop.
toString Utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mypackage; | |
import java.lang.reflect.Method; | |
import java.util.Enumeration; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpSession; | |
public class ToStringUtil { | |
private static final String eol = System.getProperty("line.separator"); | |
private static final String treeSeperator = "| "; | |
/** | |
* Creates a list of getters, session data and attributes, and headers | |
* | |
* @param request | |
* @return | |
*/ | |
public static String toString(HttpServletRequest request) {return toString(request, 0);} | |
public static String toString(final HttpServletRequest request, int depth) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
stringBuilder.append( toString( (Object)request, depth ) ); | |
stringBuilder | |
.append( printTreeSeperator(depth)) | |
.append( "SESSION" ).append( eol ) | |
.append( toString( request.getSession(), depth + 1) ); | |
Enumeration<?> headerNames = request.getHeaderNames(); | |
stringBuilder | |
.append( printTreeSeperator(depth)) | |
.append( "HEADERS" ).append( eol ); | |
while(headerNames.hasMoreElements()) { | |
String headerName = (String)headerNames.nextElement(); | |
stringBuilder | |
.append(printTreeSeperator(depth)) | |
.append( headerName ).append(": ") | |
.append( request.getHeader(headerName) ) | |
.append( eol ); | |
} | |
return stringBuilder.toString(); | |
} | |
public static String toString(HttpSession session) {return toString(session, 0);} | |
public static String toString(final HttpSession session, int depth) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
stringBuilder.append( toString( (Object)session, depth ) ); | |
Enumeration<?> attributes = session.getAttributeNames(); | |
stringBuilder | |
.append( printTreeSeperator(depth)) | |
.append( "ATTRIBUTES" ).append( eol ); | |
while(attributes.hasMoreElements()) { | |
String attributeName = (String)attributes.nextElement(); | |
stringBuilder | |
.append( printTreeSeperator(depth) ) | |
.append( attributeName ).append(": ") | |
.append( eol ) | |
.append( toString( session.getAttribute(attributeName), depth + 1 ) ); | |
} | |
return stringBuilder.toString(); | |
} | |
/** | |
* | |
* Uses reflection to make a list from getter methods. | |
* Ignores methods with return type whose names contains Stream or Reader. | |
* | |
* @param object | |
* @return | |
*/ | |
public static String toString(Object object) {return toString(object, 0);} | |
public static String toString(final Object object, int depth) { | |
StringBuilder stringBuilder = new StringBuilder(); | |
stringBuilder | |
.append(printTreeSeperator(depth)) | |
.append(object.getClass().getName() + '@' + Integer.toHexString(object.hashCode())).append(eol); | |
Class<?> clazz = object.getClass(); | |
//check if toString is overridden in the class | |
boolean implmentedToString = false; | |
try { | |
String toStringClassName = clazz.getMethod("toString").getDeclaringClass().getName(); | |
if( toStringClassName.equals(object.getClass().getName()) ) { | |
stringBuilder | |
.append(printTreeSeperator(depth)) | |
.append ( object.toString() ) | |
.append( eol ); | |
implmentedToString = true; | |
} | |
} catch (Exception e1) {} | |
if(!implmentedToString) { | |
Method[] methods = clazz.getDeclaredMethods(); | |
if(methods != null) { | |
for (Method method : methods) { | |
//Check no parameters | |
Class<?>[] parameters = method.getParameterTypes(); | |
String returnTypeName = method.getReturnType().getName(); | |
if( (method.getName().contains("get") || method.getName().contains("is")) | |
&& !returnTypeName.contains("Stream") | |
&& !returnTypeName.contains("Reader") | |
&& parameters.length == 0 ) { | |
try { | |
stringBuilder | |
.append(printTreeSeperator(depth)) | |
.append( method.getName() ) | |
.append( ": " ) | |
.append( method.invoke(object, new Object[]{}) ) | |
.append( eol ); | |
} catch (Exception e) { | |
stringBuilder | |
.append(printTreeSeperator(depth)) | |
.append( method.getName() ) | |
.append( ": Exception=" ) | |
.append( e.getMessage() ) | |
.append( eol ); | |
} | |
} | |
} | |
} | |
} | |
return stringBuilder.toString(); | |
} | |
/** | |
* @param depth | |
* @return | |
*/ | |
private static String printTreeSeperator(int depth) { | |
depth++; | |
StringBuilder stringBuilder = new StringBuilder(); | |
for(int i = 0; i < depth; i++) { | |
stringBuilder.append(treeSeperator); | |
} | |
return stringBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment