Created
July 12, 2009 13:55
-
-
Save qnighy/145639 to your computer and use it in GitHub Desktop.
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 org.acikelabo; | |
import java.lang.reflect.Array; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Map; | |
import java.util.Set; | |
public class RubyInspect { | |
private static HashMap<Class<?>, InspectMethod> inspectMethods; | |
static { | |
inspectMethods = new HashMap<Class<?>, InspectMethod>(); | |
addInspectMethod(String.class, new InspectMethod() { | |
@Override | |
public String inspect(Object obj, RubyInspect inspector) { | |
StringBuilder ret = new StringBuilder(); | |
ret.append('\"'); | |
for(char ch : ((String)obj).toCharArray()) { | |
switch(ch) { | |
case '\b': | |
ret.append("\\b"); | |
break; | |
case '\f': | |
ret.append("\\f"); | |
break; | |
case '\n': | |
ret.append("\\n"); | |
break; | |
case '\r': | |
ret.append("\\r"); | |
break; | |
case '\t': | |
ret.append("\\t"); | |
break; | |
case '\'': | |
ret.append("\\'"); | |
break; | |
case '\"': | |
ret.append("\\\""); | |
break; | |
default: | |
ret.append(ch); | |
break; | |
} | |
} | |
ret.append('\"'); | |
return ret.toString(); | |
} | |
}); | |
addInspectMethod(Map.class, new InspectMethod() { | |
@Override | |
public String inspect(Object obj, RubyInspect inspector) { | |
Map hash = (Map)obj; | |
StringBuilder ret = new StringBuilder(); | |
ret.append('{'); | |
boolean first = true; | |
for(Object key : hash.keySet()) { | |
if(!first)ret.append(", "); | |
first = false; | |
ret.append(inspector.continueInspect(key)); | |
ret.append(" => "); | |
ret.append(inspector.continueInspect(hash.get(key))); | |
} | |
ret.append('}'); | |
return ret.toString(); | |
} | |
}); | |
addInspectMethod(Set.class, new InspectMethod() { | |
@Override | |
public String inspect(Object obj, RubyInspect inspector) { | |
Set set = (Set)obj; | |
StringBuilder ret = new StringBuilder(); | |
ret.append('{'); | |
boolean first = true; | |
for(Object key : set) { | |
if(!first)ret.append(", "); | |
first = false; | |
ret.append(inspector.continueInspect(key)); | |
} | |
ret.append('}'); | |
return ret.toString(); | |
} | |
}); | |
} | |
public static String inspect(Object obj) { | |
return new RubyInspect().continueInspect(obj); | |
} | |
public static void p(Object obj) { | |
System.out.println(inspect(obj)); | |
} | |
public static synchronized void addInspectMethod(Class<?> clazz, InspectMethod method) { | |
inspectMethods.put(clazz, method); | |
} | |
public static interface InspectMethod { | |
public String inspect(Object obj, RubyInspect inspector); | |
} | |
private HashSet<Object> objs = new HashSet<Object>(); | |
private RubyInspect() {} | |
public String continueInspect(Object obj) { | |
if(objs.contains(obj)) { | |
return "..."; | |
} | |
objs.add(obj); | |
if(obj.getClass().isArray()) { | |
StringBuilder ret = new StringBuilder(); | |
ret.append('['); | |
for(int i = 0; i < Array.getLength(obj); i++) { | |
if(i!=0)ret.append(", "); | |
ret.append(continueInspect(Array.get(obj, i))); | |
} | |
ret.append(']'); | |
objs.remove(obj); | |
return ret.toString(); | |
} | |
for(Class<?> clazz = obj.getClass(); clazz != null; clazz = clazz.getSuperclass()) { | |
if(inspectMethods.containsKey(clazz)) { | |
return inspectMethods.get(clazz).inspect(obj, this); | |
} | |
} | |
for(Class<?> clazz : obj.getClass().getInterfaces()) { | |
if(inspectMethods.containsKey(clazz)) { | |
return inspectMethods.get(clazz).inspect(obj, this); | |
} | |
} | |
return obj.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment