Added depth parameter to dump method
This commit is contained in:
parent
e28c84efb3
commit
581b76687f
2 changed files with 66 additions and 88 deletions
|
|
@ -1,37 +0,0 @@
|
||||||
/*
|
|
||||||
* The MIT License (MIT)
|
|
||||||
*
|
|
||||||
* Copyright (c) 2015 Ziver Koc
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package zutil;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If an class implements this interface and runs it through
|
|
||||||
* MultiPrintStream.dump() then all the values in the object
|
|
||||||
* will be written out.
|
|
||||||
*
|
|
||||||
* @author Ziver
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public interface Dumpable {
|
|
||||||
|
|
||||||
}
|
|
||||||
117
src/zutil/io/MultiPrintStream.java
Normal file → Executable file
117
src/zutil/io/MultiPrintStream.java
Normal file → Executable file
|
|
@ -24,11 +24,13 @@
|
||||||
|
|
||||||
package zutil.io;
|
package zutil.io;
|
||||||
|
|
||||||
import zutil.Dumpable;
|
|
||||||
|
import zutil.ClassUtil;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
@ -180,51 +182,60 @@ public class MultiPrintStream extends PrintStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dumps the content of:
|
* Same as {@link #dump(Object, int)} but prints to this OutputStream.
|
||||||
* <br>- Array content
|
|
||||||
* <br>- Map content (HashMap etc.)
|
|
||||||
* <br>- List content (ArrayList, LinkedList etc.)
|
|
||||||
* <br>- InputStream content (Prints out until the end of the stream)
|
|
||||||
* <br>- Reader content (Prints out until the end of the reader)
|
|
||||||
* <br>- Instance variables of a Object
|
|
||||||
*
|
*
|
||||||
* @param o is the Object to dump
|
* @param o is the Object to dump
|
||||||
*/
|
*/
|
||||||
public void dump( Object o ){
|
public void dump(Object o){
|
||||||
println(dumpToString( o ));
|
println(dumpToString( o, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as {@link #dump(Object, int)} but prints to this OutputStream.
|
||||||
|
*
|
||||||
|
* @param o is the Object to dump
|
||||||
|
* @param depth sets the object dump depth, the object recursion depth
|
||||||
|
*/
|
||||||
|
public void dump(Object o, int depth){
|
||||||
|
println(dumpToString( o, depth));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dumps the content of:
|
* Same as {@link #dump(Object, int)}
|
||||||
* <br>- Array content
|
*
|
||||||
* <br>- Map content (HashMap etc.)
|
* @param o is the Object to dump
|
||||||
* <br>- List content (ArrayList, LinkedList etc.)
|
|
||||||
* <br>- InputStream content (Prints out until the end of the stream)
|
|
||||||
* <br>- Reader content (Prints out until the end of the reader)
|
|
||||||
* <br>- Instance variables of a Object
|
|
||||||
*
|
|
||||||
* @param o is the Object to dump
|
|
||||||
* @return a String with all the printed data
|
* @return a String with all the printed data
|
||||||
*/
|
*/
|
||||||
public String dumpToString( Object o) {
|
public static String dumpToString(Object o) {
|
||||||
return dumpToString(o, "");
|
return dumpToString(o, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dumps the content of:
|
||||||
|
* <br>- Array content
|
||||||
|
* <br>- Map content (HashMap etc.)
|
||||||
|
* <br>- List content (ArrayList, LinkedList etc.)
|
||||||
|
* <br>- InputStream content (Prints out until the end of the stream)
|
||||||
|
* <br>- Reader content (Prints out until the end of the reader)
|
||||||
|
* <br>- Instance variables of a Object
|
||||||
|
*
|
||||||
|
* @param o is the Object to dump
|
||||||
|
* @param depth sets the object dump depth, the object recursion depth
|
||||||
|
*/
|
||||||
|
public static String dumpToString(Object o, int depth) {
|
||||||
|
return dumpToString(o, "", depth);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dumps the content of:
|
* See {@link #dumpToString(Object)}
|
||||||
* <br>- Array content
|
*
|
||||||
* <br>- Map content (HashMap etc.)
|
|
||||||
* <br>- List content (ArrayList, LinkedList etc.)
|
|
||||||
* <br>- InputStream content (Prints out until the end of the stream)
|
|
||||||
* <br>- Reader content (Prints out until the end of the reader)
|
|
||||||
* <br>- Instance variables of a Object
|
|
||||||
*
|
|
||||||
* @param o is the Object to dump
|
* @param o is the Object to dump
|
||||||
* @param head is the string that will be put in front of every line
|
* @param head is the string that will be put in front of every line
|
||||||
|
* @param depth sets the object dump depth, the object recursion depth
|
||||||
* @return A String with all the printed data
|
* @return A String with all the printed data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private String dumpToString( Object o , String head) {
|
private static String dumpToString(Object o , String head, int depth) {
|
||||||
if(o == null)
|
if(o == null)
|
||||||
return "NULL";
|
return "NULL";
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
|
@ -238,7 +249,8 @@ public class MultiPrintStream extends PrintStream {
|
||||||
Object value = Array.get(o,i);
|
Object value = Array.get(o,i);
|
||||||
buffer.append("\n");
|
buffer.append("\n");
|
||||||
buffer.append(nextHead);
|
buffer.append(nextHead);
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
buffer.append( (dumbCapable(value, depth-1) ?
|
||||||
|
dumpToString(value, nextHead, depth-1) : value) );
|
||||||
if ( i+1<Array.getLength(o) )
|
if ( i+1<Array.getLength(o) )
|
||||||
buffer.append( "," );
|
buffer.append( "," );
|
||||||
}
|
}
|
||||||
|
|
@ -249,18 +261,19 @@ public class MultiPrintStream extends PrintStream {
|
||||||
// Prints out a list
|
// Prints out a list
|
||||||
else if(o instanceof Collection){
|
else if(o instanceof Collection){
|
||||||
Iterator<?> it = ((Collection<?>)o).iterator();
|
Iterator<?> it = ((Collection<?>)o).iterator();
|
||||||
buffer.append( "{" );
|
buffer.append( "[" );
|
||||||
while(it.hasNext()){
|
while(it.hasNext()){
|
||||||
Object value = it.next();
|
Object value = it.next();
|
||||||
buffer.append("\n");
|
buffer.append("\n");
|
||||||
buffer.append(nextHead);
|
buffer.append(nextHead);
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
buffer.append( (dumbCapable(value, depth-1) ?
|
||||||
|
dumpToString(value, nextHead, depth-1) : value) );
|
||||||
if(it.hasNext())
|
if(it.hasNext())
|
||||||
buffer.append( "," );
|
buffer.append( "," );
|
||||||
}
|
}
|
||||||
buffer.append( "\n" );
|
buffer.append( "\n" );
|
||||||
buffer.append(head);
|
buffer.append(head);
|
||||||
buffer.append( "}" );
|
buffer.append( "]" );
|
||||||
}
|
}
|
||||||
// Prints out a Map
|
// Prints out a Map
|
||||||
else if(o instanceof Map){
|
else if(o instanceof Map){
|
||||||
|
|
@ -273,7 +286,8 @@ public class MultiPrintStream extends PrintStream {
|
||||||
buffer.append(nextHead);
|
buffer.append(nextHead);
|
||||||
buffer.append( key );
|
buffer.append( key );
|
||||||
buffer.append( "=>" );
|
buffer.append( "=>" );
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
buffer.append( (dumbCapable(value, depth-1) ?
|
||||||
|
dumpToString(value, nextHead, depth-1) : value) );
|
||||||
if(it.hasNext())
|
if(it.hasNext())
|
||||||
buffer.append( "," );
|
buffer.append( "," );
|
||||||
}
|
}
|
||||||
|
|
@ -293,7 +307,7 @@ public class MultiPrintStream extends PrintStream {
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace(this);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
buffer.append( "\n" );
|
buffer.append( "\n" );
|
||||||
buffer.append(head);
|
buffer.append(head);
|
||||||
|
|
@ -311,7 +325,7 @@ public class MultiPrintStream extends PrintStream {
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace(this);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
buffer.append( "\n" );
|
buffer.append( "\n" );
|
||||||
buffer.append(head);
|
buffer.append(head);
|
||||||
|
|
@ -323,17 +337,20 @@ public class MultiPrintStream extends PrintStream {
|
||||||
while ( oClass != null ) {
|
while ( oClass != null ) {
|
||||||
Field[] fields = oClass.getDeclaredFields();
|
Field[] fields = oClass.getDeclaredFields();
|
||||||
for ( int i=0; i<fields.length; i++ ) {
|
for ( int i=0; i<fields.length; i++ ) {
|
||||||
|
if (Modifier.isFinal(fields[i].getModifiers())) // Skip constants
|
||||||
|
continue;
|
||||||
fields[i].setAccessible( true );
|
fields[i].setAccessible( true );
|
||||||
buffer.append("\n");
|
buffer.append("\n");
|
||||||
buffer.append(nextHead);
|
buffer.append(nextHead);
|
||||||
buffer.append( fields[i].getType().getSimpleName() );
|
//buffer.append( fields[i].getType().getSimpleName() );
|
||||||
buffer.append( " " );
|
//buffer.append( " " );
|
||||||
buffer.append( fields[i].getName() );
|
buffer.append( fields[i].getName() );
|
||||||
buffer.append( " = " );
|
buffer.append( " = " );
|
||||||
try {
|
try {
|
||||||
Object value = fields[i].get(o);
|
Object value = fields[i].get(o);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) );
|
buffer.append( (dumbCapable(value, depth-1) ?
|
||||||
|
dumpToString(value, nextHead, depth-1) : value) );
|
||||||
}
|
}
|
||||||
} catch ( IllegalAccessException e ) {}
|
} catch ( IllegalAccessException e ) {}
|
||||||
if ( i+1<fields.length )
|
if ( i+1<fields.length )
|
||||||
|
|
@ -352,15 +369,13 @@ public class MultiPrintStream extends PrintStream {
|
||||||
/**
|
/**
|
||||||
* An helper function for the dump function.
|
* An helper function for the dump function.
|
||||||
*/
|
*/
|
||||||
private boolean dumbCapable(Object o){
|
private static boolean dumbCapable(Object o, int depth){
|
||||||
if(o != null){
|
if (depth <= 0)
|
||||||
if(o.getClass().isArray()) return true;
|
return false;
|
||||||
else if(o instanceof Collection)return true;
|
if (o == null)
|
||||||
else if(o instanceof Map)return true;
|
return false;
|
||||||
else if(o instanceof InputStream)return true;
|
if (ClassUtil.isPrimitive(o.getClass()) || ClassUtil.isWrapper(o.getClass()))
|
||||||
else if(o instanceof Reader)return true;
|
return false;
|
||||||
else if(o instanceof Dumpable)return true;
|
return true;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue